0% found this document useful (0 votes)
48 views25 pages

Structured Programming: Dr. Ahmed Sherif Zekri

The document provides an overview of structured programming concepts including: 1) The course goals are to introduce programming concepts, teach problem solving skills using C programming language, and for students to become junior C programmers. 2) The course outline covers introduction to programming, problem solving methods, data types, selection and repetition structures, functions, arrays, and multidimensional arrays. 3) Lecture one provides an overview of computers, programming, problem solving, what programming is, the programming life cycle, algorithm control structures, and a sample payroll problem and solution.

Uploaded by

Muhammad Emam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views25 pages

Structured Programming: Dr. Ahmed Sherif Zekri

The document provides an overview of structured programming concepts including: 1) The course goals are to introduce programming concepts, teach problem solving skills using C programming language, and for students to become junior C programmers. 2) The course outline covers introduction to programming, problem solving methods, data types, selection and repetition structures, functions, arrays, and multidimensional arrays. 3) Lecture one provides an overview of computers, programming, problem solving, what programming is, the programming life cycle, algorithm control structures, and a sample payroll problem and solution.

Uploaded by

Muhammad Emam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Structured Programming

Arab Academy for Science and Technology


CC112, Spring 2012

Dr. Ahmed Sherif Zekri

1
Course Goals
• Introduce General concepts of
Programming

• Begin to Think Like a Programmer

• Start Programming With C (a powerful,


widely-used programming language)

• Become Junior C Programmer


2
Course Outline
• An introduction to computer programming
• Problem solving skills and software
development methods
• Data types, operators and simple functions
• Selection structure (IF and Switch
statements)
• Repetition and loop statements
• Function & modular programming
• Arrays & applications
• Multidimensional arrays
3
Lecture One

Overview of Computer,
Programming and Problem Solving

4
Outline
• Overview of Computer, Programming and
Problem Solving

• What is Programming?

• Programming Life-Cycle Phases

• Algorithm Basic Control Structures

• Sample problem 5
Background
?What is a Computer
• Device capable of performing computations and
logical decisions

• Perform these tasks millions and even billions of


times faster than people can

• Computers process data through sets of


instructions called computer programs (s.w.)

• Programs guide the computer through actions as


specified by people called computer programmers; For
this course, that will be YOU.
6
Background
Computer Components

7
?What is Programming

• Given a well-defined problem:

1. Find an algorithm to solve a problem.


2. Express that algorithm in a way that the
computer can execute it.

8
Programming Life Cycle Phases

1. Analyze the problem.


• This involves identifying the data you have to work with it,
the desired results, and any additional requirements or
constrains on the solution.

2. Design the algorithm to solve the problem.


• An algorithm is a step-by-step procedure for solving a problem in
a finite amount of time.

3. Implement the algorithm.


• Each algorithm is converted into one or more steps in a
programming language. This process is called CODING.

9
… Programming Life Cycle Phases
4. Test and verify the completed program.
• Run the program several times using different sets of data, making sure
that it works correctly for every situation in the algorithm .

• if it does not work correctly, then you must find out what is
wrong with your program or algorithm and fix it--this is called
DEBUGGING

5. Maintain and update the program.


• maintenance begins when your program is put into use and accounts for
the majority of effort on most programs.

• MODIFY the program to meet changing requirements or correct


errors that show up in using it.

10
Algorithm Basic Control
Structures
• a sequence is a series of statements that execute one after
another

• selection (branch) is used to execute different statements


depending on certain condition(s)

• Looping (repetition) is used to repeat statement(s) while


certain condition(s) are met.

• a subprogram is used to break the program into smaller


units

11
Control Structures
Sequences

Statement Statement Statement ...

12
Control structures
Selection (Branching)

IF Condition THEN Statement1 ELSE Statement2

True Statement1
Statement
Condition ...
False Statement2

13
Control structures
Loop (Repetition)

WHILE Condition Statement1

False
...
Condition
Tr
ue

Statement

14
Control structures
Subprogram (Function)

PROGRAM1 ...

SUBPROGRAM1
a meaningful collection
of SEQUENCE,
SELECTION, LOOP,
SUBPROGRAM

15
Sample Problem
Company payroll case study
A small company needs an interactive
program to figure its weekly payroll.

• The payroll clerk will input data for each


employee: ID, #hours, pay rate/hour

• Each employee’s wage is calculated

• Display the total wages for the week on the


screen.

16
Sample Problem

A programmer needs an algorithm to


determine an employee’s weekly
wages. How would the calculations
be done by hand?

17
One Employee’s Wages
Ex1. In one week an employee works 52
hours at the hourly pay rate of 24.75. Assume
a 40.0 hour normal work week and an
overtime pay rate factor of 1.5
What are the employee’s wages?

40 x 24.75 = 990.00
12 x 1.5 x 24.75 = ___________
445.50
1435.50 18

18
One Employee’s Wages
Ex2. In one week an employee works 36
hours at the hourly pay rate of 24.75. Assume
a 40.0 hour normal work week and an
overtime pay rate factor of 1.5
What are the employee’s wages?

36 x 24.75 = 891.00

19

19
Weekly Wages, in General
If hours are more than 40.0, then
wages = (40.0 * payRate) +
(hours - 40.0) * 1.5 *payRate
otherwise,
wages = hours * payRate

payRate: rate per hour


hours; # of hours
20
Algorithm for Company
Payroll Program
• initialize total company payroll to 0.0
• repeat this process for each employee:
1. Get the employee’s ID (empNum)
2. Get the employee’s hourly rate (payRate)
3. Get the hours worked this week (hours)
4. Calculate this week’s wages
5. Add wages to total company payroll
• write total company payroll on screen

21
C Program
/* ***************************************************
Payroll program
This program computes each employee’s wages and
the total company payroll
***************************************************/

#include <stdio.h> /* for keyboard/screen I/O */


#define MAX_HOURS 40.0 /* Maximum normal hours */
#define OVERTIME 1.5 /* Overtime pay factor */

void CalcPay ( float, float, float& ) ;


int main( )
{
float payRate; /* Employee’s pay rate */
float hours; /* Hours worked */
float wages; /* Wages earned */
float total; /* Total company payroll */
int empNum; /* Employee ID number */ 22

22
total = 0.0 ;
printf( “Enter employee number: “); /* Prompt */
scanf(“%d” , &empNum); /* Read ID number */

while ( empNum != 0 ) /* While not done */


{
printf(“Enter pay rate: “);
scanf(“%f”, &payRate) ; /* Read pay rate */
printf(“Enter hours worked: “);
scanf(“%f”, &hours) ; /* and hours worked */

CalcPay(payRate, hours, wages); /* Compute wages */

total = total + wages; /* Add to total */

printf( “Enter employee number: “);


scanf(“%d”,&empNum); /* Read ID number */
}
printf(“Total payroll is %f “,total);
return 0 ; /* Successful completion */
} 23

23
void CalcPay ( /* in */ float payRate ,
/* in */ float hours ,
/* out */ float& wages )

/* CalcPay computes wages from the employee’s pay rate


and the hours worked, taking overtime into account
*/
{
if ( hours > MAX_HOURS )
wages = (MAX_HOURS * payRate ) +
(hours - MAX_HOURS) * payRate * OVER_TIME;
else
wages = hours * payRate;
}

24

24
Problem
• Write a program that takes the length and
width of a piece of land, and the length and
width of a house to be built inside it and
then tell you how much grass you need to
buy to cover the garden (in square meters).

25

You might also like