Apsc 160 (Ubc)
Apsc 160 (Ubc)
Apsc 160 (Ubc)
if( *** )
printf( "A" );
printf( "B" );
printf( "B" );
What Boolean expression can be inserted in place of *** in the code segment on the right
so that these two code segments produce exactly the same output for every possible value of
num1 and num2?
( num1 > 9 && num2 < 12 )
Q1B. What value should be used in place of *** in the code segment below so that count has the
value 20 when the code segment has executed?
int count = 0;
int indexA = 0;
int indexB;
while( indexA < *** )
{
indexB = 0;
while( indexB < 5 )
{
count++;
indexB++;
}
indexA++;
}
4
num2 > 7 )
);
);
);
What is printed on the screen when this code segment runs assuming that num1 and num2
are variables of type int and that num1 has the value 4 and num2 has the value 8?
AC
Page 2 of 5
Q1E. Consider the following code segment:
int count = 1;
int sum = 0;
while( count < 6 && sum < 7 )
{
sum += count;
count++;
}
printf( "sum = %d, count = %d\n", sum, count );
remainder
1
1
0
0
0
1
A( 161 ) + F ( 160 )
10( 16 ) + 15( 1 )
160 + 15
17510
Page 3 of 5
Section 3: Explain in Plain English
Q3A. Consider the following code segment. Assume that b is greater than a.
int retVal = 0;
int next = a;
while( next <= b )
{
retVal += next;
next++;
}
Explain in plain English the value that will be stored in retVal when this code segment
has executed.
The value stored in retVal will be the sum of the integers from a
to b, including a and b.
Q3B. Explain in plain English (in no more than one or two sentences) the purpose of the
following code segment, assuming that a, b and c are variables of type int. For example,
you might answer "Prints the average of the values a, b and c." This is not what the code
does but it gives you an example of the kind of answer that's expected.
if( a < c && b < c )
printf( "%d", c );
else if( a < b )
printf( "%d", b );
else
printf( "%d", a );
Page 4 of 5
Section 4: Flowcharts
Q4A.
Page 5 of 5
Section 5: Writing a complete program
Q5A.
repeatedly prompts the user for his/her age until an age between 1 and 125 inclusive
(i.e., including 1 and 125) is entered. If the user enters an age that is not valid (i.e.,
not in the specified range), your program must print an error message before
prompting for another value.
prints a message on the screen to indicate the cost of a ticket (to 2 decimal places)
for a tour bus as indicated in the table below:
age of tourist
child - under 18
adult 18 to 64 inclusive
senior 65 or older
cost of ticket
$13.45
$21.95
$18.95
Important notes: In the interests of time, it is not necessary to include any comment
statements in the code that you write.
#include <stdio.h>
#define
#define
#define
#define
#define
#define
#define
MIN_AGE 1
MAX_AGE 125
ADULT 18
SENIOR 65
CHILD_FARE 13.45
ADULT_FARE 21.95
SENIOR_FARE 18.95