-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmw_7_3.cpp
84 lines (66 loc) · 1.23 KB
/
hmw_7_3.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
/*
Homework 7_3
Arian Owji
604649619
*/
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void swap(double* a, double* b) {
double temp = *a;
*a = *b;
*b = temp;
}
void reverse(double* a, int a_size) {
double* b = a + a_size - 1;
while (a < b) {
swap(a, b);
a++;
b--;
}
}
void print(double* a, int a_size) {
for (double* p = a; p < a + a_size; p++) {
if (p == a + a_size - 1) {
std::cout << *p << endl;
}
else {
std::cout << *p << ", ";
}
}
}
int main() {
char response = 'y';
while (response == 'y') {
string input;
int aSize;
double number;
int numberCounter = 0;
int sizeCounter = 0;
std::cout << "Enter a list of numbers: ";
getline(cin, input);
for (int i = 0; i < input.size(); i++) {
if (input[i] == ' ') {
sizeCounter += 1;
}
}
aSize = sizeCounter + 1;
double* array = new double[aSize];
istringstream stream(input);
while (stream >> number) {
array[numberCounter] = number;
numberCounter += 1;
}
reverse(array, aSize);
std::cout << "Reversed list: ";
print(array, aSize);
std::cout << endl;
std::cout << "Continue <y/n>? ";
std::cin >> response;
cin.clear();
cin.ignore(1000, '\n');
std::cout << endl;
}
return 0;
}