1. Lecture 1
Overview of Programming and
Problem Solving
Structured Programming
1
Amr El Maghraby , Ph.D.
Computer and Systems Engineering
2. Topics
• Computer Programming
• Programming Life-Cycle Phases
• Creating an Algorithm
• Machine Language vs. High Level Languages
• Compilation and Execution Processes
• Computing-Profession Ethics
• Example
2
3. Computer components
• Input devices
Keyboard, mouse, scanner
• Central processing Unit (CPU)
• Arithmetic-logic unit (ALU)
• Control unit (CU)
• Memory:
• Random access memory (RAM)
• Read only memory(ROM)
• Output devices
Monitor,speakers, printer
• Secondary storage
Floppy Disk, Tape, CD-ROM, DVD, Hard disk
3
5. What is Computer Programming?
• It is the process of planning a sequence
of steps (called instructions) for a
computer to follow.
5
STEP 1
STEP 2
STEP 3
. . .
7. 1.Problem-Solving Phase
• ANALYZE the problem and SPECIFY
what the solution must do
• develop a GENERAL SOLUTION
(ALGORITHM) to solve the problem
• VERIFY that your solution really
solves the problem
7
8. Sample Problem
A programmer needs an algorithm to
determine an employee’s weekly
wages. How would the calculations
be done by hand?
8
9. One Employee’s Wages
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?
9
40 x 24.75 = 990.00
12 x 1.5 x 24.75 = 445.50___________
1435.50
10. 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
10
RECALL EXAMPLE
( 40 x 24.75 ) + ( 12 x 1.5 x 24.75 ) = 1435.50
11. An Algorithm is . . .
• a step-by-step procedure for solving
a problem in a finite amount of time.
11
12. Structured programming is a programming paradigm aimed at
improving the clarity, quality, and development time of a computer
program by making extensive use of subroutines, block
structures, for and while loops—in contrast to using simple tests
and jumps such as the goto statement which could lead to
"spaghetti code"
Structured programming
13. Spaghetti code
• describe source code that is
hard to follow and has a
complex and tangled control
structure, especially one using
many GOTO statements. which
is difficult both to follow and
to maintain.
14.
15. In 1979, Bjarne Stroustrup, a Danish computer scientist, began
work on the predecessor to C++, "C with Classes". The
motivation for creating a new language originated from
Stroustrup's experience in programming for his Ph.D. thesis
16. C++ is a language that has evolved much over the years, and
these course explain many features added recently to the
language. Therefore, in order to properly follow the course, a
recent compiler is needed. It shall support (even if only
partially) the features introduced by the 2011 standard.
17. What is a compiler?
Computers understand only one language and that language
consists of sets of instructions made of ones and zeros. This
computer language is appropriately called machine language.
A single instruction to a computer could look like this:
00000 10011110
A particular computer's machine language program that
allows a user to input two numbers, adds the two numbers
together, and displays the total could include these machine
code instructions:
18. This is a portion of code written in C++ that
accomplishes the exact same purpose:
int a, b, sum;
cin >> a;
cin >> b;
sum = a + b;
cout << sum << endl;
Because a computer can only understand machine language and
humans wish to write in high level languages high level languages
have to be re-written (translated) into machine language at some
point. This is done by special programs called compilers,
interpreters, or assemblers that are built into the various
programming applications.
19. The easiest way to compile C++ programs is by using an
Integrated Development Environment (IDE). An IDE generally
integrates several development tools, including a text editor and
tools to compile programs directly from it.
22. 22
Data object – The Data object, often referred to as the I/O Shape
shows the Inputs to and Outputs from a process.
Rectangle – This is used to represent an event which is controlled
within the process. Typically this will be a step or action which is
taken.
Diamond – Used to represent a decision point in the process.
Typically, the statement in the symbol will require a `yes’ or `no’
response and branch to different parts of the flowchart accordingly.
23. 23
Document – The Document object is a rectangle with a wave-like base.
This shape is used to represent a Document or Report in a process flow.
Rounded box – This is used to represent an event which occurs
automatically. Such an event will trigger a subsequent action, for
example `receive telephone call, or describe a new state of affairs.
Stored data – This is a general data storage object used in the process
flow as opposed to data which could be also stored on a hard drive,
magnetic tape, memory card, of any other storage device
24. 24
Manual input – This object is represented by rectangle with the top
sloping up from left to right. The Manual Input object signifies an
action where the user is prompted for information that must be
manually input into a system.
Direct data – Direct data object in a process flow represents
information stored which can be accessed directly. This object
represents a computer’s hard drive.
25. 25
Circle – Used to represent a point at which the flowchart connects with
another process. The name or reference for the other process should
appear within the symbol
Internal storage – This is an object which is commonly found in
programming flowcharts to illustrate the information stored in
memory, as opposed to on a file.
Predefined process – This allows you to write one subroutine and
call it as often as you like from anywhere in the code
27. 27
Some guidelines in flowcharting:
• All necessary requirements should be listed out in logical order.
• The flowchart should be clear and easy to follow.
• The usual direction of the flow of a procedure or system is from
left to right or top to bottom.
• only one flow line is utilized together with a terminal symbol.
• Write within standard symbols briefly
• It is imperative that your flowchart has a logical start and finish.
31. Implementation Phase: Test
• TESTING your program means running
(executing) your program on the
computer, to see if it produces correct
results
• if it does not, then you must find out
what is wrong with your program or
algorithm and fix it--this is called
debugging
31
32. 3. Maintenance Phase
• USE and MODIFY the program to meet
changing requirements or correct
errors that show up in using it
• maintenance begins when your
program is put into use and accounts
for the majority of effort on most
programs
32
33. Programming Life Cycle
1 Problem-Solving Phase
Analysis and Specification
General Solution ( Algorithm )
Verify
2 Implementation Phase
Concrete Solution ( Program )
Test
3 Maintenance Phase
Use
Maintain 33
35. 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 conditions
• Looping (repetition) is used to repeat statements
while certain conditions are met.
• a subprogram is used to break the program into
smaller units
35
40. 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, and each employee’s wages.
Display the total wages for the week on
the screen.
40
41. One Employee’s Wages
In one week employee ID # 4587 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?
41
40 x 24.75 = 990.00
12 x 1.5 x 24.75 = 445.50___________
1435.50
42. Week’s 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
42
RECALL EXAMPLE
( 40 x 24.75 ) + ( 12 x 1.5 x 24.75 ) = 1435.50
43. 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 payRate
3. Get the hours worked this week
4. Calculate this week’s wages
5. Add wages to total company payroll
• write total company payroll on screen
43