Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 26th, 2012  |  syntax: None  |  size: 3.42 KB  |  hits: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  #include <stdio.h>
  2. #include <stdlib.h>
  3. /* David Hurng
  4. 09/26/12
  5. hw#4
  6. the functions topscore will compute the totals and return the highest test scores
  7. the toughesthomework will compute the averages and return the homework with the lowest scores
  8. the numhighest will return the number of hw that have the highest grade
  9. */
  10. #define NROW 100
  11. #define NCOL 20
  12. /*int hws[NROW][NCOL]; change to*/ extern int hws[NROW][NCOL];
  13. int totals[NROW]; //totals[i] is the total sum of all student i’s homeworks
  14. double averages [NCOL];
  15. //return the id number of the student with the top score
  16. int topscore()  //compute totals and returns the id of that student
  17. {
  18.     int i;
  19.     int j;
  20.     int hwSum = 0;
  21.     int highestScore = 0;
  22.     int studentID;
  23.     for(i = 0; i <= NROW - 1; i++)
  24.     {
  25.         for(j = 0; j <= NCOL - 1; j++)
  26.         {
  27.            hwSum = hwSum + hws[i][j];
  28.         }
  29.     }
  30.     for(i = 0; i <= NROW - 1; i++)
  31.     {
  32.         totals[i] = hwSum;//this time all the sums are in place
  33.         if(highestScore < totals[i])//compare which is largest
  34.         {
  35.             studentID = i;
  36.         }
  37.     }
  38.     //printf("%d", hwSum);
  39.     return studentID;
  40. }
  41. //return the homework that has the lowest average score
  42. int toughesthomework() //compute averages and how often highest number appears
  43. {
  44.     int i;
  45.     int j;
  46.     int studentID;
  47.     double hwSum = 0; //working with averages
  48.     float average;
  49.     for(j = 0; j <= NCOL - 1;  j++)//checks through all the homeworks rather than students
  50.     {
  51.         for(i = 0; i <= NROW - 1; i++)
  52.         {
  53.             hwSum = hwSum + hws[i][j];
  54.         }
  55.         averages[j] = hwSum/NROW;//average of the number of students
  56.     }
  57.     average = averages[0];
  58.     for(j = 0; j <= NCOL - 1;  j++)
  59.     {
  60.         if(average > averages[j])
  61.         {
  62.             average = averages[j];
  63.             studentID = j;
  64.         }
  65.     }
  66.     //printf("%d", average);
  67.     return studentID;
  68. }
  69. /*return the number of homeworks that have the highest grade
  70. for example, if the highest grade is 15,
  71. and two students got 15 in HW1,
  72. and three students got 15 in HW 7,
  73. then the function should return 5*/
  74. int numhighest()
  75. {
  76.     int i;
  77.     int j;
  78.     int highestScore = 0;
  79.     int numberOfHighest = 0;
  80.     for(i = 0; i <= NROW - 1; i++)
  81.     {
  82.         for(j = 0; j <= NCOL - 1;  j++)
  83.         {
  84.            if(highestScore < hws[i][j])//check for the highest
  85.            {
  86.               highestScore = hws[i][j];//changes to the highest
  87.            }
  88.         }
  89.     }
  90.     for(i = 0; i <= NROW - 1; i++)
  91.     {
  92.         for(j = 0; j <= NCOL - 1;  j++)
  93.         {
  94.             if(hws[i][j] == highestScore)
  95.             {
  96.                numberOfHighest++;//keep count of the number of times it will show
  97.             }
  98.         }
  99.     }
  100.     return numberOfHighest;
  101. }
  102.  
  103. /*main() //use rand() from lib
  104. {
  105.     int i;
  106.     int j;
  107.     for(i = 0; i <= NROW; i++)
  108.     {
  109.         for(j = 0; j <= NCOL; j++)
  110.         {
  111.             hws[i][j] = rand()%5;
  112.         }
  113.     }
  114.     printf("&d", totals);
  115.     printf("%d", averages);
  116.  
  117. }*/
  118. /*
  119. int main() {
  120.         int i;
  121.         int j;
  122. //printf(" starting");
  123.         for(i = 0; i<100; i++){
  124.                 printf(" %2d", i);
  125.                 for(j = 0; j<20; j++){
  126.                         hws[i][j] = rand()%101;
  127.                         printf(" %d", hws[i][j]);
  128.  
  129.                 }
  130.                 printf("\n");
  131.         }
  132. printf("%d", numhighest());
  133. }
  134. */