Home > Blockchain >  How to order a list of integers from greatest to least?
How to order a list of integers from greatest to least?

Time:09-16

I have an assignment where I must use a structure to put in student information. I must then order the credit hours from greatest to least. I am focused on the integer ordering loop, I just can't figure out why my program is outputting incorrectly.

#include <stdlib.h>
#include <stdio.h>

struct Student {
    char name[21];
    int credits;
} s[99];

int main()
{
    int students;
    int tempCred = 0;
    char tempName[21];

    printf("How many students?: ");
    scanf_s("%d", &students);

    for (int i = 0; i < students; i  )
    {
        printf("\nStudent Name?: ");
        scanf_s("%s", &s[i].name, 21);

        printf("\nCredits Taken?: ");
        scanf_s("%d", &s[i].credits);
    }

    for (int i = 0; i < students; i  ) {
        for (int j = 0; j < students; j  ) {
            if (s[j].credits > tempCred) {
                tempCred = s[j].credits;
                s[i].credits = s[j].credits;
                s[j].credits = tempCred;
            }
        }

        printf("\n%d", s[i].credits);
    }
}

For example, if I were to enter 2,6, and 8 when asked for credit hours, the program would output '8,6,8'. I am not unfamiliar with sorting, but for some reason something isn't making sense when I look at this code. Could anyone help me order the integers from greatest to least? Thanks!

NOTE: I am aware there are better ways to do this, but my professor is making us use strictly C, no C at all. I just need help ordering the integers.

CodePudding user response:

There are various techniques used for sorting. For instance the bubble sort, quick sort, insertion sort, etc. The simplest one is the bubble sort - but it's not the most efficient one.

In your program you have an array of structs. You've done the insertion part of the structs into the array and that's fine. The problem lies in the second part - the sorting. You have a for loop that starts at the very first element (i.e. 0) and goes all the way up to the last element (i.e. students-1). And nested inside this loop is another for loop - that also has the same range???

No, that's wrong. Instead replace the first and second for loops with this:

    for (int i = 0 ; i < students-1 ; i  ) 
    {
        for (int j = i 1 ; j < students ; j  ) 
        {
             ...
        }
    }

Here, the outer for loop begins with element 0 and goes up to the element before the last. The inner for loop starts with the next element to what the outer for loop stores (i.e. j = i 1). So if i = 0, j = 1. And this loop goes all the way up to the last element of the array of structs.

Now, inside the inner for loop specify the condition. In your case you want them sorted in descending order (highest to lowest) of the credits.

    for (int i = 0 ; i < students-1 ; i  ) 
    {
        for (int j = i 1 ; j < students ; j  ) 
        {
            if(s[j].credits > s[i].credits)         // then swap the credits
            {
                tempCred = s[j].credits ;
                s[j].credits = s[i].credits ;
                s[i].credits = tempCred ;
            }
        }
    }

Note that j is one greater that i. So if i = 0, j = 1, then the if statement reads

If the credits held in the struct in element 1 of the array is greater than the credits stored in the struct in element 0 of the array, then...

If the condition is met, the credits in these 2 structs are swapped.

This an implementation of the "bubble sort". See this for more techniques and explanations.

Finally, you can display the credits:

    for(int index = 0 ; index < students ; index  )
    {
        printf("\n%d", s[index].credits) ;
    }

Like a lot of people in the comments have said, use debugger. It'll help you trace the logic of your programs.

CodePudding user response:

Like @Barmar said use the qsort() function from glibc. Not only is easier than writting your own method but it is much faster at O(N log N) on average.

  •  Tags:  
  • c
  • Related