Study Guide 1
Exams will be closed book. Lab coding challenges will be closed-internet.
Topics:
-
UNIX commands
-
Basic C programs
-
Data types and sizes
-
Function stacks
-
Arrays
Write C programs to check your answers. Or you can ask a TA or instructor to give feedback on your responses.
Exercises
1) What sizes are each of the variables below? If the variable is a pointer, give the size of the memory that the pointer points to. If the variable is a struct, give the minimum size.
A)
struct Snack {
char name[32];
int quantity;
float cost;
};
B)
struct Snack snacks[2];
C)
struct Snack* snacks = malloc(sizeof(struct Snack) * 10);
D)
char message[16];
E)
int values[2];
Hint: To check your answers, use the sizeof() function.
2) Consider navigating directories from the command prompt in UNIX. Suppose you are in your home directory.
-
How would you create a subdirectory with name
foo? -
How would you see the contents of your
.bashrcfile? -
How would you list the contents of
foo? -
How can you create a file in the directory
foocalledtest.c? -
How can you compile a c program?
3) Suppose Willa is in their home directory, /home/willa. Write commands to produce the following directory structure.
Willa contains two sub-directories, A ad B, and one file hello.c. B contains a sub-directory C and file test.txt.
Below is a visualization of it where child directories are indented.
\home\willa
A
B
C
test.txt
hello.c
4) When Willow runs her program from the command line, she gets the following error? What is happening and how can she fix it?
$ ls
Makefile fortune fortune.c hello hello.c
$ hello
Command 'hello' not found
5) The following program crashes. What is the problem and how can we fix it?
#include <stdio.h>
int main() {
int* value = NULL;
int a = 4;
printf("value is %d\n", *value);
}
6) The following program crashes. What is the problem and how can we fix it?
#include <stdio.h>
#include <string.h>
void initialize(char text[]) {
strcpy(text, "pina collada");
}
int main() {
char str1[5];
initialize(str1);
printf("%s\n", str1);
return 0;
}
7) What is the output of this program?
#include <stdio.h>
int arg_modifier(int x, int *y);
int main() {
int val1, val2, ret;
printf("Enter a value: ");
scanf("%d", &val1);
printf("Enter another value: ");
scanf("%d", &val2);
// pass val1 by value and val2 by pointer:
printf("before call: val1 = %d val2 = %d\n", val1, val2);
ret = arg_modifier(val1, &val2);
printf("after call: val1 = %d val2 = %d ret = %d\n", val1, val2, ret);
return 0;
}
int arg_modifier(int x, int *y) {
printf(" in arg_modifier: x = %d *y = %d\n", x, *y);
*y = *y + x;
x = x + 5;
printf(" leaving arg_modifier: x = %d *y = %d\n", x, *y);
// Draw function stack here
return x;
}
8) Draw the state of the function stack and heap for the program above in arg_modifier. Assume the
user entered the values 6 and 1. Draw the stack at the point in the execution
right before the return from the arg_modifier function, and consider the
following questions:
-
Where are variables val1 and val2 located on the stack?
-
What value does each parameter get?
9) Consider the following code. Find and fix the errors in the associated stack diagram.
float foo(float* vals, int n)
{
float x = 0.0;
for (int i = 0; i < n; i++)
{
x += vals[i];
}
return x;
}
void main()
{
float heights[3] = {6.1f, 5.2f, 4.9f};
float a = foo(heights, 3);
float b = a / 3;
// draw stack here
printf("%.2f\n", b);
}
10) Draw the directory hierarchy that corresponds to the following commands
$ pwd
/home/alinen
$ mkdir A
$ cd A
$ mkdir Z
$ touch talk.c
$ cd ..
$ touch listen.c
$ cd
$ touch sing.c
11) The following code has an error. What is wrong and how can we fix it?
int age;
printf("Enter your age: ");
scanf("%d", age);
Coding
-
Implement a program,
ask_matrix.c, that asks the user for a number of rows and columns, creates a 2D matrix, and then asks the user for its values.
Enter a number of rows: 3
Enter a number of cols: 4
Please enter a value for matrix[0][0]: 0
Please enter a value for matrix[0][1]: 1
Please enter a value for matrix[0][2]: 2
Please enter a value for matrix[0][3]: 4
Please enter a value for matrix[1][0]: 5
Please enter a value for matrix[1][1]: 6
Please enter a value for matrix[1][2]: 7
Please enter a value for matrix[1][3]: 8
Please enter a value for matrix[2][0]: 9
Please enter a value for matrix[2][1]: -1
Please enter a value for matrix[2][2]: -2
Please enter a value for matrix[2][3]: -3
Here is your matrix!
0 1 2 4
5 6 7 8
9 -1 -2 -3
-
Write code to reverse an array
$ ./reverse_numbers
Enter the size of the array: 4
Enter a number: 2
Enter a number: 4
Enter a number: 6
Enter a number: 8
The array in reverse order is: 8 6 4 2
-
Write code that implements a power function (for positive integer exponents only). A call to your function should compute \(base^{exp}\) and might look like:
result = power(base, exp);
-
Write code that implements a function
void removeLetter(char* str, char letter)that removes all occurrences ofletterfromstr. Your function should modify the contents of str. (To check your work, implement your program and test it frommain). -
Write code that implements a function
isPalindromethat returns 1 if a given string is a palindrome and 0 otherwise. Your function can be iterative or recursive. Test your function frommain. -
Write a program that takes a list of words as command line arguments and outputs the largest one.