Last Updated: 2015-06-17 Wed 12:16

Exam 1 Practice Problems

This document contains some practice problems designed to prepare students for the first exam. Some questions have answers are partial answers available while others do not. Students may visit office hours to discuss full answers to all problems.

Table of Contents

1 State of Stack

 1: #include <stdio.h>
 2: 
 3: double f1(double x){            /* 3. SHOW STACK THE 3rd TIME THIS FUNCTION IS CALLED */
 4:   return x+1.0;                 
 5: }
 6: 
 7: double f2(double x){
 8:   double tmp = f1(x);           /* 2. SHOW STACK AFTER THIS LINE FINISHES */
 9:   double z = f1(x+1);           
10:   return (z+ tmp) / 2;          
11: }
12: 
13: double f3(double x, double y){
14:   double z = f1(1);             
15:   double tmp1 = x*z;            /* 1. SHOW STACK AFTER THIS LINE FINISHES */
16:   double tmp2 = f2(y);          
17:   return tmp1+tmp2;             
18: }
19: 
20: int main(){
21:   double x = 2;                 
22:   double y = f3(x, x+3);        
23:   printf("%.3lf\n",y);          
24:   return 0;                     
25: }

For the program above, show the state of the function call stack in 3 cases labeled above. Note that these answers are exclusive and you need to have a different diagrams for each as some of the stack frames get overwritten by others as functions finish and other functions run.

Format your answers in the table below. The initial stack frame for main() is filled after if it finishes line 1.

main() line 1 finished

Method Line Var Value Addr Notes
main() 22 x 2.0 2048  
    y ? 2056  
           
           
           

The solutions to this problem can be found in the Function Call Stack Demonstrations Document at the following spots.

  1. f3 executes second line
  2. f1 returns to f2, f2 advances to next line
  3. f2 calls f1 on second line

2 Compile Errors

Examine the following program.

 1: #include <math.h>
 2: 
 3: int main(){
 4:   printf("Enter a real number: ");
 5:   double x;
 6:   scanf("%lf",&x);
 7:   printf("Sqrt: %lf\nExp: %lf\n",
 8:          sqrt(x), exp(x));
 9:   return 0;
10: }

On trying to compile this program on a standard unix system, the following errors and warnings are given and no program is produced.

lila [exam1-practice-code]% gcc compile_error.c 
compile_error.c: In function ‘main’:
compile_error.c:4:3: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
   printf("Enter a real number: ");
   ^
compile_error.c:4:3: warning: incompatible implicit declaration of built-in function ‘printf’
compile_error.c:6:3: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
   scanf("%lf",&x);
   ^
compile_error.c:6:3: warning: incompatible implicit declaration of built-in function ‘scanf’
/tmp/ccktGXmy.o: In function `main':
compile_error.c:(.text+0x3b): undefined reference to `exp'
compile_error.c:(.text+0x52): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status

These errors stem from 2 mistakes

  1. Something is wrong in the program source code
  2. Something is wrong with the way the code is being compiled

Describe how to fix both these problems.

3 Erroneous Program

A specification for a program reads as follows.

  • Prompt the user to enter numbers
  • Run an input loop that repeatedly reads an integer input from the user
  • Count how many odd inputs and how many even inputs the user enters
  • If the user enter the number -1 (negative 1), stop the loop and print the counts of how many odd and even numbers have been entered.

Several versions of this program are given below and all have problems with them. Going from Version 1 to Version 2 fixes one problem, to Version 3 fixes another, but all are incomplete. Determine all the errors and produce a completed program that works correctly. The interactive sessions below the program source code may prove useful in diagnosing the problems with this code.

Note: In an exam setting, only one version would be given and all errors should be identified and corrected.

