C 2017 4 1
1. 2. while 3. 4. 5. for 6. 2/161 C
7. 8. (do while) 9. 10. (nested loop) 11. 12. 3/161 C
1.
I 1 // summing.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 long num; 6 long sum = 0L; 7 int status; 8 printf("enter an integer to be summed"); 9 printf("(q to quit): "); 10 status = scanf("%ld", &num); 11 while (status == 1) { 5/161 C
II 12 sum = sum + num; 13 printf("enter next integer(q to quit): "); 14 status = scanf("%ld", &num); 15 } 16 printf("those integers sum to %ld.\n", sum); 17 return 0; 18 } 6/161 C
Enter an integer to be summed(q to quit): 22 Enter next integer(q to quit): 33 Enter next integer(q to quit): 44 Enter next integer(q to quit): q Those integers sum to 99. 7/161 C
2. while
while while (condition) statement while (condition) { statements } 9/161 C
while no go to next statement { } condition yes statements; 10/161 C
while while while 11/161 C
while while index = 1; while (index < 5) { printf("good morning!\n"); } 12/161 C
while while index = 1; while (index < 5) { printf("good morning!\n"); } index 12/161 C
while while index = 1; while (--index < 5) { printf("good morning!\n"); } 13/161 C
while while index = 1; while (--index < 5) { printf("good morning!\n"); } index 13/161 C
while while index = 1; while (++index < 5) { printf("good morning!\n"); } 14/161 C
while while index = 1; while (++index < 5) { printf("good morning!\n"); } 14/161 C
while 15/161 C
while 1 // when.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int n = 5; 6 while (n < 7) { 7 printf("n = %d\n", n); 8 n++; 9 printf("now n = %d\n", n); 10 } 11 return 0; 12 } n = 5 Now n = 6 n = 6 Now n = 7 16/161 C
while while while 17/161 C
while index = 10; while (index++ < 5) printf("have a fair day or better.\n"); 18/161 C
while index = 10; while (index++ < 5) printf("have a fair day or better.\n"); index = 3; 18/161 C
while while 19/161 C
while 1 // while1.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int n = 0; 6 while (n < 3) 7 printf("n = %d\n", n); 8 n++; 9 printf("that s all this" 10 " program does.\n"); 11 return 0; 12 } 20/161 C
while 1 // while1.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int n = 0; 6 while (n < 3) 7 printf("n = %d\n", n); 8 n++; 9 printf("that s all this" 10 " program does.\n"); 11 return 0; 12 } n = 0 n = 0 n = 0 n = 0... 20/161 C
while while while 21/161 C
while 1 // while2.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int n = 0; 6 while (n++ < 3); 7 printf("n = %d\n", n); 8 printf("that s all this program does.\n"); 9 return 0; 10 } 22/161 C
while 1 // while2.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int n = 0; 6 while (n++ < 3); 7 printf("n = %d\n", n); 8 printf("that s all this program does.\n"); 9 return 0; 10 } n = 4 That s all this program does. 22/161 C
while C (null statement) 23/161 C
while while 24/161 C
while while while(scanf("%d",&num)==1) ; 24/161 C
while while while(scanf("%d",&num)==1) ; while 24/161 C
3.
: < <= > >= ==!= 26/161 C
while C 27/161 C
while(number < 6) { printf("your number is too small.\n"); scanf("%d", &number); } while(ch!= * ) { count++; scanf("%c", &ch); } while(scanf("%f", &num) == 1) sum = sum + num; 28/161 C
29/161 C
< > 3 1 == 1.0 3 6 1/3 0.999 999 30/161 C
math.h fabs 31/161 C
I 1 // cmpflt.c: 2 #include <stdio.h> 3 #include <math.h> 4 int main(void) 5 { 6 const double PI = 3.14159; 7 double response; 8 printf("what is the value of pi?\n"); 9 scanf("%lf", &response); 10 while (fabs(response-pi)>0.0001) { 11 printf("try again!\n"); 32/161 C
II 12 scanf("%lf", &response); 13 } 14 printf("close enough!\n"); 15 return 0; 16 } 33/161 C
What is the value of pi? 3.14 Try again! 3.1416 Close enough! 34/161 C
1 // t_and_f.c 2 #include <stdio.h> 3 int main(void) 4 { 5 int true_val, false_val; 6 true_val = (10 > 2); 7 false_val = (10 == 2); 8 printf("true = %d; false = %d\n", 9 true_val, false_val); 10 return 0; 11 } 35/161 C
true = 1; false = 0 35/161 C 1 // t_and_f.c 2 #include <stdio.h> 3 int main(void) 4 { 5 int true_val, false_val; 6 true_val = (10 > 2); 7 false_val = (10 == 2); 8 printf("true = %d; false = %d\n", 9 true_val, false_val); 10 return 0; 11 }
C 1 0 36/161 C
while (1) { }... 37/161 C
while (1) { }... 37/161 C
1 0 while 38/161 C
I 1 // truth.c 2 #include <stdio.h> 3 int main(void) 4 { 5 int n = 3; 6 while (n) 7 printf("%2d is true\n", 8 n--); 9 printf("%2d is false\n", n); 10 n = -3; 11 while (n) 39/161 C
II 12 printf("%2d is true\n", 13 n++); 14 printf("%2d is false\n", n); 15 return 0; 16 } 40/161 C
3 is true 2 is true 1 is true 0 is false -3 is true -2 is true -1 is true 0 is false 41/161 C
C 0 42/161 C
while (n!= 0) { }... while (n) { }... 43/161 C
trouble.c // trouble.c: #include <stdio.h> int main(void) { long num; long sum = 0L; 44/161 C
I 1 // trouble.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 long num; 6 long sum = 0L; 7 int status; 8 9 printf("enter an integer to be summed "); 10 printf("(q to quit): \n"); 11 status = scanf("%ld", &num); 45/161 C
II 12 while (status = 1) { 13 sum = sum + num; 14 printf("enter next integer (q to quit):\n"); 15 status = scanf("%ld", &num); 16 } 17 printf("those integers sum to %ld.\n", sum); 18 19 return 0; 20 } 46/161 C
Enter an integer to be summed (q to quit): 1 Enter next integer (q to quit): 2 Enter next integer (q to quit): 3 Enter next integer (q to quit): q Enter next integer (q to quit): Enter next integer (q to quit): Enter next integer (q to quit):... 47/161 C
while status=1 status==1 status=1 1 while (status = 1) while (1) 48/161 C
q scanf scanf q q q scanf 49/161 C
== = = == i = 5 // i 5 i == 5 // i 5 == 5 = i // 5 == i // i 5 50/161 C
1 0 while if 51/161 C
_Bool / C int C99 _Bool 1 0 1 C 52/161 C
_Bool I 1 // boolean.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 long num; 6 long sum = 0L; 7 _Bool input_is_good; 8 9 printf("enter an integer to be summed"); 10 printf(" (q to quit):\n"); 11 53/161 C
_Bool II 12 input_is_good = (scanf("%ld", &num) == 1); 13 while (input_is_good) { 14 sum = sum + num; 15 printf("enter next integer (q to quit):\n"); 16 input_is_good = (scanf("%ld", &num) == 1); 17 } 18 printf("those integers sum to %ld.\n", sum); 19 20 return 0; 21 } 54/161 C
_Bool input_is_good = (scanf("%ld", &num) == 1); == == = while (input_is_good) 55/161 C
_Bool C99 stdbool.h bool _Bool true false 1 0 56/161 C
_Bool I 1 // boolean1.c: 2 #include <stdio.h> 3 #include <stdbool.h> 4 int main(void) 5 { 6 long num; 7 long sum = 0L; 8 bool input_is_good; 9 10 printf("enter an integer to be summed"); 11 printf(" (q to quit):\n"); 57/161 C
_Bool II 12 input_is_good = (scanf("%ld", &num) == true); 13 while (input_is_good) { 14 sum = sum + num; 15 printf("enter next integer (q to quit):\n"); 16 input_is_good = (scanf("%ld", &num) == true) ; 17 } 18 printf("those integers sum to %ld.\n", sum); 19 20 return 0; 21 } 58/161 C
+ - x > y+2 x > (y+2) x = y > 2 x = (y > 2) x_bigger = x > y x_bigger = (x > y) 59/161 C
: < <= > >= ==!= 60/161 C
c!= a == b (c!= a) == b 61/161 C
( ) - + ++ - sizeof (type) * / % + - < <= > >= ==!= = 62/161 C
4.
64/161 C
1 // sweetie1.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 const int NUMBER = 2; 6 int count = 1; 7 while (count <= NUMBER) { 8 printf("hello world!\n"); 9 count++; 10 } 11 return 0; 12 } 65/161 C
66/161 C
while (count <= NUMBER){... count++; } = while (count++ <= NUMBER){ }... 67/161 C
5. for
for for (initialization; condition; increment) { } statements for (initialization; condition; increment) statement for 69/161 C
for 1 // sweetie2.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 const int NUMBER = 4; 6 int count; 7 8 for (count = 0; count < NUMBER; count++) 9 printf("hello world!\n"); 10 11 return 0; 12 } 70/161 C
for Hello world! Hello world! Hello world! Hello world! 71/161 C
for for 1. for 2. 3. 72/161 C
for 1 #include <stdio.h> 2 int main(void) 3 { 4 int num; 5 printf("%3s %8s\n", "n", "n cubed"); 6 for (num = 1; num <= 4; num++) 7 printf("%3d %8d\n", num, num*num*num); 8 return 0; 9 } 73/161 C
for n n cubed 1 1 2 8 3 27 4 64 74/161 C
for for 1 1 // for_down.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int secs; 6 for (secs = 4; secs > 0; secs--) 7 printf("%d seconds!\n", secs); 8 printf("ignition!\n"); 9 return 0; 10 } 75/161 C
for for 4 seconds! 3 seconds! 2 seconds! 1 seconds! Ignition! 76/161 C
for for 2 2 10 1 // for_13s.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int n; 6 for (n = 2; n < 60; n = n+13) 7 printf("%d\n", n); 8 return 0; 9 } 77/161 C
for for 2 15 28 41 54 78/161 C
for for 3 1 // for_char.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 char ch; 6 for (ch = a ; ch <= z ; ch++) 7 printf("the ASCII value of %c is %d\n", ch, ch); 8 return 0; 9 } 79/161 C
for for The ASCII value of a is 97 The ASCII value of b is 98... The ASCII value of y is 121 The ASCII value of z is 122 80/161 C
for for 4 1 // for_cube1.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int num; 6 printf("%3s %8s\n", "n", "n cubed"); 7 for (num = 1; num*num*num <= 64; num++) 8 printf("%3d %8d\n", num, num*num*num); 9 return 0; 10 } 81/161 C
for for n n cubed 1 1 2 8 3 27 4 64 82/161 C
for for 5 1 // for_geo.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 double debt; 6 for (debt = 100.0; debt <= 140; debt=debt*1.1) 7 printf("your debt is now $%.2f\n", debt); 8 return 0; 9 } 83/161 C
for for Your debt is now $100.00 Your debt is now $110.00 Your debt is now $121.00 Your debt is now $133.10 84/161 C
for for 6 1 // for_wild.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int x; 6 int y = 55; 7 for (x = 1; y <= 75; y = (++x * 5) + 50) 8 printf("%10d %10d\n", x, y); 9 return 0; 10 } 85/161 C
for for 1 55 2 60 3 65 4 70 5 75 86/161 C
for for 7 87/161 C
for for 1 // for_none.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int ans, n; 6 ans = 2; 7 for (n = 3; ans <= 25; ) 8 ans = ans * n; 9 printf("n = %d; ans = %d.\n", n, ans); 10 return 0; 11 } 88/161 C
for for n = 3; ans = 54. 89/161 C
for for for ( ; ; ) printf("i want som action\n"); 90/161 C
for for 8 printf 91/161 C
for for 1 // for_show.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int num; 6 for (printf("keep entering numbers!\n"); 7 num!=6; ) 8 scanf("%d", &num); 9 printf("that s the one I want!\n"); 10 return 0; 11 } 92/161 C
for for Keep entering numbers! 2 6 That s the one I want! 93/161 C
for for 9 for (n = 1; n < 10000; n = n + delta) delta if delta 94/161 C
6.
+= -= *= /= %= 96/161 C
num += 20 num -= 20 num /= 20 num *= 20 num %= 20 num = num + 20 num = num - 20 num = num / 20 num = num * 20 num = num % 20 97/161 C
num += 20 num -= 20 num /= 20 num *= 20 num %= 20 num = num + 20 num = num - 20 num = num / 20 num = num * 20 num = num % 20 x *= 3 * y + 12 x = x * (3 * y + 12) 97/161 C
= xxxxyyyyzzzz *= 3 xxxxyyyyzzzz = xxxxyyyyzzzz * 3 98/161 C
7.
for for 100/161 C
1 37 1 23 101/161 C
1 // postage.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 const int FIRST_OZ = 37, NEXT_OZ = 23; 6 int ounces, cost; 7 for (ounces = 1, cost = FIRST_OZ; 8 ounces <= 16; 9 ounces++, cost += NEXT_OZ) 10 printf("%2d $%4.2f\n", ounces, cost/100.0); 11 return 0; 12 } 102/161 C
1 $0.37 2 $0.60 3 $0.83... 103/161 C
ounces = 1, cost = FIRST_OZ; no ounces <= 16 ounces++, cost += NEXT_OZ yes printf("..."); 104/161 C
for for 105/161 C
ounces++, cost = ounces * FIRST_OZ 106/161 C
x = (y = 3, (z = ++y + 2) + 5); (1) y = 3; (2) y = y + 1 = 4; (3) z = (y + 2) = (4 + 2) = 6; (4) x = z + 5 = 6 + 5 = 11; 107/161 C
houseprice = 249,500; houseprice = 249; 500; 108/161 C
houseprice = 249,500; houseprice = 249; 500; C houseprice = 249 500 500 houseprice 249 108/161 C
houseprice = (249,500); 109/161 C
houseprice = (249,500); 500 houseprice 109/161 C
int m, n; printf("%d %d\n", m, n); 110/161 C
int m, n; printf("%d %d\n", m, n); 110/161 C
S = 1 + 1 2 + 1 4 + 1 8 + 1 16 + 111/161 C
I 1 // zeno.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 int count, limit; 6 double sum, x; 7 printf("enter the number of terms: "); 8 scanf("%d", &limit); 9 for (sum = 0., x = 1, count = 1; 10 count <= limit; 11 count++, x *= 2.) { 112/161 C
II 12 sum += 1.0/x; 13 printf("sum = %f when terms = %d.\n", 14 sum, count); 15 } 16 return 0; 17 } 113/161 C
Enter the number of terms you want: 10 sum = 1.000000 when terms = 1. sum = 1.500000 when terms = 2. sum = 1.750000 when terms = 3. sum = 1.875000 when terms = 4. sum = 1.937500 when terms = 5. sum = 1.968750 when terms = 6. sum = 1.984375 when terms = 7. sum = 1.992188 when terms = 8. sum = 1.996094 when terms = 9. sum = 1.998047 when terms = 10. 114/161 C
8. (do while)
(do while) while for do while 116/161 C
(do while) I 1 // do_while.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 const int SECRET_CODE = 13; 6 int code_entered; 7 do { 8 printf("to withdraw money from ATM. \n"); 9 printf("enter the secret code number: "); 10 scanf("%d", &code_entered); 11 } while (code_entered!= SECRET_CODE); 117/161 C
(do while) II 12 printf("congratulations! You are permitted!\n" ); 13 return 0; 14 } 118/161 C
(do while) To withdraw money from ATM. Please enter the secret code number: 11 To withdraw money from ATM. Please enter the secret code number: 12 To withdraw money from ATM. Please enter the secret code number: 13 Congratulations! You are permitted! 119/161 C
(do while) I while 1 // entry.c: 2 #include <stdio.h> 3 int main(void) 4 { 5 const int SECRET_CODE = 13; 6 int code_entered; 7 printf("to withdraw money from ATM. \n"); 8 printf("enter the secret code number: "); 9 scanf("%d", &code_entered); 10 while (code_entered!= SECRET_CODE) { 120/161 C
(do while) II 11 printf("to withdraw money from ATM. \n"); 12 printf("enter the secret code number: "); 13 scanf("%d", &code_entered); 14 } 15 printf("congratulations! You are permitted!\n" ); 16 return 0; 17 } 121/161 C
(do while) do printf("ba la ba la!"); while no go to next statement count++ <= limit; yes 122/161 C
(do while) do { statements } while (condition); do statement while (condition); 123/161 C
(do while) do while do while 124/161 C
9.
126/161 C
for while for (; condition; ) while (condition) 127/161 C
while for initialization; while (condition) { body; update; } for (initialization; condition; update){ body; } 128/161 C
for while while while (scanf("%ld", &num) == 1){... } for for (count = 1; count <= 100; count++){... } 129/161 C
10. (nested loop)
(nested loop) 131/161 C
(nested loop) I 1 // rows1.c: 2 #include <stdio.h> 3 #define ROWS 6 4 #define CHARS 10 5 int main(void) 6 { 7 int row; 8 char ch; 9 for (row = 0; row < ROWS; row++) { 10 for (ch = A ; ch < ( A + CHARS); ch++) 11 printf("%c ", ch); 132/161 C
(nested loop) II 12 printf("\n"); 13 } 14 return 0; 15 } 133/161 C
(nested loop) A B C D E F G H I J A B C D E F G H I J A B C D E F G H I J A B C D E F G H I J A B C D E F G H I J A B C D E F G H I J 134/161 C
(nested loop) I 1 // row2.c: 2 #include <stdio.h> 3 #define ROWS 6 4 #define CHARS 6 5 int main(void) 6 { 7 int row; 8 char ch; 9 for (row = 0; row < ROWS; row++) { 10 for (ch = A + row; ch < ( A + CHARS); ch ++) 135/161 C
(nested loop) II 11 printf("%c ", ch); 12 printf("\n"); 13 } 14 return 0; 15 } 136/161 C
(nested loop) A B C D E F B C D E F C D E F D E F E F F 137/161 C
11.
139/161 C
float debts[20]; debts 20 float debts[0] debts[1]... float debts[3] = 2.; debts[7] = 1.2e+10; 140/161 C
scanf("%d", &debts[4]); // 5 141/161 C
C debts[20] = 11.0; // debts[31] = 22.22; // 142/161 C
int num[10]; // 10 char ch[20]; // 20 double a[40]; // 40 double 143/161 C
(subscript) (index) (offset) 0 144/161 C
for 5 145/161 C
I 1 // scores.c: 2 #include <stdio.h> 3 #define DAYS 5 4 #define PAR 10000 5 int main(void) 6 { 7 int index, steps[days]; 8 int sum = 0; 9 float average; 10 printf("enter steps of %d days:\n", DAYS); 11 for (index = 0; index < DAYS; index++) 146/161 C
II 12 scanf("%d", &steps[index]); 13 printf("the steps read in are as follows:\n"); 14 for (index = 0; index < DAYS; index++) 15 printf("%7d", steps[index]); 16 printf("\n"); 17 for (index = 0; index < DAYS; index++) 18 sum += steps[index]; 19 average = (float) sum / DAYS; 20 printf("sum of steps = %d, average = %.2f\n", sum, average); 21 printf("that s a handicap of %.0f.\n", average -PAR); 147/161 C
III 22 return 0; 23 } 148/161 C
Enter steps of 5 days: 9999 9995 11000 12012 11145 95667 The steps read in are as follows: 9999 9995 11000 12012 11145 Sum of steps = 54151, average = 10830.20 That s a handicap of 830. 149/161 C
12.
n p 151/161 C
n p math.h pow 151/161 C
pow 1 // pow.c: 2 #include <stdio.h> 3 #include <math.h> 4 int main(void) 5 { 6 printf(" 2^3 = %f\n", pow(2.0,3.0)); 7 printf(" sqrt 2 = %f\n", pow(2.0,0.5)); 8 printf(" 3^(1/4) = %f\n", pow(3.0,0.25)); 9 return 0; 10 } 152/161 C
pow 2^3 = 8.000000 sqrt 2 = 1.414214 3^(1/4) = 1.316074 153/161 C
1 double power(double n, int p) 2 { 3 double pow = 1; 4 int i; 5 6 for (i = 1; i <= p; i++) 7 pow *= n; 8 9 return pow; 10 } 154/161 C
return 155/161 C
return return 2 * x + b; 156/161 C
b = power(1.2, 3); b = 2.0 + power(1.2, 3); printf("%f", power(1.2, 3)); 157/161 C
power I 1 // power.c: 2 #include <stdio.h> 3 double power(double n, int p); // function prototype 4 int main(void) 5 { 6 double x, xpow; 7 int exp; 8 printf("enter a number and the positive integer power\n"); 158/161 C
power II 9 printf("to which\nthe number will be raised. Enter q"); 10 printf("to quit.\n"); 11 while (scanf("%lf%d", &x, &exp) == 2) { 12 xpow = power(x, exp); // function call 13 printf("%.3g to the power %d is %.5g\n", 14 x, exp, xpow); 15 printf("enter next pair of numbers or q to quit.\n"); 16 } 17 printf("hope you enjoyed this power trip -- bye!\n"); 159/161 C
power III 18 return 0; 19 } 20 double power(double n, int p) // function definition 21 { 22 double pow = 1; 23 int i; 24 for (i = 1; i <= p; i++) 25 pow *= n; 26 return pow; 27 } 160/161 C
power Enter a number and the positive integer power to which the number will be raised. Enter qto quit. 1.2 12 1.2 to the power 12 is 8.9161 Enter next pair of numbers or q to quit. 2 16 2 to the power 16 is 65536 Enter next pair of numbers or q to quit. q 161/161 C