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 and Learn various methods for detecting the end of file Related Chapters ABC Chapter 4 8 4 15 13 14 2 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 13 14 3 total 0 count 0 Flowchar t False True while EOF scanf value total total value count count 1 printf total double count double is a cast see slide 14 13 14 4 C Program to compute the average of a list of numbers include stdio h void main void int value total 0 count 0 Why initialized to zero while the scanf function has not reached the EndOfFile while EOF total count end of scanf i value total value count 1 while loop Output the average use a cast printf Average of the i numbers f n count total float count 13 14 5 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 13 14 6 4 Run the program using Unix redirection a out input dat 5 What happens if you type a out input dat 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 input dat output dat 13 14 7 int value total 0 count 0 Why are total and count initialized to zero Answer Un initialized variables have unknown garbage values A second form of a declaration in C data type variable name initializer 13 14 8 while EOF scanf i value total total value count count 1 end of while loop while statement format 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 Error Don t put semicolon here while value 0 0 This is an example of a logical error 13 14 9 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 13 14 10 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 statement1 statement2 statementn 13 14 11 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 14 12 EOF scanf i value 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 13 14 13 printf Average of the i numbers f n count total float count 2 5 Example float result int total 10 count 4 result total count result has the value 2 0 To fix the above use the cast operator data type 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 13 14 14 1 Conversion of assigned values 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 13 14 15 2 Conversion of values with mixed data types in an expression using arithmetic operators 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 13 14 16 Example float result x 3 1416 int y 3 result x y result now has the value 6 1416 Example 5 int x 2 5 float result y y x result 1 y result now has the value 13 14 17 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 are Output Non blank characters from input2 dat output to the file output2 dat 3 Develop Algorithm processing steps to solve problem 13 14 18 scanf c Flowchar t if c a blank printf c False True while EOF scanf c 13 14 19 C Program to remove the blanks from a text file include stdio h void main void char c scanf c c do if c printf c c while EOF scanf c c 13 14 20 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 and a out input2 dat output2 dat 5 View the contents of output2 dat more output2 dat 13 14 21 do while statement format do statement while expression The statement is executed first Then if expression evaluates to true execution loops up and …
View Full Document