BASIC C EXERCISES READING ------- Notes for lecture 2. Kernighan and Ritchie, 2.10, 2.11, 3.1-3.7 King, Chapters 1 (Introducing C) 2 (C Fundamentals) 3 (printf, scanf) 4 (Expressions) 5 (Selection statements) 6 (Loops) SUPPLEMENTAL READING -------------------- Deitel and Deitel, Chapters 3 and 4 EXERCISES --------- 1. What do the following C statements do? t = x; x = y; y = t; 2. What is wrong with the following C statements? A. if (a > b) then c = 0; B. if a > b { c = 0; } C. if (a > b) c = 0; D. if (a > b) c = 0 else b = 0; 3. What is the value of j after the following code is executed? for (i = 0, j = 0; i < 10; i++) j += i; 4. What is the value of j after the following code is executed? for (i = 0, j = 1; i <= 10; i++) j += j; 5. What is the value of i after the following code is executed? for (i = 0; i < 10; i++) i += i; 6. Write a C program that reads in three integers and prints "equal" if all three are equal, and "not equal" otherwise. 6. Write a C program that reads in an integer and prints "leap year" if it is a leap year, and "regular year" otherwise. Recall a year is a leap year if it is divisible by 400, or if is divisible by 4 but not 100. 7. What does the following program do? #include int main(void) { int val, sum = 0; while (scanf("%d", &val) != EOF) { sum += val; } printf("The sum is %d\n", sum); return 0; } 8. Write a C program that reads in a sequence of real numbers and prints their average and standard deviation. The average and standard deviation of the values x_1, x_2, ..., x_n is given by avg = (x_1 + ... + x_n) / n stdev = sqrt( (x_1*x_1 + ... + x_n*x_n) / n - avg*avg ) 9. Write a C program that reads in a sequence of positive and negative integers and prints whether there were more positive or negative integers. 10. Write a C program that reads in a sequence of positive integers and prints out the longest streak of the same value. E.g., % a.out 2 6 4 3 3 2 3 2 2 2 2 6 6 6 1 6 6 6 Streak of 4 2's in a row. 11. Write a C program that reads in two integers m and n and plots an m x n rectangle of *'s. E.g., if m = 2 and n = 6, plot ****** ****** 12. Write a C program that reads in an integer n and plots an isoscoles right triangle with two sides of length n. E.g., if n = 4, plot * ** *** **** 13. Write a C program that reads in an integer n and plots an isoscoles right triangle as above but in the opposite orientation. E.g., if n = 4, plot **** *** ** * 14. What does the following program print. #include #define SIX 1+5 #define NINE 8+1 int main(void) { printf("%d * %d = %d\n", SIX, NINE, SIX * NINE); return 0; } . . ANSWERS TO BASIC C EXERCISES 1. Exchanges x and y. 2. A. No keyword "then" in C. B. Always need parentheses around the conditional. C. Nothing wrong. This one is OK. D. Missing semicolon before "else". 3. 45 4. 2048 5. 15 6. #include int main(void) { int a, b, c; scanf("%d %d %d", &a, &b, &c); if ((a == b) && (a == c)) printf("equal\n"); else printf("not equal\n"); return 0; } 6. #include int main(void) { int year; scanf("%d", &year); if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) printf("%d is a leap year\n", year); else printf("%d is NOT a leap year\n", year); return 0; } 7. It reads in a sequence of integers and prints their sum. The while(scanf("%d", &val) != EOF) is a C idiom that repeatedly reads in integers and stores them in variable val. The scanf() function returns EOF if there is no more input; this signals the end of the while loop. 8. #include #include int main(void) { double val, sum = 0.0, sum2 = 0.0, avg, stdev; int n = 0; while (scanf("%lf", &val) != EOF) { n++; sum += val; sum2 += val*val; } avg = sum / n; stdev = sqrt(sum2 / n - avg * avg); printf("Average = %f\n", avg); printf("Standard deviation = %f\n", stddev); return 0; } The "%lf" is needed with scanf() to read in doubles, but "%f" works with printf(). The sqrt() function is in the math library; in order to compile you should #include and you may need to compile with "gcc myprog.c -lm" to link in the math library. 9. #include int main(void) { int pos = 0, neg = 0; /* counter variables */ int val; /* integer just read in */ while (scanf("%d", &val) != EOF) { if (val > 0) pos++; else if (val < 0) neg++; } if (pos > neg) printf("There were more positive integers.\n"); else if (pos < neg) printf("There were more negative integers.\n"); else printf("There were an equal number of each.\n"); return 0; } This could also be done with a single counter variable that equals the number of positive integers minus the number of negative ones. 10. #include int main(void) { int val; /* integer just read in */ int prev = 0; /* previous integer */ int streak = 0; /* streak length of current integer */ int best_val = 0; /* value of best streak */ int best_streak = 0; /* streak length of best integer */ while (scanf("%d", &val) != EOF) { if (prev == val) streak++; else streak = 1; if (streak > best_streak) { best_streak = streak; best_val = val; } prev = val; } printf("Streak of %d %d's in a row.\n", best_streak, best_val); return 0; } 11. #include int main(void) { int m, n, i, j; scanf("%d %d", &m, &n); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("*"); } printf("\n"); } return 0; } 12. #include int main(void) { int n, i, j; scanf("%d",&n); for (j = 0; j < n; j++) { for (i = 0; i <= j; i++) { printf("*"); } printf("\n"); } return 0; } 13. #include int main(void) { int n, i, j; scanf("%d",&n); for (j = 0; j < n; j++) { for (i = 0; i < n; i++) { if (i >= j) printf("*"); else printf(" "); } printf("\n"); } return 0; } 14. 6 * 9 = 52 Trick question. #define is used to substitute one sequence of characters with another. Thus, SIX * NINE is replaced by 1+5*8+1. Multiplication has a higher precedence than addition. Lesson: #define is useful to avoid "magic numbers" or "hardwired constants", but be careful.