Random numbers appear later in sorting algorithm

Asked

Viewed 63 times

3

At the end of my program the result is giving random numbers, which occurs?

main(){

    int i;
    int a[10];
    int x;
    int aux;

    for (i=0; i<10; i++){
    printf ("Entre com os numeros %i: ", i);
    scanf ("%i", & a[10]);
    }

for(i = 0; i < 10; i++){
    for (x=0; x<10; x++)
        if (a[i]>a[x+1]){

        aux = a[i];
        a[i] = a[i+1];
        a[i+1] = aux;

    }
}
printf ("Lista dos elementos em ordem a seguir:\n   ");

for (i = 0 ; i < 10; i++)
printf ("%i\n", a[i]);
}
  • I believe that the innermost link should start from i+1 and not from 0. Shouldn’t the exchange be between a[i] and a[x] in your program at[x+1]? I believe you have mixed two methods of ordination.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

4

There are several problems in the code. The main thing is that it is giving a scanf() in item 10 of array, which is not even an element that exists inside it, and picks up junk in memory. It goes from 0 to 9. And precisely why you should have used i there and no 10.

But the loop is also wrong, the internal should start from where it is on the external, ie from the i + 1, and not 0 as it was placed. Also the exchange should be with the element x and not with the i + 1.

I tweaked the code well to make it more readable and use some better stuff like prefer the %d in place of %i.

#include <stdio.h>

int main(void) {
    int a[10];
    for (int i = 0; i < 10; i++) {
        printf("Entre com os numeros %d: ", i);
        scanf("%d", &a[i]);
    }
    for (int i = 0; i < 10; i++) {
        for (int x = i + 1; x < 10; x++) {
            if (a[i] > a[x]) {
                int aux = a[i];
                a[i] = a[x];
                a[x] = aux;
            }
        }
    }
    printf ("Lista dos elementos em ordem a seguir:\n");
    for (int i = 0; i < 10; i++) printf("%d\n", a[i]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

0

happens that you are always saving the value in the last vector box by an error in reading:

scanf ("%i", & a[10]);

he should be:

scanf ("%i", & a[i]);

Where the loop counter indicates the position of the vector to store the entered value.

Where the crazy numbers come from?

So the crazy numbers that are actually popping up are leftover memories from other programs (which may be running or not, so pay attention to that, because dislocating a wrong memory space can shut down your pc or cause other running software to malfunction).

I rewrote the code you sent so it’ll work, take a look at it:

#include <stdio.h>

int main (){

  int i;
  int a[10];
  int x;
  int aux;

  for (i = 0; i < 10; i++){
      printf ("Entre com os numeros %i: ", i);
      scanf ("%i", &a[i]);
    }

  for (i = 0; i < 10; i++){
      for (x = 0; x < 10; x++){
            if (a[i] > a[x + 1]){
                aux = a[i];
                a[i] = a[i + 1];
                a[i + 1] = aux;
            }
        }
    }
    for (i = 0; i < 10; i++){
        printf ("Valor escrito: %i\n", a[i]);
    }
    return 0;
}
  • Thanks for the help!!

  • Dispose, my friend!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.