-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hw1 - Results || Intermediate C and C++
195 lines (167 loc) · 4.66 KB
/
Hw1 - Results || Intermediate C and C++
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iomanip>
#include <iostream>
using namespace std;
//-----------------------------------------------------------
double findMean(double amounts[]);
double findMedian(double amounts[], double sortedAmounts[]);
void checkResults(double amounts[], double sortedAmounts[], string results[]);
void sortMonths(string months[], double amounts[]);
void sortAmounts(double amounts[], double sortedAmounts[]);
void printTable(string months[], double amounts[], double sortedAmounts[]);
void modifyAmounts(double amounts[], string months[]);
//-----------------------------------------------------------
int main() {
// Array with Months
string months[12] = {"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
// Array with doubles
double amounts[12] = {76321.56,
62912.84,
55986.54,
65535.35,
34794.75,
12345.67,
98765.43,
45481.25,
83289.63,
48736.29,
50901.39,
47580.26};
double sortedAmounts[12] = {};
// While loop for user menu
bool menu = true;
char input;
while (menu == true) {
cout << "\nPlease enter your choice.\n\n"
<< "To add an item: type A\n"
<< "To print the data: type P\n"
<< "To exit: type E\n\n";
input = cin.get();
// Checks response and executes action
if (input == 'A' || input == 'a') {
modifyAmounts(amounts, months);
cout << "\nTry checking the data.\n" << endl;
} else if (input == 'P' || input == 'p') {
sortAmounts(amounts, sortedAmounts);
printTable(months, amounts, sortedAmounts);
cout << "\nBye!" << endl;
// menu = false;
} else if (input == 'E' || input == 'e') {
cout << "\n\nHave a nice day!\n" << endl;
abort();
} else {
cout << "\n\nInput is not accepted.\n";
}
cin.clear();
cin.ignore(100, '\n');
}
return 0;
}
//--------------------------------------------------------------------------
double findMean(double amounts[]) {
double mean = 0;
// For loop to find mean
for (size_t i = 0; i < 12; ++i) {
mean += amounts[i];
}
mean /= 12; // finds mean
return mean;
}
double findMedian(double amounts[], double sortedAmounts[]) {
double median = 0;
median = (sortedAmounts[5] + sortedAmounts[6]) / 2;
return median;
}
void checkResults(double amounts[], double sortedAmounts[], string results[]) {
results[11] = "Outstanding Sales";
results[10] = "Outstanding Sales";
results[9] = "Outstanding Sales";
double mn = findMean(amounts);
for (size_t m = 0; m < 12; ++m) {
if (amounts[m] > mn && amounts[m] < sortedAmounts[9]) {
results[m] = "Above Average Sales";
}
}
}
void sortMonths(string months[], double amounts[]) {
int n = 12;
// Sort Total Sales
for (size_t i = 0; i < n - 1; i++) {
for (size_t j = 0; j < n - i - 1; j++) {
if (amounts[j] > amounts[j + 1]) {
swap(amounts[j], amounts[j + 1]);
swap(months[j], months[j + 1]);
}
}
}
}
void sortAmounts(double amounts[], double sortedAmounts[]) {
// Sorts array
for (size_t i = 0; i < 12; ++i) {
sortedAmounts[i] = amounts[i];
}
int i, j;
int n = 12;
// Loop to sort array
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (sortedAmounts[j] > sortedAmounts[j + 1]) {
swap(sortedAmounts[j], sortedAmounts[j + 1]);
}
}
}
}
void printTable(string months[], double amounts[], double sortedAmounts[]) {
string temp1 = "@";
string results[12];
sortMonths(months, amounts);
checkResults(amounts, sortedAmounts, results);
// For loop to print table
cout << "\n\nWelcome to Results by !\n"
<< endl
<< "\nMonth" << setw(20) << "Total Sales" << setw(20) << "Results"
<< endl;
for (size_t i = 0; i < 12; ++i) {
temp1 = months[i];
cout << months[i] << setw(25 - temp1.size()) << setprecision(2) << fixed
<< amounts[i] << setw(25) << results[i] << endl;
}
cout << "\n\nThe mean is $" << setprecision(2) << fixed << findMean(amounts)
<< ", and the median is $" << findMedian(amounts, sortedAmounts) << "."
<< endl;
}
void modifyAmounts(double amounts[], string months[]) {
int t = 1;
string tempMonth;
double salesAmt = 0;
while (t == 1) {
cout << "\n\nPlease enter the number of the month and sales amount you "
"want to modify. \nE.g. January 12345.67\n"
<< endl;
cin >> tempMonth >> salesAmt;
// cout << tempMonth << endl << setprecision(2) << salesAmt;
bool valid = false;
for (size_t i = 0; i < 12; ++i) {
if (tempMonth == months[i]) {
valid = true;
amounts[i] = salesAmt;
break;
}
}
if (valid == true) {
t = 0;
} else {
cout << "\nInput not accepted. Please try again" << endl;
}
}
}