-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREC9.cpp
320 lines (244 loc) · 7.98 KB
/
REC9.cpp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//Problem 3.1a
// #include <iostream>
// #include <string>
// using namespace std;
// void flipSign(int numbers[]){
// for (int i = 0; i < 14; i++){
// numbers[i] = -1.0 * numbers[i];
// }
// //removed return statement since void functions cannot return values
// }
// int main(){
// const int length = 4;
// int numbers[] = {1, 2, 3, 4};
// cout << "The contents of the array before changing: ";
// for (int i = 0; i < length; i++){
// cout << numbers[i] << " ";
// }
// cout << endl;
// flipSign(numbers);
// cout << "The contents of the array after changing: ";
// for (int i = 0; i < length; i++){
// cout << numbers[i] << " ";
// }
// return 0;
// }
//Problem 3.1b
// #include <iostream>
// #include <string>
// #include <fstream> //added f stream
// using namespace std;
// int main(){
// ifstream in_file("visitors.txt");
// string full_line;
// string days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
// int visitors[5] = {0, 0, 0, 0, 0};//changed array to visitors instead of vis to match the rest of the variable calls
// int i = 0;
// int traffic = 0;
// int j = 0;
// while (getline(in_file, full_line)){
// for(int i = 0; i < static_cast<int>(full_line.length()); i+=1){
// if(full_line[i] == ' ' && i < static_cast<int>(full_line.length())-1){
// visitors[i]++;
// }
// if(full_line[i] == ','){//changed quotes to single quotes since it is looking for a character
// visitors[i]++;
// }
// }
// if (visitors[i] < traffic){
// traffic = visitors[i];
// j = i;
// }
// i++;
// }
// cout << days[j] << " is the busiest day of the week at the motel." << endl;
// return 0;
// }
//Problem 3.1c
// #include <iostream>
// #include <string>
// #include <cassert>
// using namespace std;
// int split(string input_string, char separator, string arr[], const int ARR_SIZE){
// int arrayPosition = 0;
// string newString;
// bool presentSeparator = false;
// if(input_string == ""){
// return 0;
// }
// for(unsigned int i = 0; i < input_string.length() && arrayPosition < ARR_SIZE; i++){
// if(input_string[i] != separator){
// newString += input_string[i];
// }
// else if(input_string[i] == separator){
// arr[arrayPosition] = newString;
// newString = "";
// arrayPosition += 1;
// presentSeparator = true;
// }
// }
// if(newString.length() > 0 && arrayPosition < ARR_SIZE){
// arr[arrayPosition] = newString;
// }
// if(presentSeparator == false){
// arr[0] = newString;
// return 1;
// }
// else if(presentSeparator == true && arrayPosition >= ARR_SIZE){
// return -1;
// }
// else if(presentSeparator == true){
// return arrayPosition + 1;
// }
// return 0;
// }
// //added split function right before
// string appendPrepend(string message){
// const int LENGTH = 4;
// string message_fragments[LENGTH] = {};
// string empty_word = "";
// split(message, ' ', message_fragments, LENGTH);//added semicolon and removed the brackets from the array
// //assert(message_fragments[4] == empty_word);//added semicolon
// string answer, word;
// for(int i = 0; i < LENGTH; i++){//removed double =
// answer += "_" + message_fragments[i]; //removed the second _ and made message as the message fragment array instead
// }
// int first_word_length = message_fragments[0].length();
// int second_word_length = message_fragments[1].length();
// //assert(message_fragments[1] == answer.substr(first_word_length+3, second_word_length)); //made single equals double equals instead
// return answer;
// }
// int main(){
// cout << "Please enter the string message:" << endl;
// string message;
// getline(cin, message);
// cout << appendPrepend(message);
// }
//Problem 3.2
#include <iostream>
#include <fstream>
#include <cmath>
#include <sstream>
using namespace std;
int split(string input_string, char separator, string arr[], const int ARR_SIZE){
int arrayPosition = 0;
string newString;
bool presentSeparator = false;
if(input_string == ""){
return 0;
}
for(unsigned int i = 0; i < input_string.length() && arrayPosition < ARR_SIZE; i++){
if(input_string[i] != separator){
newString += input_string[i];
}
else if(input_string[i] == separator){
arr[arrayPosition] = newString;
newString = "";
arrayPosition += 1;
presentSeparator = true;
}
}
if(newString.length() > 0 && arrayPosition < ARR_SIZE){
arr[arrayPosition] = newString;
}
if(presentSeparator == false){
arr[0] = newString;
return 1;
}
else if(presentSeparator == true && arrayPosition >= ARR_SIZE){
return -1;
}
else if(presentSeparator == true){
return arrayPosition + 1;
}
return 0;
}
bool validateDouble(string input){
//if the string is empty return false
if (input.length() == 0){
return false;
}
//the string must have at least one numerical digit, but it can also start with a minus sign
//it can have up to one decimal
int numDecimals = 0;
bool seenDigit = false;
//if the string is longer than 1, the first character can be a digit or a minus sign
switch(input[0]){
case '-':
break;
case '.':
numDecimals++;
break;
default:
if (isdigit(input[0])){
seenDigit = true;
}
else{
return false;
}
}
//check if all other digits are valid for index 1 through the end
for (int i = 1; i < (int)input.length(); i++){
if (input[i] == '.'){ //if the character isn't a number return false
numDecimals++;
}
else if (isdigit(input[i])){
seenDigit = true;
}
else{
return false;
}
}
if (numDecimals > 1){
return false;
return seenDigit;
}
return 0;
}
int main(){
//declare variables
ifstream in_file("coordinates1.txt");
string line;
const int size = 3;
string coords[size];
double doubCoords[size];
double totalX, totalY, totalZ;
double amountX = 0.0, amountY = 0.0, amountZ = 0.0;
double avgX = 0.0, avgY = 0.0, avgZ = 0.0;
//check the validate double function
while(getline(in_file, line)){
validateDouble(line);
}
//if there is an invalid double then return there is an invalid value
if(validateDouble == 0){
cout << "Invalid value detected!" << endl;
return 0;
}
//else check each line, split them at the delimeter and add each coordinate depending on position
else{
while(getline(in_file, line)){
split(line, ',', coords, size);
for(int i = 0; i < size; i++){
if(i == 0){
doubCoords[i] = stod(coords[i]);
totalX += doubCoords[i];
amountX += 1.0;
}
if(i == 1){
doubCoords[i] = stod(coords[i]);
totalY += doubCoords[i];
amountY += 1.0;
}
if(i == 2){
doubCoords[i] = stod(coords[i]);
totalZ += doubCoords[i];
amountZ += 1.0;
}
}
}
}
avgX = totalX/amountX;
avgY = totalY/amountY;
avgZ = totalZ/amountZ;
cout << "The center of mass is at: " << avgX << ", " << avgY << ", " << avgZ << "!" << endl;
}