- #include <stdio.h>
- #include <stdlib.h>
- /* David Hurng
- 09/26/12
- hw#4
- the functions topscore will compute the totals and return the highest test scores
- the toughesthomework will compute the averages and return the homework with the lowest scores
- the numhighest will return the number of hw that have the highest grade
- */
- #define NROW 100
- #define NCOL 20
- /*int hws[NROW][NCOL]; change to*/ extern int hws[NROW][NCOL];
- int totals[NROW]; //totals[i] is the total sum of all student i’s homeworks
- double averages [NCOL];
- //return the id number of the student with the top score
- int topscore() //compute totals and returns the id of that student
- {
- int i;
- int j;
- int hwSum = 0;
- int highestScore = 0;
- int studentID;
- for(i = 0; i <= NROW - 1; i++)
- {
- for(j = 0; j <= NCOL - 1; j++)
- {
- hwSum = hwSum + hws[i][j];
- }
- }
- for(i = 0; i <= NROW - 1; i++)
- {
- totals[i] = hwSum;//this time all the sums are in place
- if(highestScore < totals[i])//compare which is largest
- {
- studentID = i;
- }
- }
- //printf("%d", hwSum);
- return studentID;
- }
- //return the homework that has the lowest average score
- int toughesthomework() //compute averages and how often highest number appears
- {
- int i;
- int j;
- int studentID;
- double hwSum = 0; //working with averages
- float average;
- for(j = 0; j <= NCOL - 1; j++)//checks through all the homeworks rather than students
- {
- for(i = 0; i <= NROW - 1; i++)
- {
- hwSum = hwSum + hws[i][j];
- }
- averages[j] = hwSum/NROW;//average of the number of students
- }
- average = averages[0];
- for(j = 0; j <= NCOL - 1; j++)
- {
- if(average > averages[j])
- {
- average = averages[j];
- studentID = j;
- }
- }
- //printf("%d", average);
- return studentID;
- }
- /*return the number of homeworks that have the highest grade
- for example, if the highest grade is 15,
- and two students got 15 in HW1,
- and three students got 15 in HW 7,
- then the function should return 5*/
- int numhighest()
- {
- int i;
- int j;
- int highestScore = 0;
- int numberOfHighest = 0;
- for(i = 0; i <= NROW - 1; i++)
- {
- for(j = 0; j <= NCOL - 1; j++)
- {
- if(highestScore < hws[i][j])//check for the highest
- {
- highestScore = hws[i][j];//changes to the highest
- }
- }
- }
- for(i = 0; i <= NROW - 1; i++)
- {
- for(j = 0; j <= NCOL - 1; j++)
- {
- if(hws[i][j] == highestScore)
- {
- numberOfHighest++;//keep count of the number of times it will show
- }
- }
- }
- return numberOfHighest;
- }
- /*main() //use rand() from lib
- {
- int i;
- int j;
- for(i = 0; i <= NROW; i++)
- {
- for(j = 0; j <= NCOL; j++)
- {
- hws[i][j] = rand()%5;
- }
- }
- printf("&d", totals);
- printf("%d", averages);
- }*/
- /*
- int main() {
- int i;
- int j;
- //printf(" starting");
- for(i = 0; i<100; i++){
- printf(" %2d", i);
- for(j = 0; j<20; j++){
- hws[i][j] = rand()%101;
- printf(" %d", hws[i][j]);
- }
- printf("\n");
- }
- printf("%d", numhighest());
- }
- */