Practice Questions for Exam 2

The following are a set of questions to help you prepare for Exam 2. I recommend trying out these problems with paper and pencil. Remember that any material from the first nine weeks is eligible, whether or not it appears in a problem below. There will be a focus on material from after the first exam, however since most things build on our previous work, ideas from those first weeks will appear as well.

Topics these questions cover:

  • pointers
  • arrays as pointers
  • reading files
  • structures
  • strings

Problem 1

Consider the following C code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int fun1(int a, int * b){
  a = 2;
  *b = 12;
  return 0;
}

void fun2(int * a, int *  b){
  fun1(*a,b);
  *b = fun1(*b,a);
}

int main(void){
  int a,b;
  a = 1;
  b = 7;

  fun2(&a,&b);
  printf("Value for a = %d, b = %d after calling fun2\n", a,b);
  
}


What values for a and b are printed in the main function?

Problem 2

Consider the following snippet of C code.

#include <stdio.h>
#include <stdbool.h>

/* A program to reverse the elements of an array.
*
*/

void reverse1(int message[],int len){






}//end of reverse1

void reverse2(int* message, int len){





}//end of reverse2

int main(void){
  int code[] = {8,6,7,5,3,0,9};

  reverse1(code, 7);
  // array should be in reversed order now

  reverse2(code, 7);
  // array should be in original order now
  
  
  return 0;
}//end of main

(a) Fill in the first function using array indexing.

(b) Fill in the second function using function pointers.

Problem 3

The string.h library includes a number of useful functions. In this problem, you’ll write one of them from scratch. Fill in the function below so that the function has the same functionality as strlen in the string.h library.

/* Function which returns the length of a string.
*  We may assume that the string is stored 
*  with a terminating null character.
*  Program should return the number of elements
*  of the string, not including the null terminating
*  character. 
*/
int myStringLen(const char *str){
  // your code here








}

Problem 4

Suppose we have the following structure declared in a program.

struct color{
  int red;
  int green;
  int blue;
};

(a) Write the following function, which returns the value of c’s red member.

int getRed(struct color c);

(b) Write the following function, which returns true if the corresponding members of color1 and color2 are equal.

bool equal_color(struct color color1, struct color color2);

(c) Write the following function, which receives the struct as a pointer, and updates the green member to be 0.

void zeroGreen(struct color *c)

Problem 5

Write a C program that has the following properties:

  • The main function has command line arguments
  • The expected call of the program is to include a single parameter which represents a file to be read in main. The file has some number of rows (unknown), and three integers on each row in the following form:
1 3 1
2 4 6
3 7 3
2 -1 1
...
  • The program should report, for how many lines of the file, the first number plus the second number is equal to the third number.
  • The program should include a call to perror if the file given is not succesfully opened.