Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. //Start Function Declarations
  9.     //Arrays for GPAs and student names and number of students
  10.     //promps user for name and gpa for that name
  11.     //if invald GPA, will error message and reprompt user
  12. void obtainNamesAndGPAs (string[], double[], int nstud);
  13.     // Param string - student names
  14.     // param double - student GPA
  15.     // param int - student number
  16. void sortGPA(string[], double[], int nstud);
  17.     //Sort GPAs with corresponding names in ASCENDING ORDER.
  18. void displayMinMaxGPA(string[], double[], int nstud);
  19.     //display of lowest and highest GPAs with corresponding names
  20. int calcAvgGPA(string[], double[], int nstud);
  21.     //calculates the average GPA from the GPAs entered in the array
  22. //End Function Declarations
  23.  
  24. int main () {
  25. //variable
  26.     int nstud;
  27.    
  28.     //promps user number of students
  29.     cout << "Enter the number of students: ";
  30.     cin >> nstud;
  31.    
  32.     //Arrays for GPA and Names
  33.     string* nameList;
  34.     nameList = new string [nstud];
  35.    
  36.     double* gradeList;
  37.     gradeList = new double [nstud];
  38.    
  39.     //calling functions
  40.     obtainNamesAndGPAs (nameList, gradeList, nstud);
  41.     sortGPA(nameList, gradeList, nstud);
  42.     displayMinMaxGPA (nameList, gradeList, nstud);
  43.     calcAvgGPA (nameList, gradeList, nstud);
  44.    
  45.    
  46.     return 0;
  47. }
  48.  
  49. //Start Function Defenitions
  50.  
  51.  
  52. //promps user for name and gpa for that name
  53. //Arrays for GPAs and student names and number of students
  54. //if invald GPA, will error message and reprompt user
  55. void obtainNamesAndGPAs (string nameList[], double gradeList[], int nstud) {
  56.    
  57.     //Prompt user for Student name
  58.     for (int i = 0; i < nstud; i++) {
  59.         cout << "Enter student name: ";
  60.         cin >> nameList[i];
  61.        
  62.         //Prompt user for Students GPA
  63.         cout << "Enter their GPA: ";
  64.         cin >> gradeList[i];
  65.  
  66.         if (gradeList[i] < 0 || gradeList[i] > 4) {
  67.                 cout << "Invalid GPA - re-enter: ";
  68.                 cin >> gradeList[i];
  69.             }
  70.     }
  71.    
  72. }
  73.  
  74.  
  75.  
  76. //Sort GPAs with corresponding names in ASCENDING ORDER.
  77. void sortGPA(string nameList[], double gradeList[], int nstud) {
  78.    
  79.    
  80.     //bubblesort
  81.  //   for (int i = 0; i<nstud; i++) {
  82. //      for (int j = 0, exchange = 0;j<nstud;j++) {
  83.  //           if (*gradeList[j] > *gradeList[j + 1]) {
  84.   //              temp1 = *gradeList[j];
  85.   //              *gradeList[j] = *gradeList[j + 1];
  86.  //               *gradeList[j + 1] = temp1;
  87.  //               swap(*nameList[j], *nameList[j + 1]);
  88.  //               exchange++;
  89.   //          }
  90.    
  91.         for (int a = 0; a<nstud; a++) {
  92.             cout << nameList[a] << " " << gradeList[a];
  93.         }
  94.    
  95. }
  96.  
  97.  
  98.  
  99. //display of lowest and highest GPAs with corresponding names
  100. void displayMinMaxGPA (string nameList[], double gradeList[], int nstud) {
  101.    
  102.  
  103.     }
  104.  
  105. //calculates the average GPA from the GPAs entered in the array
  106. int calcAvgGPA(string nameList[], double gradeList[], int nstud) {
  107.     double sum=0.0, avg;
  108.     for(int i=0;i<nstud;i++) {
  109.         sum=sum + gradeList[i];
  110.     }
  111.     avg = sum / nstud;
  112.    
  113.     return avg;
  114. }
  115.  
  116. //End Function Defentitions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement