Sei sulla pagina 1di 48

Lecture-03

Structured Program Development in C

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


UNDERSTANDING A PROBLEM
 A procedure for solving a problem in terms of
1. The actions to be executed, and
2. The order in which these actions are to be executed
is called an algorithm.

 To make it Simple
 “Which statements are to be executed and in which order is called an algorithm”
 And
 “Specifying the order in which statements are to be executed in a computer
program is called program control”.

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


ALGORITHM: ORDER IS IMPORTANT : “RISE-AND-SHINE ALGORITHM”
1. Get out of bed 1. Get out of bed
2. take a shower 2. get dressed
3. get dressed 3. take a shower
4. eat breakfast 4. eat breakfast
5. carpool to work 5. carpool to work

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


PSEUDOCODE
 Pseudocode is an artificial and informal language that helps you develop
algorithms.
 help you “think out” a program before attempting to write it in a programming
language

 Pseudocode consists only of action statements


 For example
 input, output, or a calculation

 “int i;” is not an action statement

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FLOWCHARTS
 A flowchart is a graphical representation of an algorithm or of a portion of an
algorithm.
 Drown using
 Rectangles
 Diamonds (Decision Symbol)
 Ovals
 Small circles

 Connected using “Flowlines”

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


CONTROL STRUCTURE

Sequential Transfer of
Execution Control

 All programs could be written in terms of only three control structures


1. Sequence Structure
2. Selection Structure
3. Repetition Structure

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


CONTROL STRUCTURE
1. Sequence Structure
2. Selection Structure
1. If (Single selection statement )
2. If…else (Double selection statement )
3. Switch (Multiple selection statement )
3. Repetition Structure
1. While
2. Do..while
3. for

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


THE IF SELECTION STATEMENT
Pseudocode If Statement

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


THE IF…ELSE SELECTION STATEMENT
Pseudocode If Statement

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


CONDITIONAL OPERATOR (?:)
 Closely related to if…else statement

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


NESTED IF…ELSE STATEMENTS
Pseudocode Nested if…else statements

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


COMPOUND STATEMENT OR A BLOCK

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


THE WHILE REPETITION STATEMENT
 A repetition statement allows you to specify that an action is to be repeated while
some condition remains true.

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS CASE STUDY 1:
COUNTER-CONTROLLED REPETITION
 Problem

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS CASE STUDY 1:
COUNTER-CONTROLLED REPETITION

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS CASE STUDY 1:
COUNTER-CONTROLLED REPETITION

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE REFINEMENT
CASE STUDY 2: SENTINEL-CONTROLLED REPETITION
 Problem

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE REFINEMENT
CASE STUDY 2: SENTINEL-CONTROLLED REPETITION
 Sentinel value (signal value, a dummy value, or a flag value)
 Top-down, stepwise refinement ?
 Begin with top

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE REFINEMENT
CASE STUDY 2: SENTINEL-CONTROLLED REPETITION

First Refinement

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE REFINEMENT
CASE STUDY 2: SENTINEL-CONTROLLED REPETITION

Second
Refinement

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE REFINEMENT
CASE STUDY 2: SENTINEL-CONTROLLED REPETITION

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE REFINEMENT
CASE STUDY 2: SENTINEL-CONTROLLED REPETITION

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE REFINEMENT
CASE STUDY 2: SENTINEL-CONTROLLED REPETITION

float

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE REFINEMENT
CASE STUDY 2: SENTINEL-CONTROLLED REPETITION

(float ) total

%.2f

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE REFINEMENT
CASE STUDY 2: SENTINEL-CONTROLLED REPETITION

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE
REFINEMENT CASE STUDY 3: NESTED CONTROL STRUCTURES

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE
REFINEMENT CASE STUDY 3: NESTED CONTROL STRUCTURES
 After reading the problem statement carefully, we make the following observations:
