1. ## Grading Program using multidimensional arrays

Hi,

I am working on a program to read in a multidimensional array of the number of students in a class and the number of exams which I have written as:
int scores [NUMBERofSTUDENTS][EXAMSCORES];

what I am trying to do is calculate the class average for each exam, the median of all the scores, print the histogram, calculate the average of each student, and the average of the class. I have both bubble sort and the histogram functions written, but only for one-dimentional arrays. How can I modify them to work for this program?

I also have already written the main program, but only for one-dimensional arrays too. So how can I change it to handle multi-dimentional arrays?

I have to do this with multiple files so bubble sort, histogram, and the main are all separate.

Basically my problem is that I cannot figure out how to pass multidimensional arrays as single dimensional arrays. Please Help me if you can. I can supply you with greater detail if needed.
I have to have this completed by Wednesday so any help is greatly appreciated.

2. needed

3. There's no problem sending a 2D array to a function. I believe however, it would be easier to just use an a struct defined globally (above any function), and say each student had 3 test scores:

Code:
```#include <stdio.h>

#define MAX 50
struct student {
int ID_num;
int score1;
int score2;
int score3;
//int avgScore?
}stud;

int main(void) {
struct student studs[MAX];```
You can add as many scores as you need, and studs[] will still be a 1D array, which should work easily with your current code, just using the dot operator:
Code:
```for(i=0;i<MAX;i++) {
sum+=studs[i].score1, etc.
sum+=studs[i].score2, etc.
//etc.
}```
Time is short, if you want good help, you have to post your code, and stick around a bit to answer questions that may be raised by it.

Forums like this are good, but if you aren't available or can't give detailed answers, they can take a very long time, since people start losing interest in the thread.

So my advice is that you get on it, ASAP.

4. oh by the way, wad does += stands for ? my lecturer also talk about this too but i didnt get him~

5. Originally Posted by l2krence
variable = variable + some_number;

Is shortened in C to allow:

variable += some_number;

Same for all the other arithmetic operators:
variable *= 4;
variable /= 3;
variable -= 5;

6. ok i think we have the same teacher I was working on one of the functions. which is to add all the test averages separately. like test one avegare, etc.

Here is what i have can some one tell me what im doing wrong?

Code:
```#include <stdio.h>
#define NUMBERofSTUDENTS 3
#define EXAMSCORES 4

double averagexam( const int [], int );

int main(void){

int exams;
{ {77,68,86,73},
{96,87,89,78},
{70,90,86,81}};
for(exams=0;exams<EXAMSCORES;exams++){
printf("the avg grade for exam %d is%.2f\n",
}

return 0;
}

double averagexam( const int setofexams[], int students)
{
int i;
int j;
int total=0;
for (i=0;i<students;i++){

total += setofexams[i];

}
return (double) total/students;
}```

7. Originally Posted by newbc
ok i think we have the same teacher I was working on one of the functions. which is to add all the test averages separately. like test one avegare, etc.

Here is what i have can some one tell me what im doing wrong?

Code:
```#include <stdio.h>
#define NUMBERofSTUDENTS 3
#define EXAMSCORES 4

double averagexam( const int [], int );

int main(void){

int exams;
{ {77,68,86,73},
{96,87,89,78},
{70,90,86,81}};
for(exams=0;exams<EXAMSCORES;exams++){
printf("the avg grade for exam %d is%.2f\n",
}

return 0;
}

double averagexam( const int setofexams[], int students)
{
int i;
int j;
int total=0;
for (i=0;i<students;i++){

total += setofexams[i];

}
return (double) total/students;
}```
Wrong.
Swap EXAMSCORES and NUMBERofSTUDENTS in your for-loop.

8. billy, which for loop? the one in the functions parameters or the one in main? hopefully you understood what i want to do, calculate the exam average's not students averages.

well it seems the loop in the function parameters only loops through rows only.
how will i loop through columns only, then?

9. well here an update on my code. I know im close, because it gives me the average for exam one which is 81, but it does it for all of them. I removed the inner loop because I thought it will change 0 everytime but i just confused me.

Code:
```#include <stdio.h>
#define NUMBERofSTUDENTS 3
#define EXAMSCORES 4

double averagexam( const int [][4], int );

int main(void){

int exams;
{ {77,68,86,73},
{96,87,89,78},
{70,90,86,81}};
for(exams=0;exams<EXAMSCORES;exams++){
printf("the avg grade for exam %d is%.2f\n",
}

return 0;
}

double averagexam( const int setofexams[][4], int students)
{
int i;
int j;
int total=0;
for (i=0;i<students;i++){
//for(j=0;j<4;j++){

total += setofexams[i][0];

//}
}
return (double) total/students;
}```

10. The averageexam function is only totaling the scores for exam 0 every time. You need to pass in the exam number into average exam, like so:
Code:
`double averagexam( const int setofexams[][4], int students, int exam_number)`

11. Originally Posted by anduril462
The averageexam function is only totaling the scores for exam 0 every time. You need to pass in the exam number into average exam, like so:
Code:
`double averagexam( const int setofexams[][4], int students, int exam_number)`
but do I need nested loops in the function? where should use int exam_number though?

12. but do I need nested loops in the function?
Nope. You only need to change averagexam to take the exam_number, pass in the exam number when you call it, and change the total += line.

where should use int exam_number though?

it gives me the average for exam one which is 81, but it does it for all of them
You are always calculating the average for exam one (array location 0). So, adjust your total += line to use the exam_number variable instead of 0.

That's it. Just a few very simple, short changes.

13. thanks for the help, i wasnt updating my function call.

now he wants us to do a buble sort in order to find the median of each set of exams, here is his example on how to sort them for one dimensional array.

Code:
```// ************************************************************************
//
//	bubble.c --- bubbleSort function, oddly enough does a bubble sort
//                   on an array of ints.
//
//
//
// ************************************************************************

void bubbleSort(int A[], int num)
{
int i,j;
int temp;

for(i = 0; i < num-1; i++)
{
for(j = 0; j < num-i-1; j++)
{
if( A[j] > A[j+1] )
{
// swap A[j], A[j+1]
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
}```
and here is my code.

Code:
```void bubbleSort(int A[][4], int students)
{
int i;
int temp;

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

if( A[i] > A[i+1] )
{
// swap A[i], A[i+1]
temp = A[i];
A[i] = A[i+1];
A[i+1] = temp;
}
}
}```

14. Your bubble sort left off one of the for loops. You will need two of them.

15. Ok im trying to call my bublesort function but it keeps saying im missing one argument. what does it mean?

Code:
```
#include <stdio.h>
#define NUMBERofSTUDENTS 3
#define EXAMSCORES 4

void bubblesort( int[][EXAMSCORES], int, int);

int main(void){

int exams;
int ex;
int studs;
{ {77,68,86,73},
{96,87,89,78},
{70,90,86,81}};

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

}

return 0;
}
// line 29

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-1;j++){

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

}
}
}

}```