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
|
/*Tyler Taormina and Isabella Mon
* CST 116
*
* Project 2
*/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int GetData();
void DisplayMenu (int&);
void ProcessMenuChoice (int, int);
void IsPosNeg (int);
void IsOddEven (int);
void DisplayAdditionTable();
void DisplayMultiplicationTable();
void FindNumDigits(int);
void FindDigitAtPosition(int);
int main() {
int User_Value = 0;
int Menu_choice = 0;
User_Value = GetData();
DisplayMenu(Menu_choice);
ProcessMenuChoice(Menu_choice, User_Value);
return 0;
}
int GetData(){
//receives input from user within negative million and positive million.
int usr_data = 0;
cout << "================================================================" << endl;
cout << "Program Running..." << endl;
cout << "================================================================\n\n\n" << endl;
cout << "Please enter the number you would like to have tested: ";
cin >> usr_data;
if (usr_data < -1000000 || usr_data > 1000000 ){
cout << "Sorry. The number you have entered is out of bounds. Please try again." << endl;
GetData();
}
return usr_data;
}
void DisplayMenu(int& menu_choice) {
//displays the menu of functions for the user to choose from
cout << "1) Determine if the number is positive or negative.\n";
cout << "2) Determine if the number is odd or even.\n";
cout << "3) Determine the number of digits in the entered number.\n";
cout << "4) Determine what digit is at a given position in the number.\n";
cout << "5) Display Addition Table.\n";
cout << "6) Display Multiplication Table.\n";
cout << "7) Exit Program.\n\n";
cout << "Enter: ";
cin >> menu_choice;
if (menu_choice > 7 || menu_choice < 1) {
cout << "Invalid Entry. Please enter a number from the options list provided.\n\n\n\n" << endl;
DisplayMenu(menu_choice);
}
}
void ProcessMenuChoice (int menu_choice, int usr_data) {
int program_rerun = 0;
switch(menu_choice){
case 1:
IsPosNeg(usr_data);
break;
case 2:
IsOddEven(usr_data);
break;
case 3:
FindNumDigits(usr_data);
break;
case 4:
FindDigitAtPosition(usr_data);
break;
case 5:
DisplayAdditionTable();
break;
case 6:
DisplayMultiplicationTable();
break;
case 7:
cout << "End program." << endl;
break;
default:
break;
}
cout << "Press 1 and enter to rerun program. Enter any other number to close program: ";
cin >> program_rerun;
if (program_rerun == 1)
main();
else {
cout << "================================================================" << endl;
cout << "Program Closing..." << endl;
cout << "================================================================\n\n\n" << endl;
}
}
void IsPosNeg (int usr_data) {
// checks whether the number entered is positive or negative.
// Displays appropriate message.
if (usr_data < 0)
cout << "The number is negative" << endl;
else if (usr_data > 0)
cout << "The number is positive." << endl;
else
cout << "The number is zero." << endl;
}
void IsOddEven(int usr_data) {
// checks if the user enterd number is odd or even.
// displays an appropriate message.
if ((usr_data % 2) == 0)
cout << "The number is even." << endl;
else
cout << "The number is odd." << endl;
}
void DisplayAdditionTable()
{
int i, j, k;
int intList1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int intList2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << "ADDITION TABLE!\n" << endl;
for (i = 0; i < 11; i++) {
cout << setw(5) << left << intList1[i] << setw(5) << left;
}
cout << endl;
for (k = 0; k < 10; k++) {
cout << setw(5) << left << intList2[k];
for (j = 1; j < 11; j++) {
cout << setw(5) << left << intList1[j] + intList2[k];
}
cout << endl;
}
cout << endl;
}
void DisplayMultiplicationTable()
{
int i, j, k;
int intList1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int intList2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << "Multiplication Table!\n" << endl;
for (i = 0; i < 11; i++) {
cout << setw(5) << left << intList1[i] << setw(5) << left;
}
cout << endl;
for (k = 0; k < 10; k++) {
cout << setw(5) << left << intList2[k];
for (j = 1; j < 11; j++) {
cout << setw(5) << left << intList1[j] * intList2[k];
}
cout << endl;
}
cout << endl;
}
void FindNumDigits(int usr_data)
{
int copy = usr_data;
// the "length" of 0 is 1:
int length = 1;
// and for numbers greater than 0:
if (copy > 0) {
// we count how many times it can be divided by 10:
// (how many times we can cut off the last digit until we end up with 0)
for (length = 0; copy > 0; length++) {
copy /= 10;
}
}
// and that's our "length":
cout << "The number of digits is: " << length << endl << endl;
}
void FindDigitAtPosition(int usr_data)
{
int position = 0;
int copy = usr_data;
int copy_length = usr_data;
int correct_pos;
// the "length" of 0 is 1:
int length = 1;
// and for numbers greater than 0:
if (copy_length > 0) {
// we count how many times it can be divided by 10:
// (how many times we can cut off the last digit until we end up with 0)
for (length = 0; copy_length > 0; length++) {
copy_length /= 10;
}
}
cout << "Enter the position you would like to check: ";
cin >> position;
correct_pos = length - position;
copy /= pow(10, correct_pos);
copy = abs(copy % 10);
cout << "The number at position " << position << " is: " << copy << endl;
}
|