Top Banner
INTEC CS160 - Jeanine Ing ber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files
31

INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

Mar 26, 2015

Download

Documents

Jessica Scott
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: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Engineering Problem Solving with C

Fundamental Concepts

Chapter 3

Control Structures and Data Files

Page 2: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Algorithm Development

Page 3: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Structured Programming

Sequence

Selection

Repetition

yesno

yes

no

Page 4: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Conditional Expressions

Page 5: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Relational Operators

== equality != non equality < less than > greater than <= less than equal to >= greater than equal to

Page 6: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Logical Operators

! not && and || or

Page 7: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Operator Precedence

1 < <= > >=2 == !=3 &&4 ||

Page 8: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Selection Statements

Page 9: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Selection Statements

if if else switch

Page 10: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

If statement

if(Boolean expression)

statement; //single statement if(Boolean expression)

{ //more than one statement

statement1;

.

statement n;

}

Page 11: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

If statement - examples

• if (x>0)k++;

• if(x>0) {

x=sqrt(x);k++;

}

Page 12: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

if - else statement

if(Boolean expression)statement;

elsestatement;

if(Boolean expression){

statement block}else{

statement block}

Page 13: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

nested if-else

if(x > y)if(y < z)

k++;else

m++;else

j++;

Page 14: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Practice!

int x=9, y=7, z=2, k=0, m=0, j=0;if(x > y)

if(y < z)k++;

elsem++;

elsej++;

What are the values of j, k and m?

Page 15: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Switch Statement

switch(expression){

case constant:statement(s);break;

case constant:statement(s);break;

/* default is optional*/default:

statement(s);}

Page 16: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Switch Statement

Expression must be of type integer or character

The keyword case must be followed by a constant

break statement is required unless you want all subsequent statements to be executed.

Page 17: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Practice!

Convert the following nested if/else statements to a switch statement:

if (rank==1 || rank==2) printf("Lower division \n");else{ if (rank==3 || rank==4) printf("Upper division \n"); else   {  if (rank==5) printf("Graduate student \n"); else printf("Invalid rank \n"); }}

Page 18: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Loop Structures

Page 19: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

repetition

while statement do while statement for statement

Page 20: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

while statement

while(expression)statement;

while(expression){

statement;statement;

.}

Page 21: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

do while

dostatement;

while(expression);

do{

statement1;statement2;

.} while(expression);

note - the expression is tested after the statement(s) are executed, so statements are executed at least once.

Page 22: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

for statement

for(initialization; test; increment/decrement)

statement;

for(initialization; test; increment/decrement)

{

statement;

statement;

.

}

Page 23: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

for statement

initalizetest

increment/decrement

true

statement(s)statement(s)

Page 24: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

for statement - examples

int sum =0;for(int I=1;I<10;I+=2)

sum = sum + I;

int fact =1;for(int n=5;n>1;n- -)

fact = fact * n;

Page 25: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Practice!

Determine the number of times that each of the following for loops are executed.

for (k=3; k<=20; k++){statements;}for (k=3; k<=20; ++k){statements;}for (count=-2; count<=14; count++){statements;}

Page 26: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

break statement

break;– terminates loop– execution continues with the first statement

following the loop

Page 27: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

continue statement

continue;– forces next iteration of the loop, skipping any

remaining statements in the loop

Page 28: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Data Files

Page 29: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Data Files

Each data file must have a file pointer– file pointer must be defined

• FILE *sensor1;• FILE *balloon;

– file pointer must be associated with a specific file using the fopen function

• sensor1 = fopen(“sensor1.dat”, “r”);

• balloon = fopen(“balloon.dat”, “w”);

Page 30: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

I/O Statements

Input file - use fscanf instead of scanf– fscanf(sensor1, “%1f %lf”, &t, &motion);

Output file - use fprint instead of printf– fprintf(balloon, “%f %f %f\n”, time, height,

velocity);

Page 31: INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

INTEC CS160 - Jeanine Ingber

Reading Data Files

counter controlled loop– for loop

sentinel controlled loop– while loop

end of file controlled loop– while loop