Directions: Fill in answers on the pages below. Don't spend
too much time on any one problem. All questions below ask for a
code segment, which does not need to be a complete program.
You never need any #include statements,
or the main function,
but you should declare the variables that you use.
Points for each problem: 1-10, 2-15, 3-15, 4-15, 5-15,
6(i)-15, 6(ii)-15.
n: 2, square root of n: 1.41421356 (in case n is 2) n: 5, square root of n: 2.23606798 (in case n is 5)
1 4 9 16
area = 2*pi*r*h + 2*pi*r2
Add code to the program below, so you define a function named area, with two formal parameters h and r, that will calculate and return the area. You also need to give a prototype.
#include <stdio.h>
#define PI 3.14159265358979
/* give prototype for area function here */
int main() {
double h, r, a;
scanf("%lf %lf", &h, &r);
a = area(h, r);
printf("Height: %.4f, radius: %.4f\n", h, r);
printf("Surface area: %.4f\n", a);
}
/* definition of function area here */
#include <stdio.h>
void chars(int m, char ch);
int main() {
int n = 6;
/* possible extra declarations here */
printf("n == %i:\n", n);
/* insert code here to print a triangle as shown */
/* you must use a loop so that this works for any positive n */
}
/* chars: print a char m times */
void chars(int m, char ch) {
int i;
for(i = 0; i < m; i++)
printf("%c", ch);
}
Sample output for runs with different values for n (next page):
n == 6: * ** *** **** ***** ****** | n == 10: * ** *** **** ***** ****** ******* ******** ********* ********** | n == 1: * | n == 4: * ** *** **** |
int roll() {
return (int)(6.0*rand()/(double)RAND_MAX + 1.0);
}
(In the questions below do not worry about #include statements, or about initializing the RNG with srand.)