Thread: sorting columns in 2d array and find avgse

1. sorting columns in 2d array and find avgse

hell I want to first sort my columns of the test grades entered. it should sort them like this

exam 0 exam 2 exam 3 exam4
70 68 86 73
77 87 86 78
96 90 89 81

but i get some weird sorting. I also want to find the average of all the total test /total number of students. I comment out the average for all because I kept getting an error.

well here is my code. and thanks for all the help.

Code:
```/* Code by
I don't think it compiles anymore it just too but I just make too many mistakes.
run on csp03 machine*/

#include <stdio.h>
//#include "constants.h"
#define NUMBERofSTUDENTS 3
#define EXAMSCORES 4

double averagexam( int [][EXAMSCORES], int, int );
double studentavg( int [], int);
void printarray(int grades[][EXAMSCORES], int , int);
//double averageall(const int[][EXAMSCORES], int,int );
void bubblesort( int[][EXAMSCORES], int, int);
//void initialScores(int[][EXAMSCORES]);

int main(void){

int exams;
int students;

int scores[NUMBERofSTUDENTS][EXAMSCORES]=
{{77,68,86,73},
{96,87,89,78},
{70,90,86,81}};

//initialScores(scores);

for(exams=0;exams<EXAMSCORES;exams++){
printf("the avg grade for exam %d is%.2f\n",
exams, averagexam(scores, NUMBERofSTUDENTS, exams));
}
for(students=0;students<NUMBERofSTUDENTS;students++){
printf("the avg for student %d is %.2f\n",
students, studentavg(scores[students], EXAMSCORES));
}

//printf("The avg for all the test/students is %d\n",
//	averageall(scores, NUMBERofSTUDENTS, EXAMSCORES));

for(exams=0;exams<EXAMSCORES;exams++){

bubblesort(scores, NUMBERofSTUDENTS, exams);
}

printarray(scores, NUMBERofSTUDENTS, EXAMSCORES);

return 0;
}

double averagexam(  int setofexams[][EXAMSCORES], int students, int
exams)
{
int i;
int total=0;

for (i=0;i<students;i++){

total += setofexams[i][exams];

}
return (double) total/students;
}

double studentavg( int grades[], int tests){

int i;
int total=0;

for(i=0;i<tests;i++){
}

return (double) total/tests;
}

/*double averageall(const grades[][EXAMSCORES], int rows, int colm){

int i;
int j;
int total;

for(i=0;i<rows;i++){
for(j=0;j<colm;j++){

}

}

return(double) total/(rows*colm);

}

void initialScores(int scores[NUMBERofSTUDENTS][EXAMSCORE]){

int i;
int j;

for (i=0;i<NUMBERofSTUDENTS;i++){
for (j=0;j<EXAMSCORES;j++){
scanf("%d", &scores[i][j]);
}
}
}
*/

void bubblesort( int a[][EXAMSCORES],int students, int exams){

int i;
int j;
int temp;

for (i=0;i<students;i++)
{

for(j=0;j<students-i-1;j++){

if(a[i][exams] > a[i][exams+1])
{
temp=a[i][exams];
a[i][exams]=a[i][exams+1];
a[i][exams+1]= temp;

}
}
}

}

void printarray(int grades[][EXAMSCORES], int pupils, int tests)
{

int i;
int j;

for(i=0;i<pupils;i++)
{
printf("\n");

for(j=0;j<tests;j++){
}
}
}```

2. it says hell I meant hello, sorry.

3. For the averages, I think it depends on what you need to look at. If you want to find the average score on a particular exam for the whole class, for example, you would add only those scores and divide by the number of students. The number of students is not row*col. It's the same story if you wanted the average final score and so forth.

To sort by column, I would just transpose the matrix and then sort on the first dimension. Normally the first dimension is constant and the second dimension moves around, but you can transpose to avoid working with that.

4. yeah the averages is for the whole class.

5. ok figured out bubble sort, thanks.

For some reason i can not find the whole class average.