int arr[10], i;
arr[0] = 0;
for (i = 0; i < 10; i++) {
printf("%d\n", arr[i]);
}
Only the first element is initialized, so this code is printing out undefined values.
Correct!
This is an off-by-one error; i’s value goes from 0 to 9 inside the loop, but this array’s indices should go from 1 to 10.
Incorrect. Array indices start at 0.
Only the first element is initialized, so the program will crash.
Incorrect. The program does not crash.
You cannot printf individual elements of an array, you have to print the whole array at once.
Incorrect
There is nothing wrong with this code.
Incorrect
Hint.
You might be tempted to run this code for yourself and see what prints out, but this could deceive you into choosing the wrong answer.
Exercise1.5.2.Arrays, Multiple-Choice (Q2).
Suppose we have the following array declaration:
int arr[10];
How can we increase its size to 100?
We can’t. In C, arrays like this are a fixed size.
Correct!
arr.append(90);
Incorrect. The append() method is used for Python lists, not C arrays.
for (i = 0; i < 90; i++) { arr.append(0); }
Incorrect. The append() method is used for Python lists, not C arrays.
arr = int arr[100];
Incorrect.
Hint.
This is an important difference between Python and C
Exercise1.5.3.Arrays, Parsons Problem (Q1).
Given the following array of integers:
int arr[8];
Write code that will initialize all the elements to 42.
int main(void) {
---
int i, arr[8];
---
for (i = 0; i < 8; i++) {
---
arr[i] = 42;
---
}
}
---
int i, arr(8); #distractor
---
for(i=1; i < 8; i++) { #distractor
---
for(i=0; i <= 8; i++) { #distractor
---
arr = 42; #distractor
---
arr = [42]*8; #distractor
---
arr[8] = {42, 42, 42, 42, 42, 42, 42, 42}; #distractor
---
arr = {42, 42, 42, 42, 42, 42, 42, 42}; #distractor
Exercise1.5.4.Arrays, Parsons Problem (Q2).
Write a snippet of code that averages the number of elements in an int array called arr[100] and then prints out the average. Assume arr is already initialized with data.
int i, sum;
float avg;
---
sum = 0;
---
for (i = 0; i < 100; i++) {
---
sum += arr[i];
---
}
---
avg = sum / 100.0;
---
printf("%g\n", avg);
---
sum/100.0; #distractor
---
avg = sum/100; #distractor
---
for (i=1; i<=10; i++) { #distractor
---
sum = arr[i]; #distractor
Exercise1.5.5.Arrays, Active Code.
Complete the implementation of the function find_min(int arr[], int len) below. Your functions should find the minimum in the passed array, which will always contain at least one element.
Exercise1.5.6.Strings, Multiple-Choice (Q1).
Suppose you have a string named str1 and you want to print it to the screen with a newline following it. How do you do so?
char str[20];
int len;
strcpy(str, "hello there");
len = strlen(str);
How would you print str to the screen with a newline following it?
printf("%s\n",str);
Correct!
printf("%str\n",s);
Incorrect.
printf("%s %d\n",str,len);
Incorrect
printf("%s",str);
Incorrect, but close! This does not include a newline at the end!
Hint.
Make sure you’re familiar with the "format string" system used by printf.
Exercise1.5.7.Strings, Multiple-Choice (Q2).
What is wrong with the following code?
char str1[10];
int len;
str1[0] = 'h';
str1[1] = 'i';
str1[2] = '0';
len = strlen(str1);
The string is not null terminated, the character ’0’ is not the same as ’\0’
Correct!
It should be the letter ’O’ instead of the character ’0’ to end the string
Incorrect. Character O is not what we are looking for. How are strings terminated?
Nothing is wrong, len is set to 2
Incorrect
Nothing is wrong, len is set to 3
Incorrect
Hint.
This question will trigger undefined behavior, so it’s possible that if you try running the code you might get the wrong answer. Or it might crash!
What is the smallest possible value of SIZE that would ensure that strcpy would have enough space to store the copied string?
Exercise1.5.10.Strings (strcat).
Suppose you want to concatenate two strings: a string str1 that contains the string "Hello" (which requires 6 bytes to store), and a string str2 that contains the string "World" (which also requires 6 bytes to store).
How many bytes would be needed to hold the concatenated string?
Hint1.
If you strcat "Hello" and "World" together, you will get "HelloWorld" not "Hello World".
Hint2.
Remember you need a byte to store the null terminator at the end of the string too.
Exercise1.5.11.Strings, Active Code.
Write a new program that implements your own custom version of a string copy function that takes a destination and a source string and copies the source string to the destination string. Test it out in main by making calls to your string copy function with different string inputs and printing out the string copy results after each call.