Advertisement
agoncharova

A3 Ex2

Mar 26th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. /* Assignment 3 Ex. 2 (Player Stats) */
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. struct TouchdownStats { // struct with stats data
  8.     int min;
  9.     int max;
  10.     int mode;
  11.     int mean;
  12.     int modeFreq;
  13.     int total;
  14. };
  15.  
  16. void maxValue(int touchdowns[], int size, TouchdownStats &player);
  17. void minValue(int touchdowns[], int size, TouchdownStats &player);
  18. void modeFreq(int touchdowns[], int size, TouchdownStats &player);
  19. void totalMean(int touchdowns[], int size, TouchdownStats &player); // calculates total and mean values
  20. void print(TouchdownStats &player); // outputs all stats
  21.  
  22. int main() {
  23.     TouchdownStats player;
  24.     int size;
  25.  
  26.     do {
  27.         cout << "Enter the size of the data array: ";
  28.         cin >> size;
  29.     } while (size <= 0); // to check the size of the array
  30.  
  31.     int *touchdowns = new int[size]; // dynamically allocate array
  32.     cout << "Enter touchdowns stats one by one: " << endl;
  33.     for (int i = 0; i < size; i++) { // saving entries into array
  34.         cin >> touchdowns[i];
  35.     }
  36.     cout << endl;
  37.  
  38.     maxValue(touchdowns, size, player);
  39.     minValue(touchdowns, size, player);
  40.     modeFreq(touchdowns, size, player);
  41.     totalMean(touchdowns, size, player);
  42.  
  43.     print(player);
  44. } // main
  45.  
  46. void maxValue(int touchdowns[], int size, TouchdownStats &player) {
  47.     int max = touchdowns[0];
  48.     for (int i = 0; i < size; i++) {
  49.         if (touchdowns[i] > max)
  50.             max = touchdowns[i];
  51.     }
  52.     player.max = max;
  53. }
  54.  
  55. void minValue(int touchdowns[], int size, TouchdownStats &player) {
  56.     int min = touchdowns[0];
  57.     for (int i = 0; i < size; i++) {
  58.         if (touchdowns[i] < min)
  59.             min = touchdowns[i];
  60.     }
  61.     player.min = min;
  62. }
  63.  
  64. void modeFreq(int touchdowns[], int size, TouchdownStats &player) {
  65.     int mode = touchdowns[0];
  66.     int modeFreq = 1; // default frequency value
  67.     for (int i = 0; i < size; i++) { // walks the array to compare values and to find repetitions
  68.         int freq = 0;
  69.         for (int j = 0; j < size; j++) {
  70.             if (touchdowns[i] == touchdowns[j]) {
  71.                 freq++;
  72.             }
  73.         }
  74.         if (modeFreq < freq) {
  75.             mode = touchdowns[i];
  76.             modeFreq = freq;
  77.         }
  78.     }
  79.     player.mode = mode;
  80.     player.modeFreq = modeFreq;
  81. }
  82.  
  83. void totalMean(int touchdowns[], int size, TouchdownStats &player) { // calculate total and mean values
  84.     int total = 0;
  85.     for (int i = 0; i < size; i++) {
  86.         total += touchdowns[i];
  87.     }
  88.     player.total = total;
  89.     player.mean = total / size;
  90. }
  91.  
  92. void print(TouchdownStats &player) { // output all values
  93.     cout << "Max value is " << player.max << endl;
  94.     cout << "Min value is " << player.min << endl;
  95.     cout << "Mode is " << player.mode << endl;
  96.     cout << "Mode Freq is " << player.modeFreq << endl;
  97.     cout << "Mean: " << player.mean << endl;
  98.     cout << "Total: " << player.total << endl;
  99. }
  100.  
  101.  
  102.  
  103.  
  104.  
  105. /* ----- Output ----- */
  106.  
  107. /* Enter the size of the data array: 13
  108. Enter touchdowns stats one by one:
  109. 29
  110. 21
  111. 32
  112. 28
  113. 26
  114. 21
  115. 17
  116. 26
  117. 17
  118. 32
  119. 18
  120. 17
  121. 17
  122.  
  123. Max value is 32
  124. Min value is 17
  125. Mode is 17
  126. Mode Freq is 4
  127. Mean: 23
  128. Total: 301
  129.  
  130.  
  131. ----- Edge case -----
  132. Enter the size of the data array: 0
  133. Enter the size of the data array: -1
  134. Enter the size of the data array: 4
  135. Enter touchdowns stats one by one:
  136. 15
  137. 23
  138. 18
  139. 15
  140.  
  141. Max value is 23
  142. Min value is 15
  143. Mode is 15
  144. Mode Freq is 2
  145. Mean: 17
  146. Total: 71
  147. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement