Top Banner
34

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.

Dec 29, 2015

Download

Documents

Allen Chandler
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.
Page 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.

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.15

Page 3: 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.

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)

Page 4: 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.

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

Page 5: 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.

13&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);

}

Page 6: 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.

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

Page 7: 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.

13&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.dat

Page 8: 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.

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;

Page 9: 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.

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 */

Page 10: 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.

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.

...

Page 11: 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.

13&14-11

{statement1;statement2;

.

.

.statementn;

}

A sequence of statements surrounded by a pairof 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, ...)

Page 12: 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.

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.

Page 13: 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.

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)

Page 14: 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.

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 );

Page 15: 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.

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

Page 16: 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.

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.

Higherdoublefloatinteger

Lower

2) Conversion of values with mixed data types in an expression using arithmetic operators

Page 17: 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.

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 */

Page 18: 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.

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)

Page 19: 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.

13&14-19

Flowchart

while EOF != scanf cTrueFalse

scanf c

if c != ‘ ‘ /* a blank */printf c

Page 20: 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.

13&14-20

/* 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));

}

Page 21: 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.

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 < and >

./a.out < input2.dat > output2.dat

5. View the contents of output2.dat

more output2.dat

Page 22: 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.

13&14-22

dostatement;

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);

...

Page 23: 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.

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)

Page 24: 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.

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

Page 25: 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.

13&14-25

/* 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);

}

Page 26: 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.

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

Page 27: 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.

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

Page 28: 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.

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.

Page 29: 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.

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!

Page 30: 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.

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)

Page 31: 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.

13&14-31

Flowchart

for i = 1 ; ; i=i+1

if value == -999break;

total = total+value;

TrueFalse

total = 0;

printf total/(float) (i-1)

scanf value

Page 32: 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.

13&14-32

/* 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);

}

Page 33: 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.

13&14-33

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.

Page 34: 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.

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 (< 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 Sentinel value.3) using the value in the file to indicate the number of values to be read.