CS 1400 - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

CS 1400

Description:

CS 1400. Pick ups from chapters 4 and 5. if-else-if. Chained if statements can be useful for menus... (a total of 24 month lines) Pseudocode... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 31
Provided by: scottc50
Category:
Tags: lines | pickup

less

Transcript and Presenter's Notes

Title: CS 1400


1
CS 1400
  • Pick ups from chapters 4 and 5

2
if-else-if
  • Chained if statements can be useful for menus
  • Enter savings plan choice (A) economy
    rate
  • (B) saver rate
  • (C) preferred rate
  • (D) special rate

3
Example Menu
  • char choice
  • cin gtgt choice
  • if (choice A)
  • rate 2.15
  • else if (choice B)
  • rate 3.25
  • else if (choice C)
  • rate 4.86
  • else rate 5.22

4
The switch statement
  • General form
  • switch (ordinal variable)
  • case constant value1 statements
  • break
  • case constant value2 statements
  • break
  • case constant value3 statements
  • break
  • default statements

5
Example Menu (bis)
  • char choice
  • cin gtgt choice
  • switch (choice)
  • case A rate 2.15
  • break
  • case B rate 3.25
  • break
  • case C rate 4.86
  • break
  • default rate 5.22

6
The break is optional
  • char choice
  • cin gtgt choice
  • switch (choice)
  • case A
  • case a rate 2.15
  • break
  • case B
  • case b rate 3.25
  • break
  • case C
  • case c rate 4.86
  • break
  • default rate 5.22

7
Compound boolean expressions
  • Two relational (or Boolean) expressions can be
    combined using
  • (and)
  • (or)
  • examples
  • if ((age gt 21) (age lt 35))
  • cout ltlt You could be drafted!
  • if ((age lt 0) (age gt 120))
  • cout ltlt Incorrect age!

(Dont confuse with and )
8
Precedence
  • has precedence over
  • relational operators have precedence over
  • if (altb cgtd ef) same as
  • if (((altb) (cgtd)) (ef))

9
Caution!!
  • Be sure , are between relational expressions
    or Boolean variables
  • if (a lt bc) ??
  • if (ba c) ??

10
Comparing two strings
  • The following will have unexpected results
  • char name120, name220
  • cin gtgt name1 gtgt name2
  • if (name1 name2)
  • cout ltlt these names are the same!
  • The reasons will become clear later on This is
    correct
  • if (strcmp (name1, name2) 0)
  • cout ltlt these names are the same!

FYI
11
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(fmyfile.txt)
  • if (fin.fail())
  • cout ltlt this file could not be opened!
  • Alternate example
  • if (!fin)
  • cout ltlt this file could not be opened!

12
Other loop statements
  • The do-while loop
  • do
  • 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

13
Example
  • Input validation a program must prompt for an
    age and only accept a value greater than 0 and
    less than 120.
  • do
  • cout ltlt Enter a valid age
  • cin gtgt age
  • while ((age lt 0) (age gt 120))

14
Other loop statements.
  • The for loop
  • for ( 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

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

16
Example
  • Add 20 numbers input by the user
  • float num, sum 0.0
  • int n
  • for (n0 nlt20 n)
  • cout ltlt Enter a number
  • cin gtgt num
  • sum num

17
Example variation
  • The test variable can actually be declared within
    the loop
  • float num, sum 0.0
  • for (int n0 nlt20 n)
  • cout ltlt Enter a number
  • cin gtgt num
  • sum num

18
A programmer has a choice
  • Convert the following while loop to a for loop
  • int count 0
  • while (count lt 50)
  • cout ltlt count is ltlt count ltlt endl
  • count
  • Convert the following for loop to a while
  • for (int x 50 x gt 0 x-- )
  • cout ltlt x ltlt seconds to launch.\n

19
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

20
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

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

22
Pseudocode
  • Prompt and input loan amount, annual interest
    rate, and number of years
  • Calculate and display monthly payment
  • Print report header
  • For each month in the loan period
  • calculate the monthly interest on the current
    balance
  • calculate the principle for this months payment
  • display the month number, interest, principle,
    and balance
  • calculate the new balance

23
First refinement
for ( int month 1 month lt num_years 12
month) // 4.

24
Example loops within loopstable of grades file
  • Students 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

25
Pseudocode rough outline
  • 1. Input number_of_students
  • 2. Repeat for number_of_students iterations
  • a-f) process a student and output the name and
    average

26
Pseudocode more detail
  • 1. 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

27
Pseudocode -- final detail
  • 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

28
First refinement
for ( int student 0 student lt num_of_students
student) //2

29
Second refinement

for (int count0 countltnum_of_grades count)
// d)

30
Examples loops within loops
for (int n0 nlt4 n) for (int k0 klt3
k) cout ltlt n ltlt n ltlt k ltlt k ltlt
endl for (int n0 nlt4 n) for
(int kn klt3 k) cout ltlt n ltlt n ltlt
k ltlt k ltlt endl
Write a Comment
User Comments (0)
About PowerShow.com