wheat.c 904 B

1234567891011121314151617181920212223242526272829
  1. /* wheat.c -- exponential growth */
  2. #include <stdio.h>
  3. #define SQUARES 64 // squares on a checkerboard
  4. int main(void)
  5. {
  6. const double CROP = 2E16; // world wheat production in wheat grains
  7. double current, total;
  8. int count = 1;
  9. printf("square grains total ");
  10. printf("fraction of \n");
  11. printf(" added grains ");
  12. printf("world total\n");
  13. total = current = 1.0; /* start with one grain */
  14. printf("%4d %13.2e %12.2e %12.2e\n", count, current,
  15. total, total/CROP);
  16. while (count < SQUARES)
  17. {
  18. count = count + 1;
  19. current = 2.0 * current;
  20. /* double grains on next square */
  21. total = total + current; /* update total */
  22. printf("%4d %13.2e %12.2e %12.2e\n", count, current,
  23. total, total/CROP);
  24. }
  25. printf("That's all.\n");
  26. return 0;
  27. }