UMBC CMSC201, Computer Science I, Spring 1994
Sections 0101, 0102 and Honors
Quiz 2
Short Answers, 4 points each
This section cannot be graded on-line. To practice
taking the exam, jot down your answer and click
on the button after each question to see a sample solution.
The questions in this section refer to the following program:
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
/* Function Prototypes */
int power2 ( int x ) ;
int power4 ( int x ) ;
/* The function implementations */
/* This function computes 4 raised to the power of x */
int power4 (int x) {
return( power2(2*x)) ;
}
/* This function computes 2 raised to the power of x */
int power2 (int x) {
int product, i ;
product = 1 ;
for (i = 1 ; i <= x ; i++) {
product = 2 * product ;
}
return (product) ;
}
/* The main program */
main() {
int a, b ;
printf("? ") ;
a = GetInteger() ;
b = power4(a) ;
printf("4^%d = %d\n", a, b) ;
}
The Questions:
- If you want to make the functions power2 and power4
available to your clients as part of a library of functions, what
would the header file power.h contain?
(Write down the entire header file. You may leave out the comments.)
Click here for a sample solution.
- Suppose that you want to separately compile the implementation of
the functions power2 and power4 in a file
power.c. Outline the contents of the file
power.c. (You don't need to repeat the C code for the
functions.)
Click here for a sample solution.
- Now that you have the functions power2 and power4
available to you, write a complete program with a main function
which uses power2 and power4 to compute 2 cubed and
4 cubed and prints the results out.
Click here for a sample solution.
Last Modified:
Mon Sep 4 15:39:40 EDT 1995
Richard Chang, chang@gl.umbc.edu