Presentation is loading. Please wait.

Presentation is loading. Please wait.

13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection.

Similar presentations


Presentation on theme: "13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection."— Presentation transcript:

1

2 13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection operators ( ). Learn various methods for detecting the end of file. Related Chapters: ABC Chapter 4.8 - 4.15

3 13&14-3 1. Problem Definition Use a while statement to read a list of integers from a file and compute the average. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = integers from file “input.dat” Output=real number representing the arithmetic average =(sum of values)/(count of number of values) 3. Develop Algorithm (processing steps to solve problem)

4 13&14-4 Flowchart while EOF != scanf value total = total + value; count = count + 1; TrueFalse total = 0; count = 0; printf total/(double) count (double) is a cast see slide #14

5 13&14-5 /* C Program to compute the average of a list of numbers. */ #include void main(void) { int value,total = 0,count = 0; /*Why initialized to zero?*/ /* while the scanf function has not reached the EndOfFile */ while ( EOF != scanf("%i", &value) ) { total = total + value; count = count + 1; } /* end of while loop */ /* Output the average use a cast */ printf("Average of the %i numbers = %f \n",count,total/(float)count); }

6 13&14-6 1. Use gedit to enter the above code in a file problem1.c, and make sure to save the file. 2. Use gedit to create an input file input.dat and enter in integers into this file. Save and exit this file. 3. Compile the problem1.c code by typing gcc problem1.c

7 13&14-7 4. Run the program using Unix redirection <./a.out < input.dat 5. What happens if you type./a.out output.dat Note: if you do this a second time it will fail because Unix will not let you over-write an existing file. 6. To append the results to the existing file output.dat./a.out > output.dat

8 13&14-8 A second form of a declaration in C : data-type variable_name = initializer; /*Why are total and count initialized to zero?*/ Answer: Un-initialized variables have unknown (garbage) values. int value,total = 0,count = 0;

9 13&14-9 while(expression) statement; if expression evaluates to true, the statement is executed and then execution loops up and re-evaluates expression; otherwise execution passes to the next statement in the program. while statement format while(value >= 0.0); /* Error! Don’t put semicolon here */ /* This is an example of a logical error */ while (EOF != scanf("%i", &value)) { total = total + value; count = count +1; } /* end of while loop */

10 13&14-10 We can also execute multiple statements when a given expression is true: while (expression) { statement1; statement2; statementn; } if expression evaluates to true, all the statements are executed and then execution loops up and re-evaluates expression otherwise execution passes to the next statement in the program........

11 13&14-11 { statement1; statement2;... statementn; } A sequence of statements surrounded by a pair of curly braces is called a block or compound statement. 1) From outside, the compound statement looks like a single statement. A compound statement can go where any single C statement can go. (e.g. a branch of if-else, body of a for loop,...)

12 13&14-12 A block is any compound statement that may include variable declaration(s). 1) As a compound statement, from outside, the block looks like a single C statement. A block can go where any single C statement can go. 2) The importance of a block is that, the curly braces of the block delimit the (i.e. the region of validity) of the variables declared in the block. 3) The concept of lies at the heart of Structured Programming, as will be discussed in a future lecture on Modularity.

13 13&14-13 The program makes use of the fact that the scanf function returns and integer value representing the number of successful conversions. For example in the code fragment: Example: int number,value1,value2,value3; number = scanf("%i%i%i",&value1,&value2,&value3); the variable number could take one the value -1,0,1,2 or 3. EOF is a built-in constant in C (usually assigned -1). If you are checking for End-of-File then use this constant. EOF != scanf("%i", &value)

14 13&14-14 To fix the above use the cast operator where data_type is a valid C data type. float result; int total = 10, count = 4 ; result = total / (float) count; /* result now has the value 2.5 */ Example, float result; int total = 10, count = 4 ; result = total / count; /* result has the value 2.0 */ ( data_ type ) printf("Average of the %i numbers = %f\n",count, total/(float)count );

15 13&14-15 data_type1 x; data_type2 result ; result = x; If x and result have different data types then an automatic conversion takes place. Data could be lost. Example, float x = 3.1416; int result; result = x; /* result now has the value 3 */ 1) Conversion of assigned values

16 13&14-16 +, -, *, / x op y ;where op is If x and y have different (mixed) data types then C automatically converts data from the lower rank to the higher rank and then performs the computation. Note: % (modulus) is only defined on integers. The ranking is given by the following table. Higher double float integer Lower 2) Conversion of values with mixed data types in an expression using arithmetic operators

17 13&14-17 Example, float result, x = 3.1416; int y = 3; result = x + y; /* result now has the value 6.1416 */ Example, int x = 2.5; float result, y; y = x; result = 1 / y; /* result now has the value.5 */

18 13&14-18 1. Problem Definition Write a program that reads a file of characters one character at a time and writes out each non-blank character to another file. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = characters in a file “input2.dat” Assume this file is non-empty. Output=Non-blank characters from input2.dat are output to the file output2.dat 3. Develop Algorithm (processing steps to solve problem)

19 13&14-19 Flowchart while EOF != scanf c TrueFalse scanf c if c != ‘ ‘ /* a blank */ printf c

20 13&14-20 /* C Program to remove the blanks from a text file. */ #include void main(void) { char c; scanf("%c",&c); do { if (c != ' ') printf("%c",c); } while(EOF != scanf("%c",&c)); }

