Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. // INCLUDE PROTOTYPES
  8. int* create_array(int);
  9. void enter_data(int*, int);
  10. float find_average(int*, int);
  11. void show_array(int*, int);
  12.  
  13. int main()
  14. {
  15. int *dyn_array;
  16. int students;
  17. float avrg;
  18.  
  19. do
  20. {
  21. cout << "How many students will you enter? ";
  22. cin >> students;
  23. }while ( students <= 0 );
  24.  
  25. dyn_array = create_array( students );
  26. //this function creates a dynamic array
  27. //and returns its pointer
  28. enter_data( dyn_array, students );
  29. //use 'pointer' notation in this function to
  30. //access array elements
  31. //accept only numbers 0-100 for movie seen
  32. avrg = find_average( dyn_array, students );
  33. //use 'pointer' notation in this function to
  34. //access array elements
  35.  
  36.  
  37. cout << "The array is:" << endl << endl;
  38. show_array( dyn_array, students);
  39.  
  40. cout << endl;
  41. cout << "The average is " << avrg << ".\n";
  42.  
  43. delete [] dyn_array;
  44. return 0;
  45. }
  46.  
  47. int* create_array( f_students )
  48. {
  49. int* f_array = new int[ f_students ];
  50. return f_array;
  51. }
  52.  
  53. void enter_data(int* f_dyn_array, int f_students)
  54. {
  55. for(int i = 0; i < f_students; i++)
  56. {
  57. cout << "How many movies did student" << i + 1 << "watch?: ";
  58. cin >> f_dyn_array[i];
  59. }
  60. while(f_dyn_array[i] < 0 || f_dyn_array[i] > 100)
  61. {
  62. cout << "Please enter a number between 0 and 100!";
  63. cin >> f_dyn_array[i];
  64. }
  65. }
  66.  
  67. float find_average(int* f_dyn_array, int f_students)
  68. {
  69. int sum = 0;
  70. float avrg;
  71.  
  72. for(int i = 0; i < f_students; i++)
  73. {
  74. sum = sum + f_dyn_array[i];
  75. }
  76.  
  77. avrg = sum/f_students;
  78.  
  79. return avrg;
  80. }
  81.  
  82. void show_array(int* f_dyn_array, int f_students)
  83. {
  84. cout << "Student Movies" << "\n";
  85.  
  86. for(int i = 0; i < f_students; i++)
  87. {
  88. cout << i + 1 << " " << f_dyn_array[i] << "/n";
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement