Thread: Exam Scores Program Problems

1. Exam Scores Program Problems

Hey guys, I've been assigned this program for homework and I've had some trouble getting it to run correctly. I've included the instructions as well as the code I have come up with. It seems to be working correctly other than printing out each students average for their exams. If you could take a look I would greatly appreciate it. Thanks

Code:
```This assignment requires you to use arrays, multiple functions, multiple files and both .c and .h files. The program logic is not as complicated as previous program(s), but the syntax can be. So, the main goal of this assignment is for you to show that you have mastered the syntax of functions and arrays.

Your program will read in a number of student grade records from a file (stdin), with each grade record including (potentially) multiple exam scores. So, for example, you might read data for 200 students that include scores for each of 4 different exams. The exact number of students and exams will be specified in a constants.h file that I will provide. For example, the constants.h file might look like

#define NUMBERofSTUDENTS 200
#define EXAMSCORES 4

Your program would then read the 800 exam scores and store them in a two-dimensional array, declared as

int scores [NUMBERofSTUDENTS][EXAMSCORES];

I don't care what variable name you use for your array. I used scores here, but that is up to you. However, you WILL need to use the names NUMBERofSTUDENTS and EXAMSCORES as those will be names I'll use in constants.h.

Here is what your program will do:

Calculate and print the "class" average for each set of exams (So, for our "example" of four exam scores for each student, you'd print 4 different class averages, one for each exam.)
Calculate and print the median exam score for each set of exams.
Print a histogram of the scores found for each set of exams.
Calculate and print the average of all exam scores for each student record (So, again for our example, the average of the 4 exams for each student.)
Calculate and print the average of the averages of the students (so the average for all 800 exam scores in our example.)
To do some of these things, you'll need to sort the individual (4?) exam scores, for example to find the median and to print the histogram. You can use the bubble sort function that I've written for class. You can even use the histogram function that I've written for class. Bear two things in mind, however, namely

Both the bubble and histogram function(s) are written to work on a single-dimension array. You'll have to determine how to pass a one-dimensional array for each group of exam scores.
You cannot change the order of the exam scores in any column of your two-dimensional array since that would have the effect of changing the scores (and averages) for each student.
Luckily, each of those concerns can be addressed easily using techniques we'll discuss in class.

Submission Requirements:

You code should follow consistent indentation, use well-chosen variable names, and be written so as to be easily read.
Your sort routine(s) (I recommend bubble sort) must be in a separate .c file from the rest of your program.
Your histogram routine(s) must be in a separate .c file from the rest of your program.
You will be graded upon your modular design of this assignment so, I expect that you'll have separate functions for computing average, for finding the median, for reading the data, etc.
You should NOT include printf prompt statements in your code as you will be reading data from a file, using Linux redirection (<).
Submit (turn in) all of your files, both .c and .h including your own version of constants.h. (Yes, I'll be supplying my own, but I want to see yours to make sure that what you submit runs on its own.) I've set the moodle page to accept up to 20 different files. That should be plenty. If you find yourself wanting to submit more than 20, you've like got design flaw(s).
In addition, submit a sample data file that demonstrates that your program works correctly.```

This is the "bubblesort" I used

Code:
```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;
}
}
}
}```

Heres the histogram i used

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

extern void bubbleSort(int[], int);

void generateHistogram(int a[], int n)
{
int i,
min,
max,
counters[150];

bubbleSort(a,n);
//    assert(a[n-1] < 150);
min = a[0];
max = a[n-1];

// zero counters array
for(i = 0; i < 150; i++)
{
counters[i] = 0;
}

for(i = 0; i < n; i++)
{
counters[a[i]]++;    // add one to counter for that score.
}

for(i = min; i <= max; i++)
{
int j;
printf("%d\t",i);
for(j = 0; j < counters[i]; j++)
{
printf("*");
}
printf("\n");
}
}```

Heres the Main program I came up with

Code:
```#include <stdio.h>
#include <stdlib.h>
#include "constants.h"

int findMedian(int[], int);
double average(int[], int);

int main()
{
extern void bubbleSort(int[], int);
extern void generateHistogram(int[], int);
int i,j;
double sum=0;
int eachExam[NUMBERofSTUDENTS];
int eachStudent[EXAMSCORES];
int grades[NUMBERofSTUDENTS][EXAMSCORES];

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

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

for(j=0;j<EXAMSCORES;j++)
{
for(i=0;i<NUMBERofSTUDENTS;i++)
{
eachExam[i]=grades[i][j];
}
printf("For exam %d, the avg is %.3f. \n",j,average(eachExam,NUMBERofSTUDENTS));
bubbleSort(eachExam, NUMBERofSTUDENTS);
printf("For exam %d, the median is %d . \n",j,findMedian(eachExam,NUMBERofSTUDENTS));
generateHistogram(eachExam, NUMBERofSTUDENTS);
}

for(i=0; i<NUMBERofSTUDENTS; i++)
{
for(j=0;j<EXAMSCORES;j++)
{
eachStudent[j]=grades[i][j];
}
printf("For student %d, the avg is %d\n", i,average(eachStudent,EXAMSCORES));
}

for(i=0; i<NUMBERofSTUDENTS; i++)
{
for(j=0;j<EXAMSCORES;j++)
{
sum+=grades[i][j];
}
}
printf("For all exams, the avg is %f\n",sum/(NUMBERofSTUDENTS*EXAMSCORES));

return 0;

}

double average(int a[], int size)
{
double sum=0;
int i;
for(i=0;i<size;i++)
{
sum+=a[i];
}
return sum/size;
}

int findMedian(int A[], int num)
{
return A[(num-1)/2];
}```
I believe each students seperate average isn't working because i was trying to put a two dimensional array into a one dimensional array but I am not positive.

2. Did you compile this? Any warnings or errors?

3. Yes it does compile without any errors. When it prints it seems to print the histogram fine, but it prints out "0" for each students individual averages

4. Ok so i changed the %d to %f for the average that that fixed it.

{
for(j=0;j<EXAMSCORES;j++)
{
eachStudent[j]=grades[i][j];
}
printf("For student %d, the avg is %d\n", i,average(eachStudent,EXAMSCORES));
}

5. Ok that makes sense. Any other issues?

Also, generally you want to move your extern declarations above main.

6. Ok thanks I moved those externs above main;

And yea the only other problem i'm having is that it is not
printing out the overall average for the last exam score.

For example: If I make it so that there are 4 exams in the
constants.h file it will only print the overall average and median
for the first three exams. For some reason it stops before it gets
to the last one

7. Ok just found out I had the counter initialized as 0 so it was starting with a test 0 instead of 1. So that was the problem

Popular pages Recent additions