Lecture 5C for Section-C
-
Upload
zeeshan-rasool -
Category
Documents
-
view
218 -
download
0
Transcript of Lecture 5C for Section-C
-
7/31/2019 Lecture 5C for Section-C
1/19
11/2/2011
1
CS141-Programming Fundamentals-Jameel Ahmad 1
CS141-Programming Fundamentals-Jameel Ahmad
CS-141 Programming Fundamentals
Lecture-5C
Counter-Controlled Repetition
Sentinel-Controlled Repetition
Nested Control Structures
Jameel AhmadAssistant Professor
Department of Electrical Engineering
University of Management and Technology
2
CS141-Programming Fundamentals-Jameel Ahmad
1. Lab 2 on Friday Nov 04, 2011
2. Quiz 2 on Friday Nov 04, 2011
3. Teaching Assistants (TAs) for class
1) Abdur Rehman: Phone No 0334-471-4413 Email: [email protected]
2) Fazalur Rehman Sharif: Phone No 0334-324-8611 Email:[email protected]. Home work Grader:
Rehan Afzal Phone No 0345-451-3577
Announcements for section C
3
C How to Program, 6/e
-
7/31/2019 Lecture 5C for Section-C
2/19
11/2/2011
2
CS141-Programming Fundamentals-
Jameel Ahmad 5
CS141-Programming Fundamentals-Jameel Ahmad 6
7
Chapter 3 - Structured
Program Development
Outline3.1 C Data Types3.2 Algorithms3.3 Pseudocode3.4 Control Structures
3.5 The If Selection Statement3.6 The IfElse Selection Statement3.7 The While Repetition Statement3.8 Formulating Algorithms: Case Study 1 (Counter-Controlled
Repetition)
3.9 Formulating Algorithms with Top-down, Stepwise
Refinement: Case Study 2 (Sentinel-Controlled Repetition)3.10 Formulating Algorithms with Top-down, Stepwise
Refinement: Case Study 3 (Nested Control Structures)3.11 Assignment Operators3.12 Increment and Decrement Operators
CS141-Programming Fundamentals-
Jameel Ahmad
CS141 Programming Fundamentals
while ( expression or condition){
Single statement
orBlock of statements;}
While Repetition structureWhile Repetition structureWhile Repetition structureWhile Repetition structure
8
-
7/31/2019 Lecture 5C for Section-C
3/19
-
7/31/2019 Lecture 5C for Section-C
4/19
11/2/2011
4
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved .
CS141 Programming Fundamentals 13
The while Loop (continued)
Infinite loop: continues to execute endlessly
Can be avoided by including statements in the
loop body that assure exit condition will
eventually be false
One-Way (if) Selection
CS141-Programming Fundamentals-Jameel
Ahmad14
The syntax of one-way selection is:
if (expression)
statement
Statement is executed if the value of the expression is true
Statement is bypassed if the value is false; program goes
to the next statement
One-Way (if) Selection (continued)
CS141-Programming Fundamentals-Jameel
Ahmad15
The expression is sometimes called a decision maker because
it decides whether to execute the statement that follows it
The statement following the expression is sometimes called
the action statement
The expression is usually a logical expression
The statement is any C++ statement
if is a reserved word
CS141-Programming Fundamentals-Jameel
Ahmad16
-
7/31/2019 Lecture 5C for Section-C
5/19
-
7/31/2019 Lecture 5C for Section-C
6/19
-
7/31/2019 Lecture 5C for Section-C
7/19
-
7/31/2019 Lecture 5C for Section-C
8/19
11/2/2011
8
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved .
29
3.9 Formulating Algorithms with Top-Down, Stepwise Refinement
Top-down, stepwise refinement
Begin with a pseudocode representation of the top:
Determine the class average for the quiz
Divide top into smaller tasks and list them in order:
Initialize variablesInput, sum and count the quiz gradesCalculate and print the class average
Many programs have three phases: Initialization: initializes the program variables
Processing: inputs data values and adjusts programvariables accordingly
Termination: calculates and prints the final results
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved .
30
3.9 Formulating Algorithms with Top-
Down, Stepwise Refinement
Refine the initialization phase fromInitialize
variables to:Initialize total to zero
Initialize counter to zero
RefineInput, sum and count the quiz grades to
Input the first grade (possibly the sentinel)While the user has not as yet entered the sentinel
Add this grade into the running total
Add one to the grade counter
Input the next grade (possibly the sentinel)
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved .
31
3.9 Formulating Algorithms with Top-Down, Stepwise Refinement
Refine Calculate and print the class average toIf the counter is not equal to zero
Set the average to the total divided by the counter
Print the average
else
Print No grades were entered
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved .
32
3.9 Formulating Algorithms with Top-
Down, Stepwise Refinement
Initialize total to zero
Initialize counter to zero
Input the first grade
While the user has not as yet entered the sentinel
Add this grade into the running totalAdd one to the grade counter
Input the next grade (possibly the sentinel)
If the counter is not equal to zero
Set the average to the total divided by the counter
Print the average
else
Print No grades were entered
-
7/31/2019 Lecture 5C for Section-C
9/19
-
7/31/2019 Lecture 5C for Section-C
10/19
-
7/31/2019 Lecture 5C for Section-C
11/19
-
7/31/2019 Lecture 5C for Section-C
12/19
-
7/31/2019 Lecture 5C for Section-C
13/19
11/2/2011
13
Algorithm Design
Set precision to two decimal places
Prompt user for account number and customer type
If customer type is R or r
Prompt user for number of premium channels
Compute and print the bill
If customer type is B or b
Prompt user for number of basic service connections andnumber of premium channels
Compute and print the bill ProgrammingExample-1
49
Variables
ProgrammingExample-1
50
Named Constants
Pro
grammingExample-1
51
Formulas
Billing for residential customers:
amountDue = RES_BILL_PROC_FEES +
RES_BASIC_SERV_COST
+ numOfPremChannels *
RES_COST_PREM_CHANNEL;
ProgrammingExample-1
52
-
7/31/2019 Lecture 5C for Section-C
14/19
11/2/2011
14
Formulas (continued)
Billing for business customers:
if (numOfBasicServConn
-
7/31/2019 Lecture 5C for Section-C
15/19
11/2/2011
15
Programming Example-2 A local bank in your town needs a program to calculate a
customers checking account balance at the end of each
month
Data are stored in a file in the following form:
467343 23750.40
W 250.00
D 1200
W 75.00
I 120.74
Pro
grammingExample-2
57
Programming Example-2
(continued) The first line of data shows the account number followed by the
account balance at the beginning of the month
Thereafter each line has two entries:
Transaction code
Transaction amount
Transaction codes W or w means withdrawal
D or d means deposit
I or i means interest paid by the bankProgrammingExample-2
58
Programming Example
(continued) Program updates balance after each transaction
During the month, if at any time the balance goes below$1000.00, a $25.00 service fee is charged
Pro
grammingExample-2
59
Programming Example
(continued)
Program prints the following information:
Account number
Balance at the beginning of the month
Balance at the end of the month
Interest paid by the bank
Total amount of deposit
Number of deposits
Total amount of withdrawal
Number of withdrawals
Service charge if any
ProgrammingExample-2
60
-
7/31/2019 Lecture 5C for Section-C
16/19
-
7/31/2019 Lecture 5C for Section-C
17/19
11/2/2011
17
Variables
ProgrammingExample-2
65
Named Constants
ProgrammingExam
ple-2
66
Steps1. Declare variables as discussed previously
2. Initialize variables
isServicedCharged is initialized to false
Read the beginning balance in the variablebeginningBalance from the file and initialize the variable
accountBalance to the value of the variable
beginningBalance
Since the data will be read from a file, you need to open inputfile
ProgrammingExample-2
67
Steps (continued)
3. Get account number and starting balance
infile >> acctNumber >> beginningBalance;
4. Get transaction code and transaction amount
infile >> transactionCode
>> transactionAmount;
5. Analyze transaction code and update appropriate
variablesProgrammingExam
ple-2
68
-
7/31/2019 Lecture 5C for Section-C
18/19
11/2/2011
18
Steps (continued)6. Repeat Steps 4 and 5 until there is no more data
Since the number of entries in the input file is not known, usean EOF-controlled while loop
7. Print the result
ProgrammingExample-2
69
Main Algorithm
1. Declare and initialize variables
2. Open input file
3. If input file does not exist, exit
4. Open output file
5. Output numbers in appropriate formats
6. Read accountNumber and beginningBalance
ProgrammingExam
ple-2
70
Main Algorithm (continued)7. Set accountBalance to beginningBalance
8. Read transactionCodeand transactionAmount
9. while (not end of input file) if transactionCode is 'D' or 'd'
i. AddtransactionAmount to
accountBalance
ii. Increment numberOfDeposits
if transactionCode is 'I' or 'i' i. Add transactionAmount to accountBalance
ii. Add transactionAmount to interestPaid
ProgrammingExample-2
71
Main Algorithm (continued)
IftransactionCode is 'W' or 'w'
i. Subtract transactionAmount from
accountBalance
ii. Increment numberOfWithdrawals
iii. if (accountBalance < MINIMUM_BALANCE
&& !isServicedCharged)1. Subtract SERVICE_CHARGE from accountBalance
2. Set isServiceCharged totrue
IftransactionCode is other than 'D', 'd', 'I','i', 'W', or 'w', output an error message
10. Output the results
ProgrammingExam
ple-2
72
11/2/2011
-
7/31/2019 Lecture 5C for Section-C
19/19
11/2/2011
19
Write C code for two programming Examples
Will be discussed in next lectures
CS141-Programming
Fundam
entals-JameelAhmad
73