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.
double temperature = 98.6; /* or = 104.2 or = 94.0 */ /* put the rest of the code below */
int i;
for (i = 1; i < 10; i++) {
printf("%i squared = %i\n", i, i*i);
}
#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 */
/* give code to print the elements of c on one line, spaces between them */
}
#include <stdio.h>
/* prototype for function average here */
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 */
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>
/* function series prototype here */
int main() {
double sum = series(100000);
printf("%18.14f\n", sum);
}
/* definition of series function here */
#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 */
sum_of_weights =
}
| Deck of cards | First part of output |
|---|---|
/* 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 |
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: