Repeat loop is in C

-1

I have to do the following exercise:

Read 3 integer values and sort them in ascending order. At the end, show the values in ascending order, a blank line and then the values in the sequence as read.

The part of sorting the numbers I got. I now need to display the index (counter) outside the for.

#include<stdio.h>

 int main() {

int n, i, maior, menor, meio;

scanf("%d", &n);
maior = n; menor = n; meio = n;

for(i = 1; i <= 2; i++) {
    scanf("%d", &n);
    if (n > maior) {
        maior = n;
    } else if (n < menor) {
        menor = n;
    } else {
        meio = n;
    }
}

  printf("%d\n", menor);
  printf("%d\n", meio);
  printf("%d\n", maior);
  printf("\n");

return 0;
}
    
asked by anonymous 12.04.2016 / 07:48

2 answers

1

The best solution is to use vectors. Here's the example:

  

To use the code, simply copy the snippets into the code boxes and paste them in order.

#include <stdio.h>

int main(int argc, char **argv){
    const int NUM = 3; // quantidade de valores lidos

Has 2 vectors with 3 spaces the n and ord . The n variable will store the entered numbers, and ord will store the ordered numbers incrementally.

    int n[NUM];
    int ord[NUM];

    int x,y;
    for(x=0; x<NUM; x++){
        scanf("%d",&n[x]);
        ord[x] = n[x];
    }

Values read in n are already entered in ord , so it will make it easier to sort the vector.

    int min; // numero mínimo
    int pos; // posição do número mínimo
    for(x=0; x<NUM; x++){
        min = ord[x];
        pos = -1;

The variable min will always receive the position x of ord , since it will always be the first number of the vector, thus being the smallest number found so far. And pos gets -1 to indicate that it is not related to any position of the vector.

        for(y=x; y<NUM; y++){

When you find another value less than min , its value and position are registered.

            if(ord[y] < min){
                min = ord[y];
                pos = y;
            }
        }

When you have found a number smaller than min , pos becomes greater than or equal to 0 , then enter this condition to exchange values.

        if(pos >= 0){
            ord[pos] = ord[x];
            ord[x] = min;
        }
    }

    printf("%d %d %d\n", ord[0], ord[1], ord[2]);
    printf("%d %d %d\n", n[0], n[1], n[2]);

    return 1;

}
    
12.04.2016 / 09:36
-1

It's been a long time since I moved C. But it looks like your code does not do the minimum necessary for sorting: Read 3 values. There is only a scanf and out of any loop . I do not know how you tested the ordination, to come to the conclusion that works.

I think you would have to do something like this:

int n1,n2,n3;

scanf("%d", &n1);
scanf("%d", &n2);
scanf("%d", &n3);

After this, you can sort the three values. However, the exercise asks for numbers to be displayed in the original order as well. So one solution is to back up the original values, something like this:

int ord1 = n1, ord2 = n2, ord3 = n3;

Then you can sort the values ord1 , ord2 and ord3 , and display the 6 values, as the exercise asks.

    
12.04.2016 / 08:05