0

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");
}
7
  • It did not work Commented Feb 19, 2018 at 0:32
  • It should be for(j; j< i; ++j) because i is the value of the last input plus one. And in the loop it should be if(a[j] == x) printf("%i ", j). Use j, the running index, not i. Commented Feb 19, 2018 at 0:42
  • regarding: for (j; j<=i; j++) The first parameter j; results in the following output from the compiler: "untitled1.c:18:5: warning: statement with no effect [-Wunused-value" Suggest using: for( ; j<=i; j++ ) Commented Feb 19, 2018 at 2:09
  • when calling any of the scanf() family of functions: Always check the returned value (not the parameter values) to assure the operation was successful. In all the calls to scanf(), in the posted code, any returned value other than 1 indicates an error occurred Commented Feb 19, 2018 at 2:11
  • when posting a question about a runtime problem, as this code is doing, Post a minimal reproducible example so we can reproduce the problem.. That includes the #include statements as making us guess as to which header files you actually included can/will result in the question being ignored Commented Feb 19, 2018 at 2:13

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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");

}

5 Comments

%d and %i are perfectly equivalent in printf; they are both valid in scanf as well, but there %d accepts only decimal values, while %i accepts octal and hexadecimal values as well.
Hey guys, thanks for information. I have been working in C for past few months, but did not know about %i. Thanks for enlightening me. :)
You could handle both 's' and 'S' for exit with if (sal == 's' || sal == 's' - 'a' + 'A')
Just another note: always, always check the return of scanf, e.g. if (scanf("%d", &a[i]) != 1) { /* handle error */ } With " %c" you will need to check if not EOF to protect against the user pressing Ctrl+d (or Ctrl+z on windoze) to generate a manual EOF to cancel input.
Hey David, that is a great suggestion. Error handling is crucial in world of software. Always check return value of methods and handle situations accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.