int value ( char c );
/* returns 26, 25, .. , 1 if c equals respectively A, B, .. , Z;
returns 0 for any character that is not a capital letter */
Give the definition of the function value :
int value ( char c )
{
if ((c >= 'A') && (c <= 'Z'))
return 'Z' - c + 1;
else
return 0;
}
+--------+
| /10 |
+--------+
#include<stdio.h>
int main ( void )
{
int c;
while ((c = getchar()) != EOF)
if (c == 13)
putchar('\n');
else
putchar(c);
return 0;
}
+--------+
| /15 |
+--------+
double value ( int q, int d, int n, int p );
/* returns the value of the given number of coins, where
q = number of quarters (value is $0.25 apiece),
d = number of dimes (value is $0.10 apiece),
n = number of nickels (value is $0.05 apiece), and
p = number of pennies (value is $0.01 apiece). */
Give the definition of the function value :
double value ( int q, int d, int n, int p )
{
double val;
val = q*0.25l + d*0.10l + n*0.05l + p*0.01l;
return val;
}
+--------+
| /10 |
+--------+
void write_octal ( int n );
/* writes the integer n in the octal number system,
starting with the least significant digit (thus backwards) */
void write_octal ( int n )
{
int wn;
for (wn = n; wn > 0; wn /= 8)
printf("%d", wn % 8);
}
+--------+
| /15 |
+--------+
{ morning, noon, afternoon, evening, night }.
In a 24 hour day, the morning starts at 6 (= 6AM),
noon at 11 (= 11AM), the afternoon at 13 (= 1PM), the evening
at 19 (= 7PM), and the night at 21 (= 9PM).
enum day24 { morning=6, noon=11, afternoon=13, evening=19, night=21 };
typedef enum day24 day24;
void read ( day24 *dt );
/* scans a floating point number t, using the convention that
for 6 <= t < 11, we call this time of the day morning;
for 11 <= t < 13, noon; for 13 <= t < 19, afternoon;
for 19 <= t < 21, evening, and otherwise we call it night. */
Give the definition of read below :
void read ( day24 *dt )
{
float t;
scanf("%f", &t);
if ((t < 6) && (t > 21)) *dt = night;
else if (t < 11) *dt = morning;
else if (t < 13) *dt = noon;
else if (t < 19) *dt = afternoon;
else *dt = evening;
}
+--------+
| /25 |
+--------+
void R ( int *p );
#include<stdio.h>
int main(void) void R ( int *p )
{ {
int a = 3; static int cnt = 0;
R(&a); int i = *p * ++cnt;
R(&a); *p += i;
printf("%d", a); }
return 0;
}
Make a diagram representing all variables in main and R.
Illustrate with the diagram what happens before, during, and after each
call of R(&a). What is printed?
variables
--------------------------------------------------------
in main | in R
========================================================
before a | cnt
first +----+ | +---+
call | 3 | | | 0 |
+----+ | +---+
|
during a | p cnt i
first +----+ | +-----+ +---+ +----+
call | 3 |<--------|- &a | | 1 | | 3 |
+----+ | +-----+ +---+ +----+
|
during a | p cnt i
second +----+ | +-----+ +---+ +----+
call | 6 |<--------|- &a | | 2 | | 12 |
+----+ | +-----+ +---+ +----+
|
after a | cnt
second +----+ | +---+
call | 18 | | | 2 |
+----+ | +---+
|
18 is printed
+--------+
| /25 |
+--------+
FINAL EXAM is in Lecture Center C6
on Monday 3 December 2001 from 1:00 till 3:00PM.