-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
179 lines (154 loc) · 5.62 KB
/
main.cpp
File metadata and controls
179 lines (154 loc) · 5.62 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
#include <iostream>
#include <string>
using namespace std;
void task1();
void task2();
void task3();
void task4();
void reverseArray(int array1[], int arrayLength);//for task 1
int removeDuplicates(int array1[], int arrayLength);//for task 2
void swap(int& num1, int& num2);//for task 4
int mainMenu();
int main() {
cout << "CS211 Lab 8\nMax Davy\n";
int task = mainMenu();
while (task > 0 && task < 5) {
switch (task) {
case 1:
task1();
break;
case 2:
task2();
break;
case 3:
task3();
break;
case 4:
task4();
break;
}
task = mainMenu();
}
return 0;
}
int mainMenu() {
cout << "Enter task (1-4). Enter any other number to quit.\n";
int input;
cin >> input;
return input;
}
/*-------------------begin task 1 code-------------------
Task 1: Write a function which reverses the original array using pointers.
*/
void task1() {
cout << "Task 1:\n";
int array1[] = { 1,2,3,4,5,6};
cout << "\tInput array:\n";
for (int i = 0;i < 6;i++) {
cout << "\t"<<array1[i] << endl;
}
cout << "\tOutput array:\n";
reverseArray(array1, 6);
for (int i = 0;i < 6;i++) {
cout << "\t"<<array1[i] << endl;
}
}
//for task 1
void reverseArray(int array1[], int arrayLength) {
int firstItemIndex = 0;
int secondItemIndex = arrayLength - 1;
//loop works in pairs moving inwards, e.g. swaps first and last, then second and second to last, etc.
while (firstItemIndex < secondItemIndex) {//until the middle of the array is reached
int* firstItemPointer = array1 + firstItemIndex;//get pointer to first item to swap
int* secondItemPointer = array1 + secondItemIndex;//get pointer to second item to swap
int temp = *firstItemPointer;//save value at first memory location to temporary variable
*firstItemPointer = *secondItemPointer;//save value at second memory location to first memory location
*secondItemPointer = temp;//save temp value (was at first memory location) to second memory location
firstItemIndex += 1;//move first memory location one item forward
secondItemIndex -= 1;//move second memory location one item back
}
}
/*-------------------begin task 2 code-------------------
Task 2: Write a function to remove duplicates from an ordered array
*/
void task2() {
cout << "Task 2:\n";
int array1[] = { 1,2,3,3,4,5 };
cout << "\tInput array:\n";
for (int i = 0;i < 6;i++) {
cout << "\t" << array1[i] << endl;
}
int length = removeDuplicates(array1, 6);
cout << "\tOutput array:\n";
for (int i = 0;i < length;i++) {
cout << "\t" << array1[i] << endl;
}
}
//for task 2
int removeDuplicates(int array1[], int arrayLength) {
bool ordered = false;
int arrayIndex = 0;//track where we are in the temporary array
int tempArray[100];//define a temporary array, maximum length function can accept is 100
for (int i = 0;i < arrayLength;i++) {//go through array
if (array1[i] != array1[i + 1]) {//if the current array element isn't a duplicate of the one after it
tempArray[arrayIndex] = array1[i];//add the non-duplicate to the temporary array
arrayIndex++;//increment position in temp array
}
}
for (int i = 0;i < arrayIndex;i++) {//save temporary array values back to the first array
array1[i] = tempArray[i];
}
return arrayIndex;//return length of new array
}
/*-------------------begin task 3 code-------------------
Task 3: Write a program to find the GCD and LCM of two numbers
(you may need to reread how to find GCD and LCM if you have forgotten it)
*/
void task3() {
cout << "Task 3:\n";
int number1;
int number2;
cout << "\tPlease enter first number:\n";
cin >> number1;
cout << "\tPlease enter second number:\n";
cin >> number2;
//find lcm
bool divisible = false;
int multiple = (number1 > number2) ? number1 : number2;//set multiple to the greater of the two numbers
while (!divisible) {//as long as the last checked multiple was not divisible
multiple++;//increment multiple
divisible = (multiple % number1 == 0) && (multiple % number2 == 0);//check if multiple is divisible evenly by both numbers
}
cout << "\tLeast Common Multiple of " << number1 << " and " << number2 << " is " << multiple << endl;
//find gcd
divisible = false;
multiple = (number1 > number2) ? number2 : number1;//set multiple to the lesser of the two numbers
while (!divisible) {//as long as the last checked multiple was not divisible
multiple--;//decrement multiple
divisible = (number1%multiple == 0) && (number2%multiple == 0);//check if multiple is divisible evenly by both numbers
}
cout << "\tGreatest Common Divisor of " << number1 << " and " << number2 << " is " << multiple << endl;
}
/*-------------------begin task 4 code------------------
Task 4: Implement a swap() function that exchanges the values of two integers.
Call the function from the main function to test the function with different values.
*/
void task4() {
int num1;
int num2;
cout << "Task 4:\n";
cout << "\tEnter first number:\n";
cin >> num1;
cout << "\tEnter second number:\n";
cin >> num2;
swap(num1, num2);
cout << "\tSwapping numbers...\n";
cout << "\tFirst number is now " << num1 << endl;
cout << "\tSecond number is now " << num2 << endl;
}
//for task 4
void swap(int& num1, int& num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}