Advertisement
Syndragonic

Question 8 Student Histograms CS311

Sep 10th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<iomanip>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8. //The program should input each student's grade as an integer and store the grade in a vector.
  9.  
  10. int value = 0;
  11. vector<int> grades;
  12. cout <<"Enter grades separated by white spaces (ending with a negative number): ";
  13. cin >> value;
  14.  
  15. // Grades should be entered until the user enters a negative number for a grade.
  16.  
  17. while(value!=-1)
  18. {
  19. grades.push_back(value);
  20. cin >> value;
  21. }
  22. cout << endl;
  23.  
  24. //In computing the histogram, the minimum value of a grade is 0,
  25. int max = 0;
  26.  
  27. //but your program should determine the maximum value entered by the user.
  28. for(int i=0; i<grades[i]; i++){
  29.     if(grades[i] > max) max = grades[i];
  30. }
  31.  
  32. //Use a dynamic array to store the histogram.
  33. int* array= new int[max+1];
  34.  
  35. //The program should then scan through the vector and compute the histogram.
  36. for(int i=0; i< grades[i] ; i++)
  37. {
  38. array[grades[i]]++;
  39. }
  40. //Output the histogram to the console. Use top-down design
  41.  
  42. for(int i=0; i<=max; i++){
  43. if(array[i]!=0)
  44. cout <<"Number of " << i <<"'s:\t" << array[i] << endl;
  45. }
  46. return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement