Solution to Self Check
Concepts Questions:
1. For each scenario below, explain what data structure you would use and why: - Storing the grades for 30 students in a class: - Array of 30 integers (or floats/doubles depending on grading system) because we have a fixed number of students and need to store multiple values of the same type. - double grades[30];
- Storing a person's first name (maximum 20 characters): - Character array (C-string) with capacity of 21 (20 characters + 1 for null terminator) because we need to store a sequence of characters - char name[21]; - Keeping track of the temperature readings for each hour of the day: Array of floats/doubles with 24 elements because there are exactly 24 hours in a day and we need to store multiple temperature values - double temperature[24]; - Storing a single test score: A single variable (int, float, or double) because we only need to store one value - int score;
2. What is wrong with the following array declaration if we want to store a word that is at most 15 characters long?
char word[15];
The array is too small. C-strings require a null terminator \0
at the end, so to store a 15-character word, we need an array of size 16:
char word[16]; // 15 characters + 1 for null terminator
3. Given the following code:
int numbers[10] = {5, 3, 8};
numbers[0]
= 5numbers[2]
= 8numbers[5]
= undefined (the initialization only set the first 3 elements; the remaining elements have undefined values)
4. What is the output of the following code?
char message[20] = "Hello";
int i;
for (i = 0; message[i] != '\0'; i++) {
printf("%c", message[i]);
}
- Output:
Hello
- The loop iterates through each character until it reaches the null terminator, printing each character.
5. Explain the difference between the capacity of an array and the used portion of an array. Why do we need to track both? - Capacity is the total number of elements that the array can hold (declared size). - Used is the number of elements actually storing meaningful data. - We need to track both because: - Capacity tells us the maximum we can store (prevents out-of-bounds access) - Used tells us how many elements actually contain valid data (prevents processing uninitialized/garbage values)
6. What is the purpose of the const
keyword when passing an array to a function? Give an example of when you would use it. - The const
keyword prevents the function from modifying the array. It provides compile-time protection against accidental changes. - Example: When calculating the sum of an array, we don't want to modify the original values in the function
7. Given an array declared as int values[100]
, what are the valid index values for this array?
- Valid indexes are 0 through 99 (inclusive). Arrays in C are zero-indexed, so the first element is at index 0 and the last element is at index (capacity - 1).
Walkthrough:
Exact output:
1 2 3 40 50
Explanation:
The program creates an array {10, 20, 30, 40, 50}
and passes it to go()
with size 3. The function divides the first 3 elements by 10, changing them to 1, 2, and 3. The last two elements (40 and 50) remain unchanged because the function was given a size of 3 so the loop only runs 3 times.
Debugging:
1. Errors identified:
-
Syntax errors:
- Parameter
str
should bechar str[]
notchar str
- Missing semicolon after
int i
- Incorrect null character:
'/0'
should be'\0'
(backslash, not forward slash)
- Parameter
-
Logical errors:
- Variable
count
is not initialized to 0 - Missing closing brace
}
for the if statement - i was initialized to 1 which will cause first element to be missed
- Assignment operator
=
used instead of comparison==
in the if statement
- Variable
-
Stylistic flaws:
- Opening brace of function should be on its own line
- Inconsistent indentation
2. Corrected function:
int countChar(char str[], char target)
{
int count = 0;
int i;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == target) {
count++;
}
}
return count;
}
Programming:
Problem 1: Count Evens
int countEvens(const int numbers[], int used)
{
int count = 0;
int i;
for (i = 0; i < used; i++) {
if (numbers[i] % 2 == 0) {
count++;
}
}
return count;
}
Problem 2: Reverse String
void reverseString(char str[])
{
int length = 0;
int i;
int j;
char temp;
// Find the length of the string
while (str[length] != '\0') {
length++;
}
//use j to reference the last character in the string
j = length - 1;
// Swap characters from beginning and end
for (i = 0; i < length / 2; i++) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
j--;
}
}
Problem 3: Find Smallest
int findSmallest(const int numbers[], int used)
{
int smallest = numbers[0];
int i;
for (i = 1; i < used; i++) {
if (numbers[i] < smallest) {
smallest = numbers[i];
}
}
return smallest;
}