Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. // Sean Anderson
  2. // CS 151, 1/22/2017
  3. // CH10, PC2
  4. /*
  5. Modify the program of Programming Challenge 1 to allow the user to enter name-score
  6. pairs. For each student taking a test, the user types a string representing the name of the
  7. student, followed by an integer representing the student's score. Modify both the sorting
  8. and average-calculating functions so they take arrays of structures, with each structure
  9. containing the name and score of a single student. In traversing the arrays, use pointers
  10. rather than array indices.
  11. */
  12.  
  13. #include <iostream>
  14. #include <string>
  15. using namespace std;
  16.  
  17. struct Student
  18. {
  19.     string name;
  20.     double grade;
  21. };
  22.  
  23. // Function Prototypes
  24. double average(Student *, int);
  25. void sort(Student*, int);
  26.  
  27. int main()
  28. {
  29.     int numberOfScores;
  30.     Student *testScores;     // Pointer to dynamically allocated array of scores
  31.     double ave;              // Average scores
  32.  
  33.                              // Determine number of scores and allocate array
  34.     cout << "Number of scores that you will input: ";
  35.     cin >> numberOfScores;
  36.     testScores = new Student[numberOfScores];
  37.  
  38.     // Get scores from the user
  39.     for (int count = 0; count < numberOfScores; count++)
  40.     {
  41.         cout << "Student #" << (count + 1) << endl;
  42.         cout << "    Name: ";
  43.         //cin >> testScores[count].name;
  44.         cin >> (testScores + count)->name;
  45.  
  46.         do {
  47.             cout << "    Score: ";
  48.             cin >> (testScores + count)->grade;
  49.  
  50.             if ((testScores + count)->grade < 0)
  51.             {
  52.                 cout << "Please only enter scores greater than 0.";
  53.             }
  54.         } while ((testScores + count)->grade < 0);
  55.     }
  56.  
  57.     // Echo scores back to user
  58.     cout << "\nThe test scores that you entered: ";
  59.     for (int count = 0; count < numberOfScores; count++)
  60.     {
  61.         cout << "\n Test #" << (count + 1) << ": " << (testScores + count)->name << ": " << (testScores + count)->grade;
  62.     }
  63.  
  64.     // Sort students based on their grade using a bubble sort function
  65.     sort(testScores, numberOfScores);
  66.  
  67.     // Print the sorted scores
  68.     cout << "\n\nThe test scores after being sorted: ";
  69.     for (int count = 0; count < numberOfScores; count++)
  70.     {
  71.         cout << " " << (testScores + count)->grade;
  72.     }
  73.  
  74.     // Computer average
  75.     ave = average(testScores, numberOfScores);
  76.  
  77.     // Print the output
  78.     cout << "\nThe average test score was: " << ave << endl;
  79.    
  80.     cout << "Press any key to exit.." << endl;
  81.     cin >> ave;
  82.  
  83.     // Delete the array
  84.     delete[] testScores;
  85.     testScores = 0;
  86.     return 0;
  87. }
  88.  
  89. double average(Student *testscores, int numberOfScores)
  90. {
  91.     double total = 0;
  92.  
  93.     for (int count = 0; count < numberOfScores; count++)
  94.     {
  95.         total += (testscores + count)->grade;
  96.     }
  97.  
  98.     return total / numberOfScores;
  99. }
  100.  
  101. //This is a simple bubble sort that sorts the students by their grades.
  102. void sort(Student *students, int nbrScores)
  103. {
  104.     Student temp;
  105.     for (int i = 0; i < nbrScores; i++)
  106.     {
  107.         for (int j = 0; j < nbrScores; j++)
  108.         {
  109.             if ((students + i)->grade > (students + j)->grade)
  110.             {
  111.                 temp = *(students + i);
  112.                 *(students + i) = *(students + j);
  113.                 *(students + j) = temp;
  114.             }
  115.         }
  116.     }
  117. }
  118.  
  119. struct Point {
  120.     double x, y;
  121.  
  122.     struct Point& operator+=(const Point& rhs)
  123.     {
  124.         x += rhs.x; y += rhs.y;
  125.         return *this;
  126.     }
  127.  
  128.     struct Point& operator+=(const double& k)
  129.     {
  130.         x += k; y += k;
  131.         return *this;
  132.     }
  133. };
  134.  
  135. Point operator+(Point lhs, const Point& rhs)
  136. {
  137.     return lhs += rhs;
  138. }
  139. Point operator+(Point lhs, const double k)
  140. {
  141.     return lhs += k;
  142. }
  143. Point operator+(const double k, Point rhs)
  144. {
  145.     return rhs += k;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement