Alo
Alo

Reputation: 15

Problems in the for loop in language C

The program compiles perfectly, the problem is the cycle, it does not show me the position

here goes the include of library stdio.h

int main(void)
{ int x, i=0,j=0, a[100];
  char sal;

    printf("Este programa lee un arreglo de numeros enteros y un numero x, y muestra en pantalla los indices de las posiciones en donde se encuentra ese numero x\n");
    do
    {
        printf("Ingrese un numero: ");
        scanf("%i", &a[i]);
        i++;
        printf("Si desea salir presione s: ");
        scanf(" %c", &sal);
    }while(sal!='s');   

    printf("Ingrese el valor de la variable x: ");
    scanf("%i", &x);

    for (j; j<=i; j++)
    {
        if(a[i]==x) 
            printf("%i",i);
    }
    printf("\n");
}

Upvotes: 0

Views: 154

Answers (2)

Pablo
Pablo

Reputation: 13570

You condition of the for loop should be j<i, not j<i+1. When the while loop exits, i has the value for the next input, but it has not been set because the loop exited.

Also you are using the index i instead of j in the for-loop. j is the running index, not i:

for (j; j<i; j++)
{
    if(a[j]==x) 
        printf("%i", j);
}

would be correct.

Your while loop is ok, but a bit clunky. First you don't check if you are writing past the limit of a. The conditions should be

while(sal!='s' && i < (sizeof a / sizeof *a));

so that the user cannot input more values than a can hold.

The way you exit the loop is also awkward, the user has to type something different to s to continue and it can only be one character. This would be better:

int c;
char line[100] = { 0 };
do
{
    printf("Ingrese un numero: ");
    scanf("%i", &a[i]);
    while((c = getchar()) != '\n' && c != EOF); // to clear the input buffer
    i++;

    printf("Si desea salir ingrese SALIR. Para continuar presione ENTER: ");
    fgets(line, sizeof line, stdin);

} while(strcmp(line, "SALIR\n") && strcmp(line, "salir\n") && i < (sizeof a / sizeof *a));

Note that strcmp returns 0 when the strings are equal, a non-zero otherwise.

Upvotes: 1

Billy
Billy

Reputation: 183

A tip, you should always set the loop counter to 0 either right before the loop, or in the loop. And it's always a good practice to initialize all variables during declarations. In your case, you did not initialize integer array. Below is a modified version of your code.

int main(void)
{
    int x, i, j;
    x = i = j = 0;

    int a[100] = { 0 };

    char sal = 0;

    printf ("Este programa lee un arreglo de numeros enteros y un numero x, "
            "y muestra en pantalla los indices de las posiciones en donde se "
            "encuentra ese numero x\n");

    while(1)
    {
        printf("Ingrese un numero: ");
        scanf("%d", &a[i]);

        i++;

        printf("Si desea salir presione s: ");
        scanf(" %c", &sal);

        if (sal == 's')
            break;

    }

    printf("Ingrese el valor de la variable x: ");
    scanf("%d", &x);

    for (j = 0; j <= i; j++)
    {
        if (a[i] == x)
            printf("%d", i);
    }
    printf("\n");

}

Upvotes: 1

Related Questions