1. The program must process 10 test results. A counter-controlled loop will be
used.
2. Each test result is a number—either a 1 or a 2. Each time the program reads a
test result, the program must determine if the number is a 1 or a 2. We test for a 1
in our algorithm. If the number is not a 1, we assume that it’s a 2.
3. Two counters are used—one to count the number of students who passed the
exam and one to count the number of students who failed the exam.
4. After the program has processed all the results, it must decide if more than 8
students passed the exam.

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE
REFINEMENT CASE STUDY 3: NESTED CONTROL STRUCTURES
 Top-down, stepwise refinement.

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE
REFINEMENT CASE STUDY 3: NESTED CONTROL STRUCTURES

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE
REFINEMENT CASE STUDY 3: NESTED CONTROL STRUCTURES

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE
REFINEMENT CASE STUDY 3: NESTED CONTROL STRUCTURES

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE
REFINEMENT CASE STUDY 3: NESTED CONTROL STRUCTURES

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE
REFINEMENT CASE STUDY 3: NESTED CONTROL STRUCTURES

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE
REFINEMENT CASE STUDY 3: NESTED CONTROL STRUCTURES

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


FORMULATING ALGORITHMS WITH TOP-DOWN, STEPWISE
REFINEMENT CASE STUDY 3: NESTED CONTROL STRUCTURES

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


ASSIGNMENT OPERATORS

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


INCREMENT AND DECREMENT OPERATORS

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


INCREMENT AND DECREMENT OPERATORS

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


PRECEDENCE AND ASSOCIATIVITY OF THE OPERATORS ENCOUNTERED

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


CLASS ACTIVITY

 Write four different C statements that each add 1 to integer variable x.


 Write a single C statement to accomplish each of the following
 Multiply the variable product by 2 using the = and * operators.
 Decrement the variable x by 1, then subtract it from the variable total.
 Calculate the remainder after q is divided by divisor and assign the result to q. Write this
statement two different ways.
 Print the floating-point value 3.14159 with three digits to the right of the decimal point.
What value is printed?

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


CLASS ACTIVITY

 Write a program that calculates the sum of the integers from 1 to 10. Use the while
statement to loop through the calculation and increment statements. The loop
should terminate when the value of x becomes 11.

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


CLASS ACTIVITY

 Identify and correct the errors in each of the following:

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


CLASS ACTIVITY

 Identify and correct the errors in each of the following:

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


CLASS ACTIVITY

 (Sales Commission Calculator) One large chemical company pays its salespeople
on a commission basis. The salespeople receive PKR.200 per week plus 9% of their
gross sales for that week. For example, a salesperson who sells PKR.5000 worth of
chemicals in a week receives PKR.200 plus 9% of PKR.5000, or a total of PKR.650.
Develop a program that will input each salesperson’s gross sales for last week and
will calculate and display that salesperson's earnings. Process one salesperson's
figures at a time. Here is a sample input/output dialog:

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


HOMEWORK-2
DUE DATE: MONDAY 28TH NOVEMBER 2016
1. (World Population Growth Calculator) Use the web to determine the current
world population and the annual world population growth rate. Write an
application that inputs these values, then displays the estimated world
population after one, two, three, four and five years.

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


HOMEWORK-2
DUE DATE: MONDAY 28TH NOVEMBER 2016
1. (Enforcing Privacy with Cryptography) The explosive growth of Internet
communications and data storage on Internet-connected computers has greatly
increased privacy concerns. The field of cryptography is concerned with coding data
to make it difficult (and hopefully—with the most advanced schemes—impossible)
for unauthorized users to read. In this exercise you’ll investigate a simple scheme for
encrypting and decrypting data. A company that wants to send data over the Interne
has asked you to write a program that will encrypt it so that it may be transmitted
more securely. All the data is transmitted as four-digit integers. Your application
should read a four-digit integer entered by the user and encrypt it as follows:
Replace each digit with the result of adding 7 to the digit and getting the remainder
after dividing the new value by 10. Then swap the first digit with the third, and swap
the second digit with the fourth. Then print the encrypted integer. Write a separate
application that inputs an encrypted four-digit integer and decrypts it (by reversing
the encryption scheme) to form the original number. [Optional reading project:
Research “public key cryptography” in general and the PGP (Pretty Good Privacy)
specific public key scheme. You may also want to investigate the RSA scheme, which
is widely used in industrial-strength applications.]

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


 Structured Program Development in C, Chapter-3 from text book.

Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST


Engr. Asim Javaid, Lecturer, Department of Software Engineering, MUST

Potrebbero piacerti anche