-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProblem5.cpp
More file actions
186 lines (164 loc) · 4.01 KB
/
Problem5.cpp
File metadata and controls
186 lines (164 loc) · 4.01 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
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
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class statisticaCalc{
private:
T* data;
int size;
public:
statisticaCalc();
~statisticaCalc();
void sort();
double findMedian();
T findMin();
T findMax();
double findMean();
T findSum();
void displayArray();
void inputData(ifstream &file);
void statisticsMenu();
};
template <typename T>
statisticaCalc<T>::statisticaCalc() {
size = 0;
data = nullptr;
}
template <typename T>
statisticaCalc<T>::~statisticaCalc() {
delete[] data;
}
template <typename T>
void statisticaCalc<T>::sort() {
for(int i = 1; i < size; i++) {
T tmp = data[i];
int j = i - 1;
while(j >= 0 && data[j] > tmp) {
data[j + 1] = data[j];
j--;
}
data[j + 1] = tmp;
}
}
template <typename T>
double statisticaCalc<T>::findMedian() {
if(size % 2 == 0) {
return (static_cast<double>(data[size/2 - 1]) + static_cast<double>(data[size/2])) / 2.0;
}
return static_cast<double>(data[size/2]);
}
template <typename T>
T statisticaCalc<T>::findMin() {
return data[0];
}
template <typename T>
T statisticaCalc<T>::findMax() {
return data[size - 1];
}
template <typename T>
double statisticaCalc<T>::findMean() {
double sum = 0;
for(int i = 0; i < size; i++) {
sum += data[i];
}
return sum / size;
}
template <typename T>
T statisticaCalc<T>::findSum() {
T sum = 0;
for(int i = 0; i < size; i++) {
sum += data[i];
}
return sum;
}
template <typename T>
void statisticaCalc<T>::displayArray() {
for(int i = 0; i < size; i++) {
cout << data[i] << " ";
}
cout << endl;
}
template <typename T>
void statisticaCalc<T>::inputData(ifstream &file) {
if (!(file >> size) || size <= 0) {
cerr << "Error: Invalid size in file!" << endl;
exit(1);
}
delete[] data;
data = new T[size];
for (int i = 0; i < size; i++) {
if (!(file >> data[i])) {
cerr << "Error: Not enough data in file!" << endl;
exit(1);
}
}
sort();
}
template <typename T>
void statisticaCalc<T>::statisticsMenu() {
int choice;
do {
cout << "\n1-> Find Median" << endl;
cout << "2-> Find Min" << endl;
cout << "3-> Find Max" << endl;
cout << "4-> Find Mean" << endl;
cout << "5-> Find Sum" << endl;
cout << "6-> Display Array" << endl;
cout << "7-> Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch(choice) {
case 1:
cout << "Median: " << findMedian() << endl;
break;
case 2:
cout << "Min: " << findMin() << endl;
break;
case 3:
cout << "Max: " << findMax() << endl;
break;
case 4:
cout << "Mean: " << findMean() << endl;
break;
case 5:
cout << "Sum: " << findSum() << endl;
break;
case 6:
displayArray();
break;
case 7:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice" << endl;
}
} while(choice != 7);
}
int main() {
int typeChoice;
string filename;
cout << "Enter filename: ";
cin >> filename;
ifstream file(filename);
if (!file) {
cerr << "Error opening file!" << endl;
return 1;
}
cout << "Select data type:\n"
"1. Integer\n"
"2. Double\n"
"Enter choice: ";
cin >> typeChoice;
if (typeChoice == 1) {
statisticaCalc<int> sc;
sc.inputData(file);
sc.statisticsMenu();
} else if (typeChoice == 2) {
statisticaCalc<double> sc;
sc.inputData(file);
sc.statisticsMenu();
} else {
cout << "Invalid data type selection!" << endl;
}
file.close();
return 0;
}