-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment3.cpp
More file actions
165 lines (157 loc) · 3.61 KB
/
assignment3.cpp
File metadata and controls
165 lines (157 loc) · 3.61 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
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <ctime>
#include <iomanip>
#include <algorithm>
using namespace std;
int getIntegerWithPrompt(string prompt);
bool isIntegerInput(string s);
bool getyesno();
int main()
{
vector<int> theList;
int min = 0, max = 0;
long int sum = 0;
int iInputs = 0;
while (iInputs <= 0)
{
iInputs = getIntegerWithPrompt("Please enter the number of random numbers to be generated: ");
if (iInputs <= 0)
cout << "\nPlease enter a positive number!\n";
}
cout << "What is the range?" << endl;
while (min >= max)
{
min = getIntegerWithPrompt("Minimum ");
max = getIntegerWithPrompt("Maximum ");
if (min > max)
cout << "Not a valid range." << endl;
}
int range = max - min;
int blocks = range / RAND_MAX;
srand(time(0));
for (int i = 0; i < iInputs; i++)
{
int r = 0;
if (blocks)
{
r += (rand() % blocks) * RAND_MAX;
r += rand() % (range % RAND_MAX);
}
else
r += (rand() % range);
r += min;
theList.push_back(r);
sum += r;
}
cout << "\nThe total of " << iInputs << " real random numbers is : " << sum;
cout << "\nThe average of " << iInputs << " real random numbers is : " << (sum / static_cast<float>(iInputs));
cout << "The list of real random numbers is: ";
int width = 0;
int dying = iInputs;
while (dying > 1)
{
++width;
dying = dying / 10;
}
for (unsigned int i = 0; i < theList.size(); i++)
{
if (i % 5 == 0)
{
cout << endl;
}
cout << setw(width + 3) << theList[i] << ' ';
}
sort(theList.begin(), theList.end());
cout << endl << endl << "The list of after sorting is: ";
for (unsigned int i = 0; i < theList.size(); i++)
{
if (i % 5 == 0)
{
cout << endl;
}
cout << setw(5) << theList[i] << ' ';
}
bool repeat = false;
bool flag = false;
do
{
//cout << endl << "Please enter a number to be searched: ";
int userinput;
cout << endl << endl;
userinput = getIntegerWithPrompt("Please enter a number to be searched: ");
flag = binary_search(theList.begin(), theList.end(), userinput);
cout << "The number \'" << userinput << "\' was" << (flag ? " " : " not ") << "found." << endl;
cout << "Would you like to search again? ";
repeat = getyesno();
} while (repeat);
cout << "\nThank you. See you again!";
return 0;
}
bool getyesno()
{
string input;
bool retry = false;
do
{
getline(cin, input);
if (input[0] == 'y' || input[0] == 'Y')
return true;
else if (input[0] == 'n' || input[0] == 'N')
return false;
else
{
cout << "\nInvalid Input!\nPlease enter y/n: ";
retry = true;
}
} while (retry);
return false;
}
int getIntegerWithPrompt(string prompt)
{
bool test = false;
int n = -1;
string sinput;
do {
cout << prompt;
//How many Students would you like to add: 2 \n\n
getline(cin, sinput);
if (sinput.length() > 7)
{
cout << "Number is too large" << endl;
continue;
}
if (!isIntegerInput(sinput))
{
cout << "Not a valid input! Please enter a number." << endl;
continue;
}
else {
n = stoi(sinput);
test = true;
//if (n < 0)
//{
// cout << "Please enter a non-negative number." << endl;
// continue;
//}
}
} while (!test);
return n;
}
bool isIntegerInput(string s)
{
bool ret = true;
//s = s.substr(s.find_first_not_of(' '), s.find_last_not_of(' ') + 1);
for (unsigned int i = 0; i < s.length(); i++)
{
ret = ret && ((s[i] <= '9') && ('0' <= s[i]));
if (i == 0)
{
ret = ret || (s[i] == '-');
}
}
return ret;
}