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 (< and >). • Learn various methods for detecting the end of file. Related Chapters: ABC Chapter 4.8 - 4.1513&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)13&14-4 Flowchart while EOF != scanf value total = total + value; count = count + 1; True False total = 0; count = 0; printf total/(double) count (double) is a cast see slide #1413&14-5 /* 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 != 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); }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.c13&14-7 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.dat13&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;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 */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. . . .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, ...)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&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)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 );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 values13&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 operators13&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 */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)13&14-19 Flowchart while EOF != scanf c True False scanf c if c != ‘ ‘ /* a blank */ printf c13&14-20 /* C Program to remove the blanks from a text file. */ #include
View Full Document