Advertisement
ptrawt

259201 Lab11

Nov 4th, 2014
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <iostream>         //for using standard i/o functions
  2. #include <cmath>            //for using math functions
  3. #include <cstdlib>          //for using rand functions
  4. #include <ctime>            //for using a time function
  5.  
  6. using namespace std;
  7.  
  8. #define n 10                //declaring n is 10
  9.  
  10. /* Finding Average Functions
  11. Input: n scores
  12. Output: An average score*/
  13. float favg (int score[n])
  14. {
  15.     float sum = 0;                    //sum of scores
  16.    
  17.     for ( int i = 0 ; i < n ; i++ )
  18.     {
  19.         sum += score[i];
  20.     }
  21.    
  22.     return sum/n;
  23. }
  24.  
  25. /* Finding SD Functions
  26. Input: An average score and scores
  27. Output: A SD*/
  28. float fsd (float avg,int score[n])
  29. {
  30.     float sum = 0;                    //sum of scores minus average score
  31.    
  32.     for ( int i = 0 ; i < n ; i++ )
  33.     {
  34.         sum += pow((score[i] - avg),2);
  35.     }
  36.    
  37.     return sqrt(sum/n);
  38. }
  39.  
  40. /* Finding Grade From Score
  41. Input: A score and multipliers
  42. Output: A grade */
  43. char fgrade (int score, float mul[3], float avg, float sd)
  44. {
  45.     char grade;
  46.    
  47.     if( score >= avg+(mul[0]*sd) && score <= 10 )
  48.     {
  49.         grade = 'A';
  50.     }
  51.     else if ( score >= avg+(mul[1]*sd) )
  52.     {
  53.         grade = 'B';
  54.     }
  55.     else if ( score >= avg+(mul[2]*sd) )
  56.     {
  57.         grade = 'C';
  58.     }
  59.     else
  60.     {
  61.         grade = 'F';
  62.     }
  63.    
  64.     return grade;
  65. }
  66.  
  67. int main ()
  68. {
  69.     char lc;                                //declaring a loop control
  70.     float mul[] = { 1.2 , 0.6 , -1 };        //declaring default multipliers
  71.     do
  72.     {
  73.         int score[n];
  74.         char grade[n];
  75.         int gc[] = {0,0,0,0};               //declaring grade counters that gc[0] is an A counter , so gc[3] is an F counter
  76.        
  77.         cout<<"--------------------------------------"<<endl<<"Score(grade): ";
  78.        
  79.         srand(time(NULL));                      //gen a seed using current time
  80.         for ( int j = 0 ; j < n ; j++)
  81.         {
  82.             score[j] = rand() % (n+1);          //declaring random scores that is between 0 - n
  83.         }
  84.        
  85.         float avg = favg(score);
  86.         float sd = fsd(avg,score);
  87.        
  88.         for( int i = 0 ; i < n ; i ++)
  89.         {
  90.             grade[i] = fgrade(score[i],mul,avg,sd); //finding grade
  91.             cout<<score[i]<<"("<<grade[i]<<"),";
  92.             switch(grade[i])
  93.             {
  94.                 case 'A':
  95.                     gc[0]++;
  96.                     break;
  97.                 case 'B':
  98.                     gc[1]++;
  99.                     break;
  100.                 case 'C':
  101.                     gc[2]++;
  102.                     break;
  103.                 default:
  104.                     gc[3]++;
  105.             }
  106.         }
  107.        
  108.         cout<<endl<<"Grade count: A("<<gc[0]<<"), B("<<gc[1]<<"), C("<<gc[2]<<"), F("<<gc[3]<<")"<<endl;
  109.         cout<<"GPA: "<<((gc[0]*4)+(gc[1]*3)+(gc[2]*2))/n<<",\tAverage: "<<avg<<",\tSD: "<<sd<<endl;
  110.         cout<<"Do you want to modify multipliers (y/n): ";
  111.         cin>>lc;
  112.        
  113.         if( lc == 'y' || lc == 'Y' )
  114.         {
  115.             cout<<"Enter new multipliers (default: 1.2 0.6 -1 ): ";
  116.             cin>>mul[0]>>mul[1]>>mul[2];
  117.         }
  118.        
  119.         cout<<endl;
  120.        
  121.     }   while( lc != 'n' || lc != 'N' );
  122.    
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement