CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

26
CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    1

Transcript of CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Page 1: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

CS 1400

25 Sept 2006

Pick ups from chapters 4 and 5

Page 2: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Condition statements

• General forms

if (expr) if (expr)

{ statements { statements

} }

else

{ statements

}

Page 3: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Braces are not required for a single statement

• But an else is always associated with the closest possible if above it!

• example: Skill level >1 is advanced, level >2 is advanced master, lower levels are beginners.

if (skill > 1) cout << “advanced “;

if (skill > 2)cout << “master”;

else cout << “beginner”;

Page 4: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Solution – use braces!

if (skill > 1)

{ cout << “advanced “;

if (skill > 2)

cout << “master”;

}

else cout << “beginner”;

Page 5: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

if-else-if

• If you are careful, if statements can be chained…– program 4.11

• Chained if statements can be useful for menus…– example: menu.cpp

Page 6: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

The switch statement• General form:

switch (integer variable)

{ case (constant value1): statements

break;

case (constant value2): statements

break;

case (constant value3): statements

break;

default: statements

}

Page 7: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Compound boolean expressions• Two boolean expressions can be

combined using&& (and)

|| (or)

• examples:if ((age > 21) && (age < 35))

cout << “You could be drafted!”;

if ((age < 0) || (age > 120))cout << “Incorrect age!”;

Page 8: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Comparing two strings…• The following will have unexpected results:

char name1[20], name2[20];

cin >> name1 >> name2;

if (name1 == name2)

cout << “these names are the same!”;

The reasons will become clear later on…

if (strcmp (name1, name2) == 0)

cout << “these names are the same!”;

Page 9: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Testing for file errors…• A file variable may be tested for the failure of the

most recent file operation using the member function .fail()

• Example:ifstream fin;fin.open(“myfile.txt”);if (fin.fail())

cout << “this file could not be opened!”;

• Alternate example:if (!fin)

cout << “this file could not be opened!”;

Page 10: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Other loop statements…

• The do-while loopdo

{ statements;

} while ( expr ) ;

This is a post-test loop: The expression is tested at the bottom of the loop and always performs at least one iteration

Page 11: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Example;

• Input validation – a program must prompt for an age and only accept a value greater than 0 and less than 120.

do

{ cout << “Enter a valid age: “;

cin >> age;

} while ((age > 0) && (age < 120));

Page 12: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Other loop statements.

• The for loopfor ( initialization ; test expr ; update )

{ statements;

}

This is a pre-test loop: The test expression is tested at the top of the loop. The initialization, test expression and update segments can be any valid C++ expression

Page 13: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Explanation of segments…

• initialization– This segment is executed once prior to

beginning the loop

• test expr– This segment is executed prior to each

iteration. If this segment is false, the loop terminates

• update– This segment is executed at the bottom of

each iteration.

Page 14: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Example;

• Add 20 numbers input by the user;float num, sum = 0.0;

int n;

for (n=0; n<20; n++)

{ cout << “Enter a number: “;

cin >> num;

sum += num;

}

Page 15: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Example variation…• The test variable can actually be declared

within the loopfloat num, sum = 0.0;

for (int n=0; n<20; n++)

{ cout << “Enter a number: “;

cin >> num;

sum += num;

}

Page 16: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

A programmer has a choice…• Convert the following while loop to a for loop

int count = 0;

while (count < 50)

{ cout << “count is: “ << count << endl;

count ++;

}

• Convert the following for loop to a whilefor (int x = 50; x > 0; x- - )

{ cout << x << “ seconds to launch.\n”;

}

Page 17: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Example – loan amoritization

• A case study (5.14, pg 295)

Write a loan amoritization program– inputs: principle, interest_rate, num_years– outputs:

monthly payment

for each month: month number, interest, principle, remaining_balance

Page 18: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Example execution:Enter loan amount: $ 2500

Enter annual Interest rate: 0.08

Enter number of years: 2

Monthly payment: $113.07

month interest principle balance

1 16.67 96.40 2403.60

2 16.02 97.04 2306.55

3 15.38 97.69 2208.86

(a total of 24 month lines)

Page 19: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Formulas

payment =

(principle * interest_rate/12 * term) / (term-1)

where term =

(1 + interest_rate/12) num_years * 12

monthly_interest =

(annual_rate / 12) * balance

principle =payment – monthly_interest

Page 20: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Pseudocode…1. Prompt and input loan amount, annual interest rate,

and number of years

2. Calculate and display monthly payment

3. Print report header

4. For each month in the loan period:a) calculate the monthly interest

b) calculate the principle

c) display the month number, interest, principle, and balance

d) calculate the new balance

Page 21: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

1

2

3

4

for ( int month = 1 ; month <= num_years * 12 ; month++)

{

}

d)

c)

b)

a)

First refinement…

Page 22: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Example – loops within loopsinput table of grades

Student count: 64

John (9 scores): 9 7 8 9 6 10 10 8 10

Fred (13 scores): 7 8 5 6 7 6 8 9 6 10 9 10 3

Emily (8 scores): 8 4 5 3 9 7 5 4

Page 23: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Pseudocode – rough outline1. Input number_of_students

2. Repeat for number_of_students iterations;

a-b) Input student_name and count_of_grades

c-d) sum grades for this student

e) Calculate average

f) Output student_name and average

Page 24: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

Pseudocode (more detailed)…1. Input number_of_students

2. Repeat for number_of_students iterations;

a) Input student name

b) Input count_of_grades

c) initialize sum to 0.

d) Repeat for count_of_grades iterations;

i– Input grade

ii– sum grade

e) Calculate average

f) Output student_name and average

Page 25: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

1

2

for ( int student = 0 ; student <= num_of_students ; student++)

{

}

c)

b)

a)

First refinement…

e)

f)

d)

Page 26: CS 1400 25 Sept 2006 Pick ups from chapters 4 and 5.

c)

Second refinement…

e)

f)

for (int count=0; count<=num_of_grades; count++)

{

}

ii--

i--