21 13&14-21 1. Use gedit to enter the above code in a file problem2.c, and make sure to save the file. 2. Use gedit to create an input file input2.dat and enter in any number of lines of text including blanks. Save and exit this file. 3. Compile the problem2.c code by typing gcc problem2.c 4. Run the program using Unix redirection./a.out output2.dat 5. View the contents of output2.dat more output2.dat

22 13&14-22 do statement; while(expression); The statement is executed first. Then if expression evaluates to true, execution loops up and the statement is executed again; otherwise execution passes to the next statement in the program. The general form of the do-while statement is: do/while statement format do { statement1; while-statementn; } while (expression);.......

23 13&14-23 1. Problem Definition Use a for statement to read a list of integers from a file and compute the average. The first number in the list gives the count of the numbers to be read. This first value is not used in the computation of the average. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = integers from file “input3.dat” Output=real number representing the arithmetic average (sum of values)/(count of number of values) 3. Develop Algorithm Use a counter controlled loop. (processing steps to solve problem)

24 13&14-24 Flowchart for i = 1 ; i <= count; i=i+1 total = total + value; TrueFalse total = 0; scanf count printf total/(double) count scanf value

25 13&14-25 /* C Program to compute the average of a list of numbers. */ #include void main(void) { int value,total = 0,count; int i; /* loop counter */ scanf("%i",&count); for(i=1;i<=count;i=i+1) { scanf("%i",&value);/* read value */ total = total + value; } /* end of for loop */ /* Output the average. */ printf("Average of %i numbers = %f \n",count,total/(float)count); }

26 13&14-26 1. Use gedit to enter the above code in a file problem3.c, and make sure to save the file. 2. Use gedit to create an input file input3.dat and enter in integers into this file.The first integer is the count of integers to be averaged. Save and exit this file. 3. Compile the problem1.c code by typing gcc problem3.c 4. Run the program using Unix redirection <./a.out < input3.dat

27 13&14-27 for(init. expressions ; expression ; update stmnts. ) statement; C allows more than one initialization expression or update statement but these statements or expressions must be separated by a comma not a semicolon. Also, if there is more than one init. expression or update statement then the order of execution is from left to right. The initialization expressions are executed once (and only once). If expression evaluates to true, statement1 is executed and execution loops up and evaluates all the update stmnts. and then expression is re-evaluated; otherwise execution passes to the next statement in the program. The following order of must be observed … init. expressions; expression ; update stmnts

28 13&14-28 for(init. expressions ; expression ; update stmnts. ) { statement1; statement2;... statementn; } The initialization expressions are executed once (and only once), next: if expression evaluates to true, the statements statement1,...,statementn are executed and execution loops up and evaluates all the update stmnts. and then expression is re-evaluated; otherwise execution passes to the next statement in the program.

29 13&14-29 Write a code fragment to add the integers from 1 to 100. int i,total; total = 0; for(i=1;i<=100;i=i+1) total = total + i; The above code could be re-written as: int i,total; for(i=1, total = 0; i<=100; total=total+i, i=i+1); but not as: int i,total; for(i=1, total = 0; i<=100; i=i+1, total=total+i); Why not? The order of the statements is critical!

30 13&14-30 1. Problem Definition Use a for statement to read a list of integers from a file and compute the average. Read the integers until the sentinel value of -999 occurs. Do not use the sentinel value in the computation of the average. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = integers from file “input4.dat” Output=real number representing the arithmetic average (sum of values)/(count of number of values) 3. Develop Algorithm (processing steps to solve problem)

31 13&14-31 Flowchart for i = 1 ; ; i=i+1 if value == -999 break; total = total+value; TrueFalse total = 0; printf total/(float) (i-1) scanf value

32 13&14-32 /* C Program to compute the average of a list of numbers. */ #include void main(void) { int value,total = 0,count; int i; /* loop counter */ for(i=1; ;i=i+1) { scanf("%i",&value);/* read value */ if (value == -999) /* -999 is the sentinel value */ break; total = total + value; } /* end of for loop */ /* Output the average. */ count = i-1; printf("Average of %i numbers = %f \n",count,total/(float)count); }

33 13&14-33 Omitting the logical expression in a for statement means that the for statement executes as an infinite loop. for(i=1; ;i=i+1) { As with the switch statement the break statement causes the termination of the loop but not the program.

34 13&14-34 You can use any of the loop statements (for,while,do/while) to code a loop. The for statement is useful especially when you know how many times the loop will iterate. The do/while loop always executes at least once. The break statement may be used in any loop or switch statement. C performs automatic data conversion when 1) assigning values of different data-types and 2) when performing computations on values of different data-types. Use a cast to force data-type conversion. Reading from one data file and writing to one data file can be accomplished with Unix redirection ( ). Later in the semester we will consider fprintf and fscanf functions that will allow the use of multiple input and output files. We considered three methods for indicating that the end of file has been reached. 1) use of the EOF built-in constant. 2) use of a Sentinel value. 3) using the value in the file to indicate the number of values to be read.


Download ppt "13&14-2 Know the forms of loop statements in C (while,do/while,for). Understanding how data conversion occurs. Read/Write data in files using Unix redirection."

Similar presentations


Ads by Google