-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalfinal.cpp
More file actions
196 lines (163 loc) · 3.64 KB
/
Copy pathfinalfinal.cpp
File metadata and controls
196 lines (163 loc) · 3.64 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
194
195
196
// June 26, 2025 Final for C++ Class
/*
// Why isn't this code working and how might we fix it?
#include <iostream>
class MyPerfectClass {
public:
MyPerfectClass() {
this->favoriteNumber = 5;
}
void pleaseWork() {
std::cout << "Hooray, it's working!";
}
private:
int favoriteNumber;
};
int main()
{
MyPerfectClass mpc;
mpc.pleaseWork();
}
// Uh oh, this code is blowing up!
// Add a try-catch block to handle the exception and print the error message.
#include <iostream>
#include <stdio.h>
#include <exception>
struct OopsException : public std::exception
{
const char* what() const throw ()
{
return "Uh oh, spaghetti codes.\n";
}
};
int runTheNumbers(int x, int y) {
if (y == 0)
throw OopsException();
else
return (x+y)*x / y;
}
int main()
{
int x = 5, y = 0;
try {
printf("runTheNumbers(%d, %d) = %d\n", x, y, runTheNumbers(x, y));
} catch (const OopsException& e) {
std::cerr << "Caught a wild exception!: " << e.what();
}
catch (const std::exception& e) {
std::cerr << "Caught a general exception!: " << e.what();
}
return 0;
}
// Given the following code, explain the purpose of the while loop
// and it's contents in insertionSort
#include <iostream>
void insertionSort(int arr[], int size)
{
int i, j, key;
for (i = 1; i < size; i++)
{
key = arr[i];
j = i - 1;
// explain what the while loop below is doing
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
std::cout << arr[i] << " ";
std::cout << '\n';
}
int main()
{
int numbers[] = { 9, 0, 5, 3, 8, 1, 4, 6, 7, 10, 2 };
int size = sizeof(numbers) / sizeof(numbers[0]);
insertionSort(numbers, size);
print(numbers, size);
}
// What is going on with this code and why won't it stop?
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = NULL;
}
};
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
std::cout << temp->data << " ";
temp = temp->next;
}
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = head;
printList(head);
}
// Given the following code,
// what is the next animal from the queue, AND the next plant from the stack?
#include <iostream>
#include <queue>
#include <stack>
int main() {
std::queue<std::string> animals;
animals.push("Aardvark");
animals.push("Bobcat");
animals.push("Cougar");
animals.pop();
std::stack<std::string> plants;
plants.push("Acacia");
plants.pop();
plants.push("Balsa");
plants.push("Cedar");
}
*/
// Given the following templated functions f(),
// which cout is called given f('a', 10);
#include<iostream>
#include <type_traits>
using namespace std;
template<typename T1, typename T2>
void f(T1 x, T2 y)
{
if (is_same<T1, string>::value) {
for (int i = y; i > 0; i--) {
cout << x << "1"; // Option #1
}
}
else {
for (int i = y; i > 0; i--) {
cout << x << "2"; // Option #2
}
}
}
template<>
void f<char, int>(char x, int y)
{
for (int i = y; i > 0; i--) {
cout << x << "3"; // Option #3
}
}
template<>
void f<int, int>(int x, int y)
{
for (int i = y; i > 0; i--) {
cout << x << "4"; // Option #4
}
}
int main()
{
f('a', 10);
}