forked from jdeans289/caci-intern-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_exercises.cpp
257 lines (166 loc) · 6.95 KB
/
c_exercises.cpp
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
#include <gtest/gtest.h>
#define TEST_NAME ::testing::UnitTest::GetInstance()->current_test_info()->name()
TEST (C_Exercise, pointer1) {
int a = 5;
int * a_ptr = NULL;
/********************* MAKE YOUR CHANGES HERE *************************/
FAIL() << TEST_NAME << " is not yet implemented" ;
// Set the value of a_ptr to the address of a
/**********************************************************************/
// TEST
ASSERT_TRUE(a_ptr != NULL);
ASSERT_EQ(a, *a_ptr);
}
/********************* MAKE YOUR CHANGES HERE for pointer2 *************************/
// Fill in the function body to swap the values of two int pointers
void swap (int * a, int * b) {
}
/***********************************************************************************/
TEST (C_Exercise, pointer2) {
int a = 5;
int b = 10;
/********************* MAKE YOUR CHANGES HERE *************************/
FAIL() << TEST_NAME << " is not yet implemented" ;
// Only fill in the arguments of swap
swap(NULL, NULL);
/**********************************************************************/
// TEST
ASSERT_EQ(a, 10);
ASSERT_EQ(b, 5);
}
TEST (C_Exercise, printf) {
int a = 5;
char buf[50];
/********************* MAKE YOUR CHANGES HERE *************************/
FAIL() << TEST_NAME << " is not yet implemented" ;
// Change the format string so that it will print:
// "The value of a is 5"
// with a newline character
const char * format_str = "";
/**********************************************************************/
// This is here to help you debug
printf(format_str, a);
// snprintf is just like printf, except it prints into the buffer in the first argument instead of to the terminal.
// We can use this to check your answer
snprintf(buf, sizeof(buf), format_str, a);
// TEST
ASSERT_STREQ(buf, "The value of a is 5\n");
}
TEST (C_Exercise, array_index) {
int arr[5] = {1, 2, 3, 4, 5};
int arr_3;
int * arr_3_addr;
/********************* MAKE YOUR CHANGES HERE *************************/
FAIL() << TEST_NAME << " is not yet implemented" ;
// Set arr_3 to the value at index 3
// Set arr_3_addr to the address of index 3 of the array
/**********************************************************************/
ASSERT_EQ(arr_3, 4);
ASSERT_EQ(*arr_3_addr, 4);
}
TEST (C_Exercise, array_2d_index) {
int arr[3][3] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} };
int arr_1_1;
int * arr_1_1_addr;
/********************* MAKE YOUR CHANGES HERE *************************/
FAIL() << TEST_NAME << " is not yet implemented" ;
// Set arr_1_1 to the value at index 1 of index 1
// Set arr_3_addr to the address of index 1 of index 1
/**********************************************************************/
ASSERT_EQ(arr_1_1, 5);
ASSERT_EQ(*arr_1_1_addr, 5);
}
TEST (C_Exercise, array_sum) {
int sum = 0;
const int arr_len = 5;
int arr[arr_len] = {1, 2, 3, 4, 5};
/********************* MAKE YOUR CHANGES HERE *************************/
FAIL() << TEST_NAME << " is not yet implemented" ;
// Use a loop to sum up all the elements of the array
/**********************************************************************/
ASSERT_EQ(sum, 15);
}
TEST (C_Exercise, array_2d_sum) {
int sum = 0;
int arr[3][3] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9} };
/********************* MAKE YOUR CHANGES HERE *************************/
FAIL() << TEST_NAME << " is not yet implemented" ;
// Use nested loops to sum up all the elements of the array
/**********************************************************************/
ASSERT_EQ(sum, 45);
}
TEST (C_Exercise, array_copy) {
const int arr_length = 5;
int arr_original[arr_length] = {10, 20, 30, 40, 50};
int arr_copy[arr_length];
/********************* MAKE YOUR CHANGES HERE *************************/
FAIL() << TEST_NAME << " is not yet implemented" ;
// Copy all of the values of original into copy
/**********************************************************************/
// All elements must be the same
for (int i = 0; i < arr_length; i++) {
ASSERT_EQ(arr_original[i], arr_copy[i]);
}
}
TEST (C_Exercise, dynamic_allocation1) {
int array_length = 10;
int * arr = NULL;
/********************* MAKE YOUR CHANGES HERE *************************/
FAIL() << TEST_NAME << " is not yet implemented" ;
// Allocate space for 10 ints for arr
// Set the values of each element to 1..10
/**********************************************************************/
ASSERT_TRUE(arr != NULL);
for (int i = 0; i < array_length; i++) {
// Meaning arr[0] = 1, arr[1] = 2, and so on
ASSERT_EQ(arr[i], i+1);
}
// Don't forget to free your memory!
free (arr);
}
TEST (C_Exercise, dynamic_allocation2) {
char ** sentence;
/********************* MAKE YOUR CHANGES HERE *************************/
FAIL() << TEST_NAME << " is not yet implemented" ;
// Allocate space for two char pointers sentence
// set the first value to "Hello"
// set the second value to "World"
/**********************************************************************/
ASSERT_TRUE(sentence != NULL);
ASSERT_STREQ(sentence[0], "Hello");
ASSERT_STREQ(sentence[1], "World");
// Don't forget to free your memory!
free (sentence);
// If you dynamically allocated the space for "Hello" and "World", you should free them here
}
// Notice that the argument here is const char *
// This means that str is a pointer to a constant
// In other words, str is a read only pointer
// Since we're just calculating the length of a string, we should not be changing the value of the characters in the string
int my_strlen(const char * str) {
/********************* MAKE YOUR CHANGES HERE for string_length *************************/
// Fill in this function to calculate the length of a string.
// Remember that all C strings end with the character '\0'
return -1;
/*********************************************************************************/
}
TEST (C_Exercise, string_length) {
const char * str = "This is a string in C";
// Remove this line when you implement my_strlen
FAIL() << TEST_NAME << " is not yet implemented" ;
// strlen is the C library function from <string.h> that does this
// But you should be able to implement it too :)
ASSERT_EQ(my_strlen(str), strlen(str));
}
TEST (C_Exercise, preprocessor) {
/********************* MAKE YOUR CHANGES HERE *************************/
FAIL() << TEST_NAME << " is not yet implemented" ;
/**********************************************************************/
#if SOME_VAR != 100
FAIL() << "Use preprocessor definitions to exclude this line" ;
#endif
}