# Dive Into Systems: Exercises

## Section1.5Arrays and Strings

### Checkpoint1.5.1.Arrays, Multiple-Choice (Q1).

What is wrong with the following code?
int arr, i;

arr = 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.

### Checkpoint1.5.2.Arrays, Multiple-Choice (Q2).

Suppose we have the following array declaration:
int arr;
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;
• Incorrect.
Hint.
This is an important difference between Python and C

### Checkpoint1.5.3.Arrays, Parsons Problem (Q1).

Given the following array of integers:
int arr;
Write code that will initialize all the elements to 42.

### Checkpoint1.5.4.Arrays, Parsons Problem (Q2).

Write a snippet of code that averages the number of elements in an int array called arr. Assume arr is already initialized with data.

### Checkpoint1.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.
#include <stdio.h>
int find_min(int arr[], int len){
}

int main(void){
int test = {1,4,2,3,5};
int test2 = {0,5,-3,3,11,1};
int test3 = {0,0,4,-1};

printf(“The minimum in test is: %d (should be 1)\n”, find_min(test,5));
printf(“The minimum in test2 is: %d (should be -3)\n”, find_min(test2,6));
printf(“The minimum int test3 is: %d (should be -1)\n”, find_min(test3,4));
return 0;
}


### Checkpoint1.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;
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",str1);
• 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.

### Checkpoint1.5.7.Strings, Multiple-Choice (Q2).

What is wrong with the following code?
char str1;
int len;

str1 = 'h';
str1 = 'i';
str1 = '0';

len = strlen(str1);

• The string is not null terminated, 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!

### Checkpoint1.5.8.Strings (strlen).

Given the code snippet
char str1;

str1 = 'a';
str1 = 'b';
str1 = '\0';
str1 = 'd';

What does a call to strlen(str1) return?
Hint.
A string is terminated by the null character.

### Checkpoint1.5.9.Strings (strcpy).

Given the following code snippet:
char str[SIZE];
strcpy(str, "hello C");

What is the smallest possible value of SIZE that would ensure that strcpy would have enough space to store the copied string?

### Checkpoint1.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?
Hint 1.
If you strcat "Hello" and "World" together, you will get "HelloWorld" not "Hello World".
Hint 2.
Remember you need a byte to store the null terminator at the end of the string too.

### Checkpoint1.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.
#include <stdio.h>
int string_copy(char source[], char destination[]){