1. ## Sorting struct

Hello,

I am pretty new to all this so bear with me. I need to sort these students based on their test scores.

My biggest problem is with my sort function. I am not sure of the syntax to call the items the proper way. I also don't know how to call the function properly as I know that i can't pass a int to a struct. Any suggestions would be helpful.

Also not sure about the z malloc. Just threw that one in there.

Code:
```struct student{
int id;
int score;
};

void sort(struct student* students, int n){
/*Sort the n students based on their score*/

int idx = 0;

for (int i = 0; i < n; i++)
{ idx = i;
for (int j = i; j < n; j++)
{
if (students[j] < students[idx]){
idx = j;
}
}
int min;
min = students[idx];
students[idx] = students[i];
students[i] = min;
}
}

int main(){
/*Declare an integer n and assign it a value.*/
int n = 12;

/*Allocate memory for n students using malloc.*/
int *m = malloc(sizeof(int)*n);
int *z = malloc(sizeof(int)*n);

/*Generate random IDs and scores for the n students, using rand().*/

for (int i = 0; i < n; i++)
{
m[i] = rand() % 10 + 1;

}
for (int j = 0; j < n; j++)
{
z[j] = rand() % 101;

}

/*Print the contents of the array of n students.*/
printf("IDs: ");
for (int i = 0; i < n; i++)
{
printf("%d, ", m[i]);
}
printf("\nScores: ");
for (int j = 0; j < n; j++)
{
printf("%d, ", z[j]);
}

/*Pass this array along with n to the sort() function*/
sort(z, n);

/*Print the contents of the array of n students.*/
printf("\n\nSorted:\n\nIDs: ");
for (int i = 0; i < n; i++)
{
printf("%d, ", m[i]);
}
printf("\nScores: ");
for (int j = 0; j < n; j++)
{
printf("%d, ", z[j]);
}
free(m);
free(z);
return 0;
}``` 2. Let's say you have two struct students, x and y. On what basis would you say x was greater than y?

You need to answer that question unambiguously, since - inside your sort() function - you're obviously expecting the test "students[j] < students[idx]" (where students is an array of struct student) to mean something to a C compiler. It does not.

Similarly, in the sort() function, what do you expect the assignment "min = students[idx];" (where min is of type int) to do? 3. using your logic, what would occur if the student ID a struct is the highest number yet the student has the lowest score. ? 4. You all raise very good points. I've scrapped my sort and z. Back to the drawing board 5. Figuring out a working sort algorithm is one issue. Work on that for simple number arrays.
Then when handling structs you need to flip entire struct records instead of just an int. Or at least flip each struct element. 6. Before you can sort the structs, you need to use the struct member (score in this case), operator. In C, that is the dot operator: .

Now your comparisons become like this, to compare (for example), two adjacent students scores: student[i].score > student[i+1].score. Note where the index is placed (before the dot operator), and where the name of the struct member is placed (after the dot operator).

Please post in plain text, instead of using the colors and other formatting style from your computer. It's hard to read your colors, and it prevents the forum software from adding in line numbering, and other formatting styles it has especially for code. Popular pages Recent additions malloc, sort, sorting, struct, struct help 