1 / 34

Lecture 13 & 14

Lecture 13 & 14. What will I learn in this lecture?. 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.

sraymond
Download Presentation

Lecture 13 & 14

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 13 & 14

  2. What will I learn in this lecture? • 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

  3. C Problem Example 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. C Problem Example total = 0; count = 0; Flowchart False True while EOF != scanf value total = total + value; count = count + 1; printf total/(double) count(double) is a cast see slide #14

  5. C Problem Example /* 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); }

  6. C Problem Example - Execution 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. C Problem Example - I/O Redirection 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

  8. Assignment Statement int value,total = 0,count = 0; /*Why are total and count initialized to zero?*/ Answer: Un-initialized variables have unknown (garbage) values. A secondform of adeclarationin C : data-typevariable_name = initializer;

  9. while - Loop Statement 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 */ /* This is an example of a logical error*/ while(value >= 0.0);

  10. while - General Format 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. What is a compound statement? • A sequence of statements surrounded by a pairof curly braces is called a block or compound statement . • 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; }

  12. A block is any compound statement that may include variable declaration(s). • 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. • 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. • The concept of lies at the heart ofStructured Programming, as will be discussed in a future lecture on Modularity. scope scoping What is all this fuss about blocks?

  13. scanf - Revisited 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 constant.

  14. Arithmetic Conversions - Casts printf("Average of the %i numbers = %f\n",count, total/(float)count ); 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 wheredata_typeis a valid C data type. float result; int total = 10 , count = 4 ; result = total / (float) count;/* result now has the value 2.5 */ ( data_type )

  15. Arithmetic Conversions 1) Conversion of assigned values data_type1x; data_type2result; result = x; If x andresulthave different data types then an automatic conversion takes place. Data could be lost. Example, float x = 3.1416; intresult; result = x;/* result now has the value 3 */

  16. Arithmetic Conversions 2) Conversion of values with mixed data types in an expression using arithmetic operators xopy ; whereopis +, - , *, / Ifx andy 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

  17. Arithmetic Conversions 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. C Problem Example 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. C Problem Example scanf c Flowchart if c != ‘ ‘ /* a blank */ printf c False True while EOF != scanf c

  20. C Problem Example /* 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)); }

  21. C Problem Example - Execution 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

  22. do/while - Loop Statement • do/while statement format 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 { statement1; while-statementn; } while (expression); . ..

  23. C Problem Example 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. C Problem Example total = 0; scanf count Flowchart False True for i = 1 ; i <= count; i=i+1 scanf value total = total + value; printf total/(double) count

  25. C Problem Example /* C Program to compute the average of a list of numbers. */ #include <stdio.h> 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. C Problem Example - Execution 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. for - Loop Statement 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. for - General Format 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. for - Examples 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. C Problem Example - Revisited 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. C Problem Example - Revisited total = 0; Flowchart False True for i = 1 ; ; i=i+1 scanf value if value == -999 break; total = total+value; printf total/(float) (i-1)

  32. C Problem Example - Revisited /* C Program to compute the average of a list of numbers. */ #include <stdio.h> 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. break Statement Omitting the logical expression in a for statement meansthat 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. What have I learned in this lecture? • 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 (< and >). 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 Sentinelvalue. • 3) using the value in the file to indicate the number of values to be read.

More Related