The questions below are meant to stimulate the preparation of the final exam. They cover some of the most important but not all topics in the course. You must review all course materials: textbook, exercises, lecture notes, quizzes, and exams.
char random_letter ( void );
/* returns a randomly generated lower case letter */
Give a definition of random_letter.
Answer:
char random_letter ( void )
{
char c = rand()%26;
return c + 'a';
}
Describe the design of a program that would simulate this ATM machine. Sketch the organization of the flow of the program and give prototypes of the most important functions in the program. Do NOT write definitions of functions, just use comments to specify their input and output.
Answer:
The pseudo code description for this program could go as follows:
initialize checking and saving balance;
do
{
choice = display_menu();
switch(choice)
{
case 0 : printf("bye\n"); break;
case 1 : show_balance(checking,saving); break;
case 2 : withdraw(checking,saving); break;
case 3 : transfer(checking,saving); break;
default : printf("invalid option\n");
}
} while (choice != 0);
The prototypes for the four main functions are:
int display_menu();
/* displays the menu and prompts user for choice */
void show_balance ( float checking, float saving );
/* shows balance of the accounts */
void withdraw ( float *checking, float *saving );
/* allows to withdraw money from the accounts */
void transfer ( float *checking, float *saving );
/* allows to transfer money between the accounts */
void replace ( char c1, char c2 );
/* reads characters from input until EOF is reached,
copies the symbols to the output, except that every
occurrence of character c1 is replaced by c2 */
Give two definitions: once using only getchar and putchar
and once using only scanf and printf.
Answer with getchar and putchar:
void replace ( char c1, char c2 )
{
int c;
while ((c = getchar()) != EOF)
if (c == c1)
putchar(c2);
else
putchar(c);
}
Answer with scanf and printf:
void replace ( char c1, char c2 )
{
char c;
while (scanf("%c", &c) > 0)
{
if (c == c1)
printf("%c", c2);
else
printf("%c", c);
}
}
double weighted_sum ( int n, int a[] );
/* returns the sum of a[i]/(i+1) */
Answer:
double weighted_sum ( int n, int a[] )
{
double sum = 0.0;
int i;
for (i = 0; i < n; i++)
sum += ((double) a[i])/(i+1);
return sum;
}
char grade ( curve s ); /* returns the representation of s as a character */
curve assign_grade ( float p );
/* returns the corresponding grade with the percentage p,
as follows : p >= A : A; A > p >= B : B; B > p >= C : C;
C > p >= D : D; and p < D : E */
The function assign_grade must adapt itself to the changing
values of A, B, C, D, and E, without changing its definition.
Answer:
enum curve { A=90, B=80, C=70, D=60, E=50 };
typedef enum curve curve;
void read ( curve *s );
/* reads a value of the curve into s */
void write ( curve s );
/* writes a string corresponding to the value of the curve */
char grade ( curve s );
/* returns the representation of s as a character */
curve assign_grade ( float p );
/* returns the corresponding grade with the percentage p,
as follows : p >= A : A; A > p >= B : B; B > p >= C : C;
C > p >= D : D; and p < D : E */
#include<stdio.h>
int main ( void )
{
curve s;
float score;
read(&s);
write(s);
printf("The letter grade : %c.\n", grade(s));
printf("Give a score : "); scanf("%f", &score);
write(assign_grade(score));
return 0;
}
void read ( curve *s )
{
int level;
printf("Give 90, 80, 70, 60, or 50 : ");
scanf("%d", &level);
switch(level)
{
case 50: *s = E; break;
case 60: *s = D; break;
case 70: *s = C; break;
case 80: *s = B; break;
case 90: *s = A; break;
default: printf("illegal value\n");
}
}
void write ( curve s )
{
printf("Your grade is ");
switch(s)
{
case A: printf("A.\n"); break;
case B: printf("B.\n"); break;
case C: printf("C.\n"); break;
case D: printf("D.\n"); break;
case E: printf("E.\n"); break;
default: printf("Unknown.\n");
}
}
char grade ( curve s )
{
switch(s)
{
case A: return 'A';
case B: return 'B';
case C: return 'C';
case D: return 'D';
case E: return 'E';
default: return ' ';
}
}
curve assign_grade ( float p )
/* returns the corresponding grade with the percentage p,
as follows : p >= A : A; A > p >= B : B; B > p >= C : C;
C > p >= D : D; and p < D : E */
{
if (p >= A)
return A;
else if (p >= B)
return B;
else if (p >= C)
return C;
else if (p >= D)
return D;
else
return E;
}
void f ( float *p );
#include<stdio.h>
int main(void) void f ( float *p )
{ {
float x = 3.0; static int i = 0;
f(&x); static float x = 1.0;
f(&x); float y = (i==0)? 0 : x * *p;
printf("%f", x); i++; x = *p;
return 0; *p = y + 1.0;
} }
Make a diagram representing all variables in main and f.
Illustrate with the diagram what happens before, during, and after each
call of f(&x). What is printed?
Answer:
variables
---------------------------------------------------------------
in main | in f
===============================================================
before x | i x
first +-----+ | +---+ +-----+
call | 3.0 | | | 0 | | 1.0 |
+-----+ | +---+ +-----+
|
during x | p i x y
first +-----+ | +-----+ +---+ +-----+ +-----+
call | 3.0 | <------|- &x | | 1 | | 3.0 | | 0.0 |
+-----+ | +-----+ +---+ +-----+ +-----+
|
during x | p i x y
second +-----+ | +-----+ +---+ +-----+ +-----+
call | 1.0 |<-------|- &x | | 2 | | 1.0 | | 3.0 |
+-----+ | +-----+ +---+ +-----+ +-----+
|
after x | i x
second +-----+ | +---+ +-----+
call | 4.0 | | | 2 | | 1.0 |
+-----+ | +---+ +-----+
|
So, 4 is printed.
FINAL EXAM is in Lecture Center C6 on Monday 3 December 2001 from 1:00 till 3:00PM.
Please let me know as soon as possible if you cannot attend the exam and need to schedule a makeup.
Incomplete grades are only given in exceptional cases. At least the two following conditions must be satisfied: the student is in good standing (has decent scores on midterm exams, projects, and quizzes) and the student is unable to take an exam during the finals week.