Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Assignment 3 Ex. 2 (Player Stats) */
- #include <iostream>
- using namespace std;
- struct TouchdownStats { // struct with stats data
- int min;
- int max;
- int mode;
- int mean;
- int modeFreq;
- int total;
- };
- void maxValue(int touchdowns[], int size, TouchdownStats &player);
- void minValue(int touchdowns[], int size, TouchdownStats &player);
- void modeFreq(int touchdowns[], int size, TouchdownStats &player);
- void totalMean(int touchdowns[], int size, TouchdownStats &player); // calculates total and mean values
- void print(TouchdownStats &player); // outputs all stats
- int main() {
- TouchdownStats player;
- int size;
- do {
- cout << "Enter the size of the data array: ";
- cin >> size;
- } while (size <= 0); // to check the size of the array
- int *touchdowns = new int[size]; // dynamically allocate array
- cout << "Enter touchdowns stats one by one: " << endl;
- for (int i = 0; i < size; i++) { // saving entries into array
- cin >> touchdowns[i];
- }
- cout << endl;
- maxValue(touchdowns, size, player);
- minValue(touchdowns, size, player);
- modeFreq(touchdowns, size, player);
- totalMean(touchdowns, size, player);
- print(player);
- } // main
- void maxValue(int touchdowns[], int size, TouchdownStats &player) {
- int max = touchdowns[0];
- for (int i = 0; i < size; i++) {
- if (touchdowns[i] > max)
- max = touchdowns[i];
- }
- player.max = max;
- }
- void minValue(int touchdowns[], int size, TouchdownStats &player) {
- int min = touchdowns[0];
- for (int i = 0; i < size; i++) {
- if (touchdowns[i] < min)
- min = touchdowns[i];
- }
- player.min = min;
- }
- void modeFreq(int touchdowns[], int size, TouchdownStats &player) {
- int mode = touchdowns[0];
- int modeFreq = 1; // default frequency value
- for (int i = 0; i < size; i++) { // walks the array to compare values and to find repetitions
- int freq = 0;
- for (int j = 0; j < size; j++) {
- if (touchdowns[i] == touchdowns[j]) {
- freq++;
- }
- }
- if (modeFreq < freq) {
- mode = touchdowns[i];
- modeFreq = freq;
- }
- }
- player.mode = mode;
- player.modeFreq = modeFreq;
- }
- void totalMean(int touchdowns[], int size, TouchdownStats &player) { // calculate total and mean values
- int total = 0;
- for (int i = 0; i < size; i++) {
- total += touchdowns[i];
- }
- player.total = total;
- player.mean = total / size;
- }
- void print(TouchdownStats &player) { // output all values
- cout << "Max value is " << player.max << endl;
- cout << "Min value is " << player.min << endl;
- cout << "Mode is " << player.mode << endl;
- cout << "Mode Freq is " << player.modeFreq << endl;
- cout << "Mean: " << player.mean << endl;
- cout << "Total: " << player.total << endl;
- }
- /* ----- Output ----- */
- /* Enter the size of the data array: 13
- Enter touchdowns stats one by one:
- 29
- 21
- 32
- 28
- 26
- 21
- 17
- 26
- 17
- 32
- 18
- 17
- 17
- Max value is 32
- Min value is 17
- Mode is 17
- Mode Freq is 4
- Mean: 23
- Total: 301
- ----- Edge case -----
- Enter the size of the data array: 0
- Enter the size of the data array: -1
- Enter the size of the data array: 4
- Enter touchdowns stats one by one:
- 15
- 23
- 18
- 15
- Max value is 23
- Min value is 15
- Mode is 15
- Mode Freq is 2
- Mean: 17
- Total: 71
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement