-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointers.cpp
More file actions
145 lines (110 loc) · 3.05 KB
/
pointers.cpp
File metadata and controls
145 lines (110 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
Jacob Privitt
CS-150-101
Lab 02
8/30/18
Pointers Lab
**/
/*
1. Lab- Program challenge Assignment Chapter 10 (30 pts.) - How many social media posts do students
make when perhaps they should be doing studying in one month? Ha! Ha! This program should calculate
and print out the following: ask number for number of students and how many posts they made, sort
those entries, average, median, and mode-(if no mode it should print out that there is no mode) of
the values entered. The program should ask the user how many students were surveyed and dynamically
allocate an array of that size. The program should then allow the user to enter the number of social
media posts each student has made. Use separate functions and name them: median, mode, getMovieData,
sort, average, and getNumber. Create functions that pass pointers. Use numerous documentation
comments to explain your code.
2. Chapter 10 Review Questions and Exercises: #2-38, even numbered items only (30 pts.)
*/
#include <iostream>
#include <iomanip>
using namespace std;
void getData(int *, int);
void selectSort(int *, int);
double average(int *, int);
double median(int *, int);
int mode(int *, int);
int main()
{
int SIZE, MOD;
double AVG, MED;
int *students;
cout << "How many students were surveyed?" << endl;
cin >> SIZE;
students = new int[SIZE];
getData(students, SIZE);
selectSort(students, SIZE);
AVG = average(students, SIZE);
MED = median(students, SIZE);
MOD = mode(students, SIZE);
cout << "Here are some social media stats:" << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Average: " << AVG << endl;
cout << "Median: " << MED << endl;
cout << "Mode: " << MOD << endl;
delete [] students;
students = 0;
return 0;
}
void getData(int *array, int size) {
for(int i = 0; i < size; i++) {
cout << "How many posts did student " << ( i + 1) << " make in a month?" << endl;
cin >> *(array + i);
}
}
void selectSort(int *array, int size)
{
int startScan, minIndex, minValue;
for (startScan =0; startScan< (size -1); startScan++)
{
minIndex = startScan;
minValue = *(array + startScan);
for(int index = startScan + 1; index < size; index++)
{
if (*(array + index) < minValue)
{
minValue = *(array + index);
minIndex = index;
}
}
*(array + minIndex) = *(array + startScan);
*(array + startScan) = minValue;
}
}
double average(int *array, int size) {
double sum = 0;
for (int i = 0; i < size; i++) {
sum += *(array + i);
}
return sum / size;
}
double median(int *array, int size) {
int Mid = (size - 1) / 2;
double Med;
if (size % 2 == 0)
{
Med = (*(array + Mid) + *(array + (Mid + 1))) / 2;
}
else
Med = *(array + Mid);
return Med;
}
int mode(int *array, int size) {
int Mode, Most, Count;
Count = Most = 0;
for (int i = 0; i < size; i++)
{
Count++;
if (*(array + i) < *(array + i + 1))
{
if (Count > Most)
{
Mode = *(array + i);
Most = Count;
}
Count = 0;
}
}
return Mode;
}