Below is a source code to show when sum of saving exceeds JPY10,000 if one keeps saving JPY1 today, JPY2 tomorrow and JPY4 the day after tomorrow. Write the source code and save it as "while1.c". After compilation, confirm by executing it with different input data.

#include <stdio.h> int main(void){ int i, j, sum; i = 1; j = 1; sum = 0; while (sum < 10000){ sum += j; printf("Sum of saving up to day %2d = %8d JPY\n", i, sum); i++; j *= 2; } return (0); }

$./a.outSum of saving up to day 1 = 1 JPY Sum of saving up to day 2 = 3 JPY Sum of saving up to day 3 = 7 JPY Sum of saving up to day 4 = 15 JPY Sum of saving up to day 5 = 31 JPY Sum of saving up to day 6 = 63 JPY Sum of saving up to day 7 = 127 JPY Sum of saving up to day 8 = 255 JPY Sum of saving up to day 9 = 511 JPY Sum of saving up to day 10 = 1023 JPY Sum of saving up to day 11 = 2047 JPY Sum of saving up to day 12 = 4095 JPY Sum of saving up to day 13 = 8191 JPY Sum of saving up to day 14 = 16383 JPY

Below is a source code to show sum and mean of input data including decimal. Write the source code and save it as "while2.c". After compilation, confirm by executing it with different input data.

#include <stdio.h> int main(void){ int n; double dat, sum, mean; sum=0.0; n=0; while (scanf("%lf", &dat)!=EOF){ sum += dat; n++; } printf("Sum = %9.4f\n", sum); mean = sum/n; printf("Mean = %9.4f\n", mean); return (0); }

(Execution result) $./a.out1 2.1 3.2 0.8 5 ^d <- Ctrl-d Sum = 12.1000 Mean = 2.4200

Write a below source code and save it as "while3.c". After compilation, confirm the difference between results of Exercise 2 and results of Exercise 3.

#include <stdio.h> int main(void){ int n, sum, mean; double dat; sum=0; n=0; while (scanf("%lf", &dat)!=EOF){ sum += dat; n++; } printf("Sum = %4d\n", sum); mean = sum/n; printf("Mean = %4d\n", mean); return (0); }

(Execution result) $./a.out1 2.1 3.2 0.8 5 ^d Sum = ******* <- concealed by ***** Mean = *******

For a group whose student number is known, we want to calculate means of english, mathematics and physics, respectively. Using while-loop, program to meet this requirement and save such a source code as "while4.c".

(Execution result) $./a.outInput number of students3English score of student 150Math score of student 170Physics score of student 160English score of student 240Math score of student 220Physics score of student 230English score of student 3100Math score of student 3100Physics score of student 390Mean of english = 63.333 Mean of math = 63.333 Mean of physics = 60.000

We want to ask age and show different message based on the age. Using if-statement, program and save such a source code as "while5.c".

(Execution result) $./a.outHow old are you?30You are 30 years old. $ $./a.outHow old are you?105You are 105 years old. You live long. $ $./a.outHow old are you?-10Input proper age. $

In addition to while5.c, if input age is not appropriate, we want to repeat to show input message as shown below. Using while-loop, program and save such a source code as "while6.c".

(Execution result) $./a.outHow old are you?30You are 30 years old. $ $./a.outHow old are you?105You are 105 years old. You live long. $ $./a.outHow old are you?-10Input proper age. How old are you?-10Input proper age. How old are you?-3Input proper age. How old are you?15You are 15 years old. $

Lessons for C programming Junichi Susaki, Kyoto University