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
qsort?for (int i = 0; ...)helps keep things contained.;at the end of the block ofinputData()function andprintSortedUniversity()function?