Apsc 160 (Ubc)
Apsc 160 (Ubc)
Apsc 160 (Ubc)
Name: _______________________
Student #:
_________________
Signature: _____________________
Instructor: _______________
1.
2.
3.
4.
5.
Question
Max
7
10
17
10
6
Total
50
1
2
3
4
Mark
Page 2 of 7
Page 3 of 7
Note:
No marks will be awarded if you simply present the output from this program. You
must include a trace table to illustrate that you understand exactly how this code segment
works.
The format specifier "%c" is used to print a single character.
Page 4 of 7
[17] Q3. Be sure to read the entire question before you write any code!
Write a complete program in C that:
1. prompts the user for the radius of the base of a cone and reads it from the keyboard
2. prompts the user for the height of a cone and reads it from the keyboard
3. computes the volume of the cone using the formula:
volume = ( 1 / 3 ) x (radius)2 x height
4. prints the volume of the cone on the screen
5. repeats steps 1-4 until the user enters 0 for the base radius of the cone
Your program must include a function that:
takes the base radius and height of a cone as its only parameters
returns the volume of the cone
Note:
if the user enters 0 for the base radius of the cone, they should not then be prompted to
enter the height of the cone!
do not assume that the base radius and the height are integers
in the interests of time, it is not necessary to provide any comment statements.
Page 5 of 7
Page 6 of 7
[10] Q4. Identify the errors in the following program by circling them, and correct the errors by crossing
out code / inserting code where appropriate. Read the comment statements carefully! Assume
that they are correct and accurately describe what the program is intended to do. Note that the
code does not contain any syntax errors!
/* This program reads from a data file information about all the animals in
* an animal shelter, and determines which ones need special attention to
* expedite their adoption.
*
* Input: the first row of the data file contains a single integer indicating
*
the number of rows that follow. Each subsequent row contains a
*
single animal's ID #, age, and number of days at the shelter. All
*
values are integers
* Output: to the screen - a list of all animal IDs corresponding to animals
*
that are more than 6 years old, and have been at the shelter for
*
more than 60 days
*/
#include <stdio.h>
#include <stdlib.h>
#define FILENAME "animals.txt"
int main(void)
{
FILE *in;
int numRecords;
int count;
int id, age, numDays;
/* open file and check if successful */
in = fopen(FILENAME, "r");
if( in = NULL )
{
printf("Error opening data file\n");
}
/* read the number of animal records */
fscanf( in, "%i", &numRecords );
printf("Animals needing critical attention:\n");
/* process all records */
while( count < numRecords )
{
/* read one record */
fscanf( in, "%i %i %i", &id, &age, &numDays );
/* if the status of the animal is critical
* (the age is more than 6, and the numDays is more than 60),
* output that animal's ID# */
if( age > 6 || numDays > 60 )
{
printf("%i\n", id);
}
}
return 0;
}
Page 7 of 7
Note that these functions contain very similar code and that neither of them takes parameters.
Replace these two functions by a single function with parameters that can perform the task of
either of the two functions above, depending on the value of the parameters that are passed.