NUMBER REPRESENTATION EXERCISES READING ------- Notes for lecture 2. Kernighan and Ritchie, 1.2 King Chapter 7 (Number types) SUPPLEMENTAL READING -------------------- Deitel and Deitel, 9.1-9.5, Appendix E EXERCISES --------- 1. Convert 7654 from octal to hexadecimal. 2. Convert 7654 from octal to decimal. 3. Convert 7654 from decimal to octal. 4. Convert 7654 from decimal to binary. 5. Write a program that reads a decimal integer and prints out its value in octal. 6. Write a program that reads an octal integer and prints out its value in decimal. 7. Write a program that reads in two floats and prints out their ratio, to two decimal places. 7. Write a program that reads in two doubles and prints out their ratio, to two decimal places. 8. Write a program that reads in two integers and prints out their ratio, to two decimal places. 9. Type in the following program, compile it, and use it to compute the average of a few sequences of five integers. #include int main(void) { int i, sum, num; sum = 0; for (i = 0; i < 5; i++) { scanf("%d", &num); sum += num; } printf("%6.2f\n", sum/5.0); return 0; } 9. Try to figure out what the following program will print. Type it in, compile it, and check. #include int main(void) { double f; for (f = 0.0; f < 0.95; f += 0.1) printf("%.30f\n", f); printf("%.30f\n", f); if (f == 1.0) printf("f is 1.0\n"); else printf("f is NOT 1.0\n"); return 0; } Repeat, replacing 0.1 with 0.2. . . . ANSWERS TO NUMBER REPRESENTATION EXERCISES 1. First convert to binary: 7654 = 111 110 101 100 = 1111 1010 1100 = FAC 2. 7*8^3 + 6*8^2 + 5*8 + 4 = 7*512 + 6*64 + 40 + 4 = 4012 3. 7654 decimal is 16746 octal 8^4 = 4096 7654 - 1*4096 = 3558 8^3 = 512 3558 - 6*512 = 486 8^2 = 64 486 - 7*64 = 38 8^1 = 8 38 - 4*8 = 6 8^0 = 1 6 - 6*1 = 0 4. Shortcut: convert the octal from exercise 3 to binary (see exercise 1). 1110111100110 5. #include int main(void) { int n; scanf("%d", &n); printf("%o\n", n); return 0; } 6. #include int main(void) { int n; scanf("%o", &n); printf("%d\n", n); return 0; } 7. #include int main(void) { float x, y; scanf("%f %f", &x, &y); printf("%.2f\n", x / y); return 0; } 7. #include int main(void) { double x, y; scanf("%lf %lf", &x, &y); printf("%.2f\n", x / y); return 0; } Note: %lf is used with scanf() and %f with printf(). 8. #include int main(void) { int x, y; scanf("%d %d", &x, &y); printf("%.2f\n", (double) x / y); return 0; } Note: the cast is needed to prevent integer division. Casting has higher precedence than division so x is promoted to a double before the division. An alternative is 1.0 * x / y. 9. % gcc avg0.c % a.out 1 2 3 4 5 3.00 % a.out 123 456 789 876 543 557.40 10. The following results were obtained on arizona. It's usually not a good idea to test a double or float for exact equality. yuma.Princeton.EDU% a.out 0.000000000000000000000000000000 0.100000000000000005551115123126 0.200000000000000011102230246252 0.300000000000000044408920985006 0.400000000000000022204460492503 0.500000000000000000000000000000 0.599999999999999977795539507497 0.699999999999999955591079014994 0.799999999999999933386618522491 0.899999999999999911182158029987 0.999999999999999888977697537484 f is NOT 1.0 yuma.Princeton.EDU% a.out 0.000000000000000000000000000000 0.200000000000000011102230246252 0.400000000000000022204460492503 0.600000000000000088817841970013 0.800000000000000044408920985006 1.000000000000000000000000000000 f is 1.0