1

I have a program that stores a variety of information about 10 universities as structures in an array. I have a function named 'inputData' that loads the information of the universities into the array by converting them to structures. I am trying to write a function that will sort the universities by tuition (from highest to lowest). I tried to use selection sort to do this, but I keep running into an error somewhere down the line.

This is the structure:


struct University
{
    char name[50];
    char city[20];
    char state[3];
    int rank;
    int tuition;
};

This is the function to convert info to a structure:

struct University inputData(char Name[50], char City[20], char State[3], int Rank, int Tuition){
    struct University uni;
    strcpy(uni.name, Name);
    strcpy(uni.city, City);
    strcpy(uni.state, State);
    uni.rank=Rank;
    uni.tuition=Tuition;
    return uni;
};

Then I created a structure array and added all the info:

struct University university[10];
university[0]= inputData("Princeton University", "Princeton", "NJ", 1, 45320);
university[1]= inputData("University of Virginia", "Charlottesville", "VA", 24, 52040);
university[2]= inputData("Boston College", "Chestnut Hill", "MA", 31, 51296);
university[3]= inputData("Georgia Institute of Technology", "Atlanta", "GA", 34, 32404);
university[4]= inputData("Lehigh University", "Bethlehem", "PA", 44, 48320);
university[5]= inputData("University of Chicago", "Chicago", "IL", 3, 52491);
university[6]= inputData("Duke University", "Durham", "NC", 8, 51265);
university[7]= inputData("University of Georgia", "Athens", "GA", 56, 29844);
university[8]= inputData("University of Denver", "Denver", "CO", 86, 46362);
university[9]= inputData("Loyola University Chicago", "Chicago", "IL", 99, 26270);

I'm trying to sort using this function (which uses selection sort):

void printSortedUniversity(struct University* list){
    int i, j, max_idx; 
  
      for (i = 0; i < 9; i++) 
    { 
        max_idx = i; 
        for (j = i+1; j < 10; j++) 
          if (list[j].tuition > list[max_idx].tuition) 
            max_idx = j; 
  
        swap(&list[max_idx], &list[i]); 
    } 
    
    for (i=0; i<10; i++){
        printInfo(list[i]);
        printf("\n");
    }
};

And this is the swap function:

void swap(int *x, int *y) 
{ 
    int temp = *x; 
    *x = *y; 
    *y = temp; 
} 

I keep getting:

Name: Univceton University City: Princeton State: NJ Rank: 1 Tuition: 45320
Name: Prinersity of Virginia City: Charlottesville State: VA Rank: 24 Tuition: 52040
Name: Univon College City: Chestnut Hill State: MA Rank: 31 Tuition: 51296
Name: Bostgia Institute of Technology City: Atlanta State: GA Rank: 34 Tuition: 32404
Name: Georgh University City: Bethlehem State: PA Rank: 44 Tuition: 48320
Name: Lehiersity of Chicago City: Chicago State: IL Rank: 3 Tuition: 52491
Name: Duke University City: Durham State: NC Rank: 8 Tuition: 51265
Name: University of Georgia City: Athens State: GA Rank: 56 Tuition: 29844
Name: University of Denver City: Denver State: CO Rank: 86 Tuition: 46362
Name: Loyola University Chicago City: Chicago State: IL Rank: 99 Tuition: 26270

I don't understand why the names get corrupted and why the sort doesn't work? I think it's because the modified list isn't being stored anywhere but I have no idea how to fix this. Any help would be appreciated. Thank You

4
  • Do you need to write your own sort or can you just use qsort? Commented Dec 2, 2020 at 2:05
  • This code assumes you have a fixed number of entries. Always use an argument to convey exactly what is going on. Commented Dec 2, 2020 at 2:05
  • Tip: Declare your iterator variables inside the scope in which they're used. Like for (int i = 0; ...) helps keep things contained. Commented Dec 2, 2020 at 2:06
  • @kashmafia Why are you putting a ; at the end of the block of inputData() function and printSortedUniversity() function? Commented Dec 2, 2020 at 2:14

3 Answers 3

2

You swap function should be

void swap(struct University *x, struct University *y) 
{ 
    struct University temp = *x; 
    *x = *y; 
    *y = temp; 
} 

This gives the expected output:

Name: University of Chicago City: Chicago Rank: 3 Tuition: 52491
Name: University of Virginia City: Charlottesville Rank: 24 Tuition: 52040
Name: Boston College City: Chestnut Hill Rank: 31 Tuition: 51296
Name: Duke University City: Durham Rank: 8 Tuition: 51265
Name: Lehigh University City: Bethlehem Rank: 44 Tuition: 48320
Name: University of Denver City: Denver Rank: 86 Tuition: 46362
Name: Princeton University City: Princeton Rank: 1 Tuition: 45320
Name: Georgia Institute of Technology City: Atlanta Rank: 34 Tuition: 32404
Name: University of Georgia City: Athens Rank: 56 Tuition: 29844
Name: Loyola University Chicago City: Chicago Rank: 99 Tuition: 26270

Note

This is an extremely inefficient sort. No one would do this in production code.

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

2 Comments

Selection sort at least have one positive, the low number of swaps.
It would be cheaper to swap pointers. Copying the whole struct is the real problem.
1

As David Cullen wrote about the swap() function in their answer, the swap() function to interchange structures according to tuition would be:

void swap(struct University *x, struct University *y) 
{ 
    struct University temp = *x; 
    *x = *y; 
    *y = temp; 
}

and your printSortedUniversity() function should be like this:

void printSortedUniversity(struct University* list){
    int i, j, max_idx;

      for (i = 0; i < 9; i++)
    {
        max_idx = i;
        for (j = i+1; j < 10; j++)
            if (list[j].tuition > list[max_idx].tuition)
                max_idx = j;
        if(i != max_idx)
            swap(&list[max_idx], &list[i]);
    }

    for (i=0; i<10; i++){
        printInfo(list[i]);
        // printf("%d\n",list[i].tuition);
        printf("\n");
    }
}

2 Comments

Don't you miss some {} or your indention is wrong?
@Surt Sorry, It is the indention.
0

This is what worked for me:

Used this swap function:

void swap(struct University *p, struct University *q)  //function to swap list items
{ 
    struct University hold = *p; 
    *p = *q; 
    *q = hold; 
}

And I didn't change the printSortedUniversity function:

void printSortedUniversity(struct University* list){ int i, j, max_idx;

  for (i = 0; i < 9; i++) 
{ 
    max_idx = i; 
    for (j = i+1; j < 10; j++) 
      if (list[j].tuition > list[max_idx].tuition) 
        max_idx = j; 

    swap(&list[max_idx], &list[i]); 
} 

for (i=0; i<10; i++){
    printInfo(list[i]);
    printf("\n");
}

};

1 Comment

Now you just need to accept one of the answers.

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.