Self Check
Concepts Questions:
- For each of the scenarios below describe what you would use to store the data described. Provide a sample declaration:
- Storing the grades for 30 students in a class
- Storing a person's first name (maximum 20 characters)
- Keeping track of the temperature readings for each hour of the day
- Storing a single test score
- 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];
- Given the following code:
What are the values stored in
int numbers[10] = {5, 3, 8};
numbers[0]
,numbers[2]
, andnumbers[5]
? - 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]);
} - Explain the difference between the capacity of an array and the used portion of an array. Why do we need to track both?
- What is the purpose of the
const
keyword when passing an array to a function? Give an example of when you would use it. - Given an array declared as
int values[100]
, what are the valid indices for this array?
Walkthrough:
a) Using tables to track your variables and function calls, show how you would go through the execution of this program. b) What is the exact output of the following program?
#include <stdio.h>
void go(int arr[], int size);
int main(void)
{
int numbers[5] = {10, 20, 30, 40, 50};
int i;
go(numbers, 3);
for (i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
void go(int arr[], int size)
{
int i;
for (i = 0; i < size; i++) {
arr[i] = arr[i] / 10;
}
}
Debugging:
The following function is supposed to count how many times a specific character appears in a string. For example, countChar("hello", 'l')
should return 2.
int countChar(char str, char target){
int count;
int i
for (i = 1; str[i] != '/0'; i++){
if (str[i] = target){
count++;
}
return count;
}
-
In the above code identify the following:
- all the syntax errors
- all logical errors
- all stylistic flaws that do not adhere to the style guide
-
Making as few changes as possible to address all of the errors above, rewrite the function so that it functions according to spec
Programming:
Problem 1:
Write the following function:
int countEvens(const int numbers[], int used);
This function counts and returns how many even numbers are in the array. The function accepts an array of integers and the number of elements actually stored in the array.
For example, if the array contains {5, 2, 8, 3, 6}
and used
is 5, the function should return 3 (because 2, 8, and 6 are even).
Problem 2:
Write the following function:
void reverseString(char str[]);
This function reverses a string in place. The function accepts a null-terminated string and reverses the order of its characters.
For example, if the string contains "hello"
, after calling this function, the string should contain "olleh"
.
Hint: You will need to find the length of the string first, then swap characters from the beginning and end, working your way toward the middle.
Problem 3:
Write the following function:
int findSmallest(const int numbers[], int used);
This function finds and returns the smallest value in an array of integers. The function accepts an array of integers and the number of elements actually stored in the array.
For example, if the array contains {15, 3, 42, 8, 23}
and used
is 5, the function should return 3.