Version 1

 1: #include <stdio.h>
 2: /* Count eve and odd entries */
 3: int main(){
 4:   printf("Enter numbers, -1 to exit loop:\n");
 5:   int evens=0, odds=0;
 6:   while(1){
 7:     int x;
 8:     scanf("%lf",&x);
 9:     if(x==-1){
10:       break;
11:     }
12:     else if(x % 2 == 0){
13:       evens++;
14:     }
15:     else{
16:       odds++;
17:     }
18:   }
19:   printf("%d evens entered\n%d odds entered\n");
20:   return 0;
21: }
lila [exam1-practice-code]% gcc odds_evens_v1.c
lila [exam1-practice-code]% a.out
Enter numbers, -1 to exit loop:
2
Segmentation fault (core dumped)
lila [exam1-practice-code]% a.out
Enter numbers, -1 to exit loop:
-1
Segmentation fault (core dumped)
lila [exam1-practice-code]% a.out
Enter numbers, -1 to exit loop:
13
Segmentation fault (core dumped)

Version 2 (on error fixed)

 1: #include <stdio.h>
 2: /* Count eve and odd entries */
 3: int main(){
 4:   printf("Enter numbers, -1 to exit loop:\n");
 5:   int evens=0, odds=0;
 6:   while(1){
 7:     int x;
 8:     scanf("%lf",&x);
 9:     if(x==-1){
10:       break;
11:     }
12:     else if(x % 2 == 0){
13:       evens++;
14:     }
15:     else{
16:       odds++;
17:     }
18:   }
19:   printf("%d evens entered\n%d odds entered\n");
20:   return 0;
21: }
lila [exam1-practice-code]% gcc odds_evens_v2.c 
lila [exam1-practice-code]% a.out
Enter numbers, -1 to exit loop:
2
3
4
5
1
13
-1
-1
-1
WTF^M?!?
  C-c C-c
lila [exam1-practice-code]%

Version 3 (two errors fixed, one remaining)

 1: #include <stdio.h>
 2: /* Count eve and odd entries */
 3: int main(){
 4:   printf("Enter numbers, -1 to exit loop:\n");
 5:   int evens=0, odds=0;
 6:   while(1){
 7:     int x;
 8:     scanf("%d",&x);
 9:     if(x==-1){
10:       break;
11:     }
12:     else if(x % 2 == 0){
13:       evens++;
14:     }
15:     else{
16:       odds++;
17:     }
18:   }
19:   printf("%d evens entered\n%d odds entered\n");
20:   return 0;
21: }
lila [exam1-practice-code]% gcc odds_evens_v3.c 
lila [exam1-practice-code]% a.out
Enter numbers, -1 to exit loop:
1
2
3
4
5
6
13
17
-1
1 evens entered
818657168 odds entered
lila [exam1-practice-code]% a.out
Enter numbers, -1 to exit loop:
12
22
-1
1 evens entered
1866635152 odds entered
lila [exam1-practice-code]% a.out
Enter numbers, -1 to exit loop:
-1
1 evens entered
-1749223536 odds entered

4 Write a program

Write a C program that does the following.

Global Array

Establishes a global array of integers called SEQ. This array has the following contents.

{ 1, 2, 3, 1, 5, -6, 11, -3, 4, 1, 7}

Function nnoneg_SEQ(N)

Write a function called nonneg_SEQ(N) which takes a single integer argument and returns an integer. The argument N is the length of the SEQ array. The function should iterate through the SEQ array and count how many number in it are not negative. Once this count has been determined, return it.

Function main()

Write a main() function that does the following.

  • Print the entire SEQ array.
  • Call nonneg_seq(N) to count how many elements of SEQ are not negative. Print this number.
  • Iterate through the array SEQ and change all the sign of all positive even numbers to negative signs. Numbers like 1,3 should not be changed and numbers like -6 should not be changed as they are already negative.
  • Print the entire SEQ array after the sign flips.
  • Call nonneg_seq(N) again to count the negative numbers in it. Print the count.

Sample Run

A sample run of the complete program is show below.

lila [exam1-practice-code]% gcc nonneg.c 
lila [exam1-practice-code]% a.out
SEQ: 1 2 3 1 5 -6 11 -3 4 1 7 
9 nonnegative entries
Flipping signs of positive evens
SEQ: 1 -2 3 1 5 -6 11 -3 -4 1 7 
7 nonnegative entries

My solution is 42 lines of code counting white space and closing curlies.

Note: This problem is a little longer than what one could expect on an exam but practices several skills that will be useful for the exam.


Author: Chris Kauffman (kauffman@cs.gmu.edu)
Date: 2015-06-17 Wed 12:16