Repeat loop for in C

Asked

Viewed 622 times

-1

I have to do the following exercise:

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

The sorting part of 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;
}

2 answers

1

The best solution is the use of vectors. Follow the example:

To use the code, just copy the snippets inside 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 variable n will store the entered numbers, and ord will store the numbers ordered increasing.

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

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

The values read in n are already inserted in ord, this will facilitate when ordering 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, because it will always be the first number of the vector, thus being the smallest number found so far. And pos receives -1 to indicate that it is not related to any vector position.

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

When you find another value less than min, is recorded its value and its position.

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

When as found a number smaller than the min, pos shall be greater than or equal to 0, then enter this condition to make the exchange of 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;

}

-1

It’s been a while since I’ve touched C. But it seems your code doesn’t do the minimum necessary for ordering: Read 3 values. There is only one scanf and out of any loop. I don’t know how you tested the ordination, to come to the conclusion that it works.

I guess you’d have to do something like that:

int n1,n2,n3;

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

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

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

Then you can order the values ord1, ord2 and ord3, and show the 6 values, as requested by the exercise.

  • Your code reads the values but does not sort in ascending order as it is requested, when writing the numbers, both will be in the same order.

  • I imagined that the author of the question had no problem with the ordination, since he himself stated that it worked. : D

  • It’s that sorting 2 or 3 numbers is easy, but above said is not feasible with conditions, so it uses sorting algorithms. If in a VC response you can give solutions and improvements, this results in the quality of your response.

Browser other questions tagged

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