Directions: Fill in answers on the pages below. Don't spend
too much time on any one problem.
Points for each problem: 1-15, 2-15, 3-20, 4-20, 5-25, 6-20, 7-20, 8-25.
Answers are in red below.
#include <stdio.h>
int main() {
double temperature;
while (1) {
scanf("%lf", &temperature);
if (temperature == 0) break;
printf("Temperature: %0.1f\n", temperature);
/* put the rest of the code below */
if (96.0 <= temperature && temperature <= 100.6)
printf("Temperature OK\n");
else
printf("You are sick\n");
/* second try */
if (temperature < 96.0 || temperature >= 100.6)
printf("You are sick\n");
else
printf("Temperature OK\n");
printf("\n");
}
}
Output:
98.6
Temperature: 98.6
Temperature OK
Temperature OK
104.2
Temperature: 104.2
You are sick
You are sick
94.0
Temperature: 94.0
You are sick
You are sick
0.0
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 10; i++) {
printf("%i squared = %i\n", i, i*i);
}
printf("\n");
i = 1;
while(i < 10) {
printf("%i squared = %i\n", i, i*i);
i++;
}
}
Output (repeated twice):
1 squared = 1
2 squared = 4
3 squared = 9
4 squared = 16
5 squared = 25
6 squared = 36
7 squared = 49
8 squared = 64
9 squared = 81
#include <stdio.h>
int main() {
int a[] = {2, 6, 4, 7, 3, 9};
int b[] = {5, 1, 3, 2, 4, 0};
int c[6];
/* give code that will let c be the */
/* coordinate-wise sum of a and b */
int i;
for (i = 0; i < 6; i++)
c[i] = a[i] + b[i];
/* give code to print the elements of c on one line, spaces between them */
for (i = 0; i < 6; i++)
printf("%i ", c[i]);
printf("\n");
}
Output:
7 7 7 9 7 9
#include <stdio.h>
/* prototype for function average here */
double average(int a[], int n);
int main() {
int n = 3;
int a[] = {90, 100, 80};
/* complete the call below */
double ave = average(a, n);
printf("Average: %8.2f\n", ave);
}
/* definition of ave function here */
double average(int a[], int n) {
int i;
double sum = 0;
for (i = 0; i < n; i++)
sum = sum + a[i];
return sum/n;
}
Output:
Average: 90.00
1 - 1/22 + 1/32 - 1/42 + 1/52 - ...
Notice that the signs are alternating. Your function should have the integer n as a formal parameter, and it should then use a loop to calculate the sum and to return that as the answer. For example, the call series(2) should return the number 1 - 1/22 = 1 - 0.25 = 0.75.
#include <stdio.h>
#include <math.h>
/* function series prototype here */
double series(int n);
int main() {
double pi = atan(1.0)*4.0;
double sum = series(100000);
printf("sum: %18.14f\n", sum);
printf("exact: %18.14f\n", pi*pi/12.0);
}
/* definition of series function here */
double series(int n) {
int i;
double sum = 0.0;
double sign = 1.0;
for (i = 1; i <= n; i++) {
sum = sum + sign*(1.0/i/i);
// or: sum = sum + sign*(1.0/((double)i*i));
// but not: sum = sum + sign*(1.0/(i*i));
// because (i*i) would overflow with i == 100000
sign = -sign;
}
return sum;
}
Output:
sum: 0.82246703337409
exact: 0.82246703342411
#include <stdio.h>
void newyear(int *p);
int main() {
int n = 2005;
newyear(&n);
printf("After call to newyear, n = %i\n", n);
}
void newyear(int *p) {
*p = 2006;
}
#include <stdio.h>
struct person {
int age; /* in years */
char sex; /* sex (M or F) */
double weight; /* in pounds */
};
int main() {
double sum_of_weights;
/* answers to 5 parts above here */
struct person george = { 37, 'M', 205};
// or:
// struct person george;
// george.age = 37; george.sex = 'M'; george.weight = 205;
struct person martha = { 33, 'F', 164};
printf("George: age: %i, sex: %c, weight: %0.1f\n",
george.age, george.sex, george.weight);
printf("Martha: age: %i, sex: %c, weight: %0.1f\n",
martha.age, martha.sex, martha.weight);
sum_of_weights = george.weight + martha.weight;
printf("Sum of weights: %0.1f\n", sum_of_weights);
}
Output:
George: age: 37, sex: M, weight: 205.0
Martha: age: 33, sex: F, weight: 164.0
Sum of weights: 369.0
| Deck of cards | Output: 1st part | Output: 2nd part |
|---|---|---|
/* 1 */ #include <stdlib.h>
/* 2 */ #include <time.h>
/* 3 */
/* 4 */ struct card { /* single card */
/* 5 */ char suit; /* suit */
/* 6 */ int value; /* value (spots) */
/* 7 */ };
/* 8 */ void printdeck(struct card deck[]);
/* 9 */
/* 10 */ int main() {
/* 11 */ // deck of 52 cards
/* 12 */ struct card deck[52];
/* 13 */ struct card temps;
/* 14 */
/* 15 */ double ran[52];
/* 16 */ int i, dum;
/* 17 */
/* 18 */
/* 19 */ // initialize deck
/* 20 */ for (i = 0; i < 52; i++) {
/* 21 */ deck[i].suit = i/13;
/* 22 */ deck[i].value = i%13 + 1;
/* 23 */ }
/* 24 */
/* 25 */ // print the deck
/* 26 */ printdeck(deck);
/* 27 */
/* 28 */ srand((long)time(NULL));
/* 29 */ for (i = 0; i < 52; i++)
/* 30 */ ran[i] = rand()/(double)RAND_MAX;
/* 31 */
/* 32 */ for (dum = 0; dum < 51; dum++)
/* 33 */ for (i = 0; i < 51; i++)
/* 34 */ if (ran[i] < ran[i+1]) {
/* 35 */ // interchange ran array elts
/* 36 */ double temp = ran[i];
/* 37 */ ran[i] = ran[i+1];
/* 38 */ ran[i+1] = temp;
/* 39 */
/* 40 */ // interchange deck array elts
/* 41 */ temps = deck[i];
/* 42 */ deck[i] = deck[i+1];
/* 43 */ deck[i+1] = temps;
/* 44 */ }
/* 45 */
/* 46 */ // print the deck
/* 47 */ printdeck(deck);
/* 48 */ }
/* 49 */
/* 50 */ void printdeck(struct card deck[]) {
/* 51 */ int i, v, s;
/* 52 */ printf("Deck of cards:\n");
/* 53 */ for(i = 0; i < 52; i++) {
/* 54 */ v = deck[i].value;
/* 55 */ if (v == 1) printf(" A");
/* 56 */ else if (v == 13) printf(" K");
/* 57 */ else if (v == 12) printf(" Q");
/* 58 */ else if (v == 11) printf(" J");
/* 59 */ else printf("%2i", deck[i].value);
/* 60 */ printf(" of ");
/* 61 */ s = deck[i].suit;
/* 62 */ if (s == 0) printf("Spades");
/* 63 */ else if (s == 1) printf("Hearts");
/* 64 */ else if (s == 2) printf("Clubs");
/* 65 */ else if (s == 3) printf("Diamonds");
/* 66 */ printf("\n");
/* 67 */ }
/* 68 */ printf("\n");
/* 69 */ }
| Deck of cards: A of Spades 2 of Spades 3 of Spades 4 of Spades 5 of Spades 6 of Spades 7 of Spades 8 of Spades 9 of Spades 10 of Spades J of Spades Q of Spades K of Spades A of Hearts 2 of Hearts 3 of Hearts 4 of Hearts 5 of Hearts 6 of Hearts 7 of Hearts 8 of Hearts 9 of Hearts 10 of Hearts J of Hearts Q of Hearts K of Hearts A of Clubs 2 of Clubs 3 of Clubs 4 of Clubs 5 of Clubs 6 of Clubs 7 of Clubs 8 of Clubs 9 of Clubs 10 of Clubs J of Clubs Q of Clubs K of Clubs A of Diamonds 2 of Diamonds 3 of Diamonds 4 of Diamonds 5 of Diamonds 6 of Diamonds 7 of Diamonds 8 of Diamonds 9 of Diamonds 10 of Diamonds J of Diamonds Q of Diamonds K of Diamonds | Deck of cards: 5 of Diamonds 6 of Diamonds 2 of Hearts 8 of Diamonds 2 of Clubs J of Spades 2 of Diamonds 10 of Hearts J of Diamonds 6 of Hearts Q of Hearts K of Diamonds 7 of Spades A of Clubs 8 of Hearts 7 of Diamonds K of Hearts 8 of Clubs K of Spades J of Hearts 9 of Clubs 4 of Hearts 6 of Spades 8 of Spades 7 of Hearts 10 of Spades 10 of Diamonds 5 of Spades 10 of Clubs A of Hearts 3 of Clubs 9 of Diamonds 6 of Clubs 2 of Spades 4 of Diamonds 3 of Spades 9 of Spades A of Spades J of Clubs 3 of Diamonds Q of Clubs 7 of Clubs 3 of Hearts 9 of Hearts 4 of Spades Q of Diamonds 4 of Clubs Q of Spades 5 of Hearts K of Clubs A of Diamonds 5 of Clubs |
The output shown is not the entire output of this program. Even if you didn't study or see this example, you still should be able to answer some of the questions below: