-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTR141_MidTerm.cpp
More file actions
193 lines (158 loc) · 4.39 KB
/
CPTR141_MidTerm.cpp
File metadata and controls
193 lines (158 loc) · 4.39 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
187
188
189
190
191
192
193
/*****************************************************************************
Program file name: petedan.cpp OS: Mac OSX 10.4 Assignment #: MidTerm
Programmer: Daniel Peterson Class: CPTR141 Date: 11-2-06
Compiler: Xcode 2.2.1
Assistance/references: none
Description: MidTerm test
Inputs: numbers
Outputs: mathematical relationships
Special Comments:
~~~~~~~~~~~~~~~~~~~~~~~~~~Grading Criteria~~~~~~~~~~~~~~~~~~~~~~~~~~
Assignment Requirements ___/3.0
Code Format/Cosmetics ___/3.0
Header & Code Comments: ___/2.0
Output Format/Cosmetics ___/2.0
***Does Not Compile***: ___ (-10.0)
***Late Work Deduction***: ___ (-0.5/day)
Total Grade: ___/10.0
*****************************************************************************/
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
void printmenu()
{
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
cout << "+ Choose one of the following: +\n";
cout << "+ 1: Investment Calculator +\n";
cout << "+ 2: Sort integers +\n";
cout << "+ 3: Find all 3 digit numbers that = the sum of the cubes of their digits +\n";
cout << "+ 4: Rotate 3 integers +\n";
cout << "+ 5: Find the nth tribonacci number +\n";
cout << "+ 0: Exit +\n";
cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
cout << "Please enter your choice: ";
}
float invest(float p, float r, int n, float t)
{
float A;
A = (p*pow((1+(r/n)),(n*t)));
return A;
}
void investIO()
{
float p,r,t;
int n;
cout << "You have chosen the investment calculator.\n";
cout << "What is your principal? ";
cin >> p;
cout << "What is your interest rate? (expressed as a decimal) ";
cin >> r;
cout << "How many times is your interest compounded per year? ";
cin >> n;
cout << "How many years do you want to calculate for? ";
cin >> t;
cout << "You would end up with $" << invest(p,r,n,t) << endl;
}
void sort3()
{
int a,b,c;
cout << "Enter 3 integers." << endl;
cout << "First integer: ";
cin >> a;
cout << "Second integer: ";
cin >> b;
cout << "Third integer: ";
cin >> c;
if(a<b)
if(b<c)
cout << a << ", " << b << ", " << c << endl;
else
if(a<c)
cout << a << ", " << c << ", " << b << endl;
else
cout << c << ", " << a << ", " << b << endl;
else
if(a<c)
cout << b << ", " << a << ", " << c << endl;
else
if(b<c)
cout << b << ", " << c << ", " << a << endl;
else
cout << c << ", " << b << ", " << a << endl;
}
void digitsCubed()
{
int a,b,c;
for(a=1;a<10;a++)
for(b=0;b<10;b++)
for(c=0;c<10;c++)
if((a*a*a + b*b*b + c*c*c)==(100*a + 10*b + c))
cout << a << b << c << endl;
}
void rotate(int &a, int &b, int &c)
{
int temp;
temp = a;
a=c;
c=b;
b=temp;
}
void rotateIO()
{
int a,b,c;
cout << "You have chosen to rotate three integers.\n";
cout << "Please enter your first integer: ";
cin >> a;
cout << "Please enter your second integer: ";
cin >> b;
cout << "Please enter your third integer: ";
cin >> c;
rotate(a, b, c);
cout << "Your numbers have been rotated to: " << a << ", " << b << ", " << c << endl;
}
unsigned long tribonacci(int n)
{
if((n==1)||(n==2)||(n==3))
return 1;
else
return (tribonacci(n-1) + tribonacci(n-2) + tribonacci(n-3));
}
void tribIO()
{
int n;
cout << "You have chosen to find the nth Tribonacci number.\n";
cout << "Enter an integer in the interval [1,38]: ";
cin >> n;
while((n<1)||(n>38))
{
cout << "Your number is not in the interval [1,38].\n";
cout << "Please try again: ";
cin >> n;
}
cout << "The nth Tribanacci number (n=" << n << ") is: " << tribonacci(n) << endl;
}
int main (int argc, char * const argv[])
{
int choice;
cout << "Welcome to my midterm exam.\n\n";
printmenu();
cin >> choice;
while(choice != 0)
{
switch(choice)
{
case 1: investIO(); break;
case 2: sort3(); break;
case 3: digitsCubed(); break;
case 4: rotateIO(); break;
case 5: tribIO(); break;
default: cout << "\nInvalid input. Please try again.\n";
}
printmenu();
cin >> choice;
}
cout << "Goodbye.\n";
system("PAUSE");
return EXIT_SUCCESS;
}