How to skip/ignore a specific position (on a list) within a loop, in C language?

A beginner in C language, I have to find the second and third smallest numbers from a list of N numbers.

void MenordeN (int N, int lista[100], int *c,int *posicion)
{
int menor,cont;
menor=lista[0];
for (cont=1;cont<N;cont++)
{
    if (lista[cont]<menor)
    {
        menor=lista[cont];
        *posicion= cont+1;
    }
}
*c=menor;

}

int main()
{
int lista[100],menor, n, con,res,pos;
printf("Ingrese el valor de N: \n");
scanf("%i",&n);
printf("Ingrese %i numeros: \n",n);
for (con=0;con<n;con++)
{
    scanf("%i",&lista[con]);
}
MenordeN(n,lista,&res,&pos);
printf("El elemento menor es: %i en posicion %i",res,pos);        //*res is the answer(smallest number)
As of now: void MenordeN is to find the smallest number and its position. Main calls void MenordeN. However, how do I skip posicion[res] so that I can call void MenordeN again to find the second smallest number?
int low = lista[0],m,i,idx; // init with first array element.
m = low;
for( i=0; i<3; ++i)
{
    for(idx=0; idx<=100; ++idx)
    {
        if (lista[idx]>low)
        {
            m = MIN(m,lista[idx]);
        }
    }
}
printf("\n3rd lowest value is %d\n", m);

return 0;
----->This just prints the first value entered, not the third smallest.
int main()
{
int lista[100],menor, n, con, res, pos,resp;
printf("Ingrese el valor de N: \n");
scanf("%i",&n);
printf("Ingrese %i numeros: \n",n);
for (con=0;con<n;con++)
{
    scanf("%i",&lista[con]);
}
MenordeN(n,lista,&res,&pos);
printf("El elemento menor es: %i en posicion %i",res,pos);
res=NULL;
MenordeN(n,lista,&resp,&pos);
printf("\nEl segundo elemento menor es: %i en posicion %i",resp,pos);
return 0;

---------------> I know this is wrong, but isn't there something simpler like res=NULL or lista[res]==NULL? My teacher wouldn't expect anything too complicated because we are still in programming I.

Answers


your list is technical an array. You can sort that (e.g. with qsort from stdlib.h) and access the n-th element from both sides: largest and smallest.

qsort is defined like this:

#include <stdlib.h>
...
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));

and to get the 3rd smallest element you access the sorted list (that is an array) like this:

printf("3rd element: %d\n", lista[2]);

The index starts at 0, not a 1.

If you don't want to sort the array, than you can do it like this (pseudo code)

#include <limits.h>
...
#ifndef MIN
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#endif

int low = lista[0]; // init with first array element.
for(int idx=0; idx<=100; ++idx) {
    low = MIN(low, lista[idx]);
}

int m = low;
for(int i=0; i<3; ++i) {
    for(int idx=0; idx<=100; ++idx) {
    if (lista[idx]>low) {
        m = MIN(m,lista[idx]);
    }
}
printf("3rd lowest value is %d\n", m);

try this

#include <limits.h>

...
MenordeN(n,lista,&res,&pos);
printf("El elemento menor es: %i en posicion %i",res,pos);
lista[pos-1]=INT_MAX;
MenordeN(n,lista,&resp,&pos);
printf("\nEl segundo elemento menor es: %i en posicion %i",resp,pos);

Need Your Help

Check the values of an dynamic object properties

powershell object

I have created an object as part of a customer import. Now each customer object has a dynamic set of properties that all with have the prefix: "Cust." . I just need to loop through these propertie...

Contentful without Composer

php composer-php contentful

I already uploaded all "Official PHP SDK for the Content Delivery API" files and folders, from GitHub, to a folder on my server named "Contentful".