Note: Answers are in red.
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)
#include <stdio.h>
#include <math.h>
int main() {
int n;
scanf("%i", &n);
printf("n: %i, square root of n: %.8f\n", n, sqrt(n));
}
Two runs:
2
n: 2, square root of n: 1.41421356
5
n: 5, square root of n: 2.23606798
#include <stdio.h>
#include <math.h>
int main() {
double temp;
scanf("%lf", &temp);
printf("A temperature of %.1f is ", temp);
if (temp <= 95) printf("LIFE-THREATENING");
else if (temp < 97.6) printf("LOW");
else if (temp <= 100.3) printf("NORMAL");
else if (temp < 107) printf("HIGH");
else printf("LIFE-THREATENING");
printf("\n");
/* second solution */
/* can have else's before each if except the first */
if (temp <= 95 || temp >= 107) printf("LIFE-THREATENING");
if (temp > 95 && temp < 97.6) printf("LOW");
if (temp >= 97.6 && temp <= 100.3) printf("NORMAL");
if (temp > 100.3 && temp < 107) printf("HIGH");
printf("\n");
}
Multiple runs:
95
A temperature of 95.0 is LIFE-THREATENING
LIFE-THREATENING
95.1
A temperature of 95.1 is LOW
LOW
97.5
A temperature of 97.5 is LOW
LOW
97.6
A temperature of 97.6 is NORMAL
NORMAL
100.3
A temperature of 100.3 is NORMAL
NORMAL
100.4
A temperature of 100.4 is HIGH
HIGH
106.9
A temperature of 106.9 is HIGH
HIGH
107
A temperature of 107.0 is LIFE-THREATENING
LIFE-THREATENING
1 4 9 16
#include <stdio.h>
int main() {
int n;
int i;
scanf("%i", &n);
for (i = 1; i <= n; i++)
printf("%i\n", i*i);
} | #include <stdio.h>
int main() {
int n, i = 1, isqr;
scanf("%i", &n);
while (i <= n) {
isqr = i*i;
printf("%i\n", isqr);
i++;
}
} |
Another run: 6 1 4 9 16 25 36 | Yet another run: 5 1 4 9 16 25 |
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 */
double area(double h, double r);
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 */
double area(double h, double r) {
return 2*PI*r*h + 2*PI*r*r;
}
Multiple runs:
1 1
Height: 1.0000, radius: 1.0000
Surface area: 12.5664
2 3
Height: 2.0000, radius: 3.0000
Surface area: 94.2478
#include <stdio.h>
void chars(int m, char ch);
int main() {
int n = 6;
/* possible extra declarations here */
int i;
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 */
for (i = 1; i <= n; i++) {
chars(i, '*');
chars(1, '\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.)
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
for (i = 0; i < 30; i++)
printf("%i ", roll());
printf("\n");
}
int roll() {
return (int)(6.0*rand()/(double)RAND_MAX + 1.0);
}
One run:
4 2 2 4 6 2 5 2 3 1 1 3 2 3 6 4 5 4 5 5 5 1 4 2 3 6 4 3 4 5
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int d;
int count = 0;
srand((long)time(NULL)); /* initialize rand() */
while (1) {
d = roll();
count++;
if (d == 6) break;
}
printf("Rolls to get a 6: %i\n", count);
}
int roll() {
return (int)(6.0*rand()/(double)RAND_MAX + 1.0);
}
Multiple runs:
Rolls to get a 6: 1
Rolls to get a 6: 9
Rolls to get a 6: 22
Rolls to get a 6: 6
Rolls to get a 6: 2
Rolls to get a 6: 2
Rolls to get a 6: 19
Rolls to get a 6: 11
Rolls to get a 6: 4
Rolls to get a 6: 2
Rolls to get a 6: 2
Rolls to get a 6: 5
Rolls to get a 6: 10
Rolls to get a 6: 9
Rolls to get a 6: 7
Rolls to get a 6: 2