PF-Week 01

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 58

Programming

Fundamental

Week 01

Sobia Iftikhar
[email protected]
Objectives of course

1. Getting acquainted with problem understanding, modeling and solving.


2. Understanding the concept of Programming Languages.
3. Two major areas to be covered:
i. Computation and problem solving
ii.Implementation in C language.
4. Design and implement algorithms to solve real world problems.
Course outline

Week 05
Discussion of the course outline, Introduction

Week 10
problem solving with decisions and iterative Constant, Variables, Keywords,

Week
to problem solving, What is algorithm, How to structures, Basic Computer Organization, Intro
Escape sequence, Format Specifiers,
write pseudo code, programming structures, to IDE (compiled program, text editors,

01
problem solving with the sequential structures debuggers, etc), Program structure and Data types, Data manipulation,
and, Basic Flowchart, IPO and PAC. Execution, First Program with Input and Output. Library, Linking, Compiling & Loading.

Week 06 – MID 1 Week 11– MID 2

Week 12
Decision Control Structures: If Nested if statements & switch
Week 02

Week 07
Introduction to Loops Design,
statements and if-else statement, statements 2. Logical &
Basic switch statements, Some
For, while and do-while loops,
Conditional Operators 3. Working
working examples. examples. Some working examples.
Week 03

Week 13
Week 08
Nested Loops, Break and Introduction to 1D Arrays, Functions: Declaration, Definition and
Calling, Passing values to functions, Passing
Continue Statement, Multiple subscripted arrays to functions, Standard library string
Working examples. arrays, Working examples. functions, 2D array of characters.
Week 04

Week 14
Week 09
Recursion 2. Introduction Nested structures, Passing
Dynamic memory
to Structures and Structure structure function 2. Introduction
to pointers 3. Pointers and Arrays. allocation 2. Void pointers
array 3. Working examples.
Week 15- Revision
Week 16- FINAL EXAM
Grading Scheme
– Mid-1 & 2: 15 each
– Assignment: 20
– Final: 50
Class 01
06-Sep-2021
Content
– Introduction to problem solving
– What is algorithm, How to write pseudo code
– Programming structures
– Problem solving with the sequential structures
– Basic Flowchart, IPO and PAC
Problem solving Concepts

– People make decisions every day to solve problems that affect their lives. If a
bad decision is made, time and resources are wasted, so it’s important that
people know how to make decisions well.
– For this, There are six steps to follow to ensure the best decision.
These six steps in problem
solving include the following:

List instructions that


enable you to solve the
1 2 3 alternative4ways
Identify 5
problem using 6the
Identify the problem to solve the problem. selected solution.
Problem

– Consider the following problem.


– “what to do this evening.”
Solution

1. Identify the problem. How do the individuals wish to spend the evening?
2. Understand the problem. With this simple problem, also, the knowledge base of the participants must be
considered. The only solutions that should be selected are ones that everyone involved would know how to do. You
probably would not select as a possible solution playing a game of chess if the participants did not know how to play.
3. Identify alternatives.
– a. Watch television.
– b. Invite friends over.
– c. Play video games.
– d. Go to the movies.
– e. Play miniature golf.
– f. Go to the amusement park.
– g. Go to a friend’s party.
– The list is complete only when you can think of no more alternatives.
Solution (cont.)

4. Select the best way to solve the problem.


– Weed out alternatives that are not acceptable, such as those that cost too much money or do not
interest one of the individuals involved.
– Specify the pros and cons of each remaining alternative.
– Weigh the pros and cons to make the final decision. This solution will be the best alternative if all
the other steps were completed well.

5. Prepare a list of steps (instructions) that will result in a fun evening.


6. Evaluate the solution. Are we having fun yet? If nobody is having fun, then the
planner needs to review the steps to have a fun evening to see whether anything can be
changed, if not then the process must start again.
Types of Problem

Problems with algorithmic Problems with heuristic


solutions solutions
Problems with algorithmic solutions

– Can be solved in a number of steps e.g. balancing a check book.


– Algorithm: A number of steps that may be performed repeatedly to reach some
solution
Problems with heuristic solutions

– Solutions that cannot be reached through a direct set of steps are called heuristic
solutions.
– These solutions require reasoning built on knowledge and experience, and a
process of trial and error.
– Example: how to buy the best stock or whether to expand the company
Problems with heuristic solutions

First guess:
Arrange a number in v = 1+3+5 =
way that sum of vertical 9
is equal to sum of H = 2+3+4 =
horizontal? 9

First guess:
v = 1+2+4 =
7
H = 3+2+5 =
10
Class 02
07-Sep-2021
Problem solving with
computers
The problem-solving process is not easy. It takes
practice and time to perfect, but Program means the set of instructions that make
in the long run the process proves to be of great up the solution after they have been coded into a
benefit. particular computer language.

Output/Results means the


outcome or the completed
computer-assisted answer.
Instructions

– Instruction can be written in two ways:


– Pseudo code
– Algorithm
Algorithm

– The algorithm is procedure or step by step instruction that you tell computer to do.
– To make a computer to do some task, one need a computer program.
– To write computer program, computer needs step by step statement, what you
exactly want to do.
– Its better to write algorithm before writing the actual computer program
– Properties of algorithm:
– Given problem should be broken into meaning full step
– Should be numbered sequentially
– Should be descriptive in simple English.
Example
Pseudo code

– Pseudo code describes how you would implement an algorithm without getting
into syntactical details
– Pseudo code is an informal way of writing a program. It is not exactly a
computer program. It represents the algorithm of the program in natural
language and mathematical notations.
Example

– Pseudo code to add 2 numbers is as follows:


o SumOfTwoNumbers()
o Begin
o Set sum=0;
o Read: num1, num2;
o Set sum = num1+num2;
o Print sum;
o End
Difficulties in Problem solving
The problem-solving process is not easy. takes practice and time to perfect

When solving problems on the computer, one of the most difficult tasks Insufficient
Incorrect prob
st
Poor training
Afraid to mak
for the problem solver is writing the instructions. definition
algorithm
Take the task of deciding which number is the largest from a
group of three numbers. Almost anyone can immediately tell which
is the largest, but many cannot explain the steps they followed to arrive
at it.
What Problem Can Be Solved
By Computer

 When the solution can be produced by a set of step-by-step procedures or


actions.
 This step-by-step action is called an algorithm.
 The algorithm will process some inputs and produced output.
 Solving problem by computer undergo two phases:
 Phase 1:
 Organizing the problem or pre-programming phase.

 Phase 2:
 Programming phase.
Pre-programming Phase

Writing the algorithms.


Drawing the Program
flowcharts.
Developing the Input-
Process-Output (IPO)
Chart.
Developing the
Hierarchy Input
Process Output
(HIPO) chart or
Analyzing the Interactivity Chart
problem. (IC).
Pre-programming Phase

1. Analyzing The Problem


 Understand and analyze the problem to determine whether it can be solved by a
computer.
 Analyze the requirements of the problem.
 Identify the following:
 Data requirement.
 Processing requirement or procedures that will be needed to solve the problem.
 The output.
Pre-programming Phase

 All These requirements can be presented in a Problem Analysis Chart


(PAC)

Data Processing Output


given in the problem or List of processing Output requirement.
provided by the user required or procedures.
Pre-programming Phase

 Example: Payroll Problem


 Calculate the salary of an employee who works by hourly basis. The formula to be
used is
 Salary = Hour works * Pay rate

Data Processing Output

Hours work, Salary = Hours work * payrate Salary


Pay rate
Problem 1

Write a Problem Analysis Chart (PAC) to convert the distance in miles to kilometers
where 1.609 kilometers per mile.

Data Processing Output


Distance in miles Kilometers = 1.609 Distance in
x miles kilometers
Problem 2

Write a Problem Analysis Chart (PAC) to find an area of a circle


where area = pi * radius * radius

Data Processing Output


radius area = 3.14 x radius x radius area
Problem 3
Write a Problem Analysis Chart (PAC) to compute and display the temperature
inside the earth in Celsius and Fahrenheit. The relevant formulae are
Celsius = 10 x (depth) + 20
Fahrenheit = 1.8 x (Celsius) + 32

Data Processing Output

depth celsius = 10 x (depth) + 20 Display celsius,


fahrenheit = 1.8 x (celsius) + 32 Display fahrenheit
PRE-PROGRAMMING PHASE

2. Developing the Hierarchy Input Process Output (HIPO) or Interactivity


Chart
 The problem is normally big and complex.
 Thus, requires big program.
 Thus, the processing can be divided into subtasks called modules.
 Each module accomplishes one function.
 These modules are connected to each other to show the interaction of processing
between the modules.
Pre-programming Phase

 Main/control module controls the flow all other modules.


 The IC is developed using top-down-method: top to down left to right order
(also refer to order of processing).
 Modules are numbered, marked for duplication, repetition or decision.
Pre-programming Phase

– The interaction will form a hierarchy, called Hierarchy Input Process Output
Chart (HIPO) or Interactivity Chart (IC). Programming which use this
approach (problem is divided into subtasks) is called Structured Programming.

Main Module

Module 1 Module 2 Module 3

Module 4 Module 5 Module 6


Pre-programming Phase

3. Developing the Input Process Output (IPO) Chart


 Extends and organizes the information in the Problem Analysis Chart.
 It shows in more detail what data items are input, what are the processing or
modules on that data, and what will be the result or output.
 It combines information from PAC and HIPO Chart.
Pre-programming Phase

Input Processing Module Output


-Hours Worked -Pay -Enter Hourly Worked 1100 -Net pay
Rate -Enter Pay Rate 1200
-Deduction -Calculate Gross Pay 1000
-Enter Deductions 2100
-Calculate Net Pay 2000
-Print Cheque 3000
-End 0000
Problem 2

Write a Input Process Output (IPO) to find an area of a circle


where area = pi * radius * radius

Input Processing Module Output


- radius - Enter radius 1000 - Area of a circle
- area = 3.14 x radius x radius 2000
-Display area 3000
-end 0000
Problem 3

Write an Input Process Output (IPO) that asks a user to enter the distance of a trip in
miles, the miles per gallon estimate for the user’s car, and the average cost of a gallon
of gas. Calculate and display the number of gallons of gas needed and the estimated
cost of the trip.
Solution
Input Processing Module Output

- Distance in - Enter distance 1100 -Total gas


miles - Enter miles per gallon 1200 needed
- Miles per - Calculate total gas 1000 - Estimated
gallon needed cost
- Cost gas per - Enter cost gas per 2100
gallon gallon
- Calculate estimated cost
2000
- Display total gas and
3000
estimated cost
- End
0000
Class 03
07-Sep-2021
Pre-programming Phase

4. Drawing the Program Flowcharts


 Flowchart is the graphic representations of the individual steps or actions to
implement a particular module.
 The flowchart can be likened to the blueprint of a building. An architect draws a
blueprint before beginning construction on a building, so the programmer draws a
flowchart before writing a program.
 Flowchart is independent of any programming language.
Pre-programming Phase

 Flowchart is the logical design of a program.


 It is the basis from which the actual program code is developed.
 Flowchart serves as documentation for computer program.
 The flowchart must be drawn according to definite rules and utilizes standard symbols
adopted internationally.
 The International Organization for Standardization (IOS) was the symbols shown below
(You can draw the symbols using ready-made flowcharting template):
Pre-programming Phase

Symbol Function
Show the direction of data flow or logical
solution.

Indicate the beginning and ending of a set of


actions or instructions (logical flow) of a
module or program.
Indicate a process, such as calculations, opening
and closing files.
PRE-PROGRAMMING PHASE

Indicate input to the program and output from the


program.

Use for making decision. Either True or False based on


certain condition.

Use for doing a repetition or looping of certain steps.

Connection of flowchart on the same page.

Connection of flowchart from page to page.


PRE-PROGRAMMING
PHASE
 Example 2.3 : Sale Problem
 Draw a flowchart for a problem that to read two numbers. The first number represents
the unit price of a product and the second number represents the quantity of the
product sold. Calculate and print the total sale.
 Solution: Stepwise Analysis of the Sale Problem
 Start of processing
 Read the unit price
 Read the quantity
 Calculate total sale
 Print total sale
 Stop the processing
PRE-PROGRAMMING
PHASE
START A

READ TOTAL SALE =


UNIT PRICE UNITPRICE ´ QUANTITY

READ PRINT
QUANTITY TOTALSALE

A STOP
PRE-PROGRAMMING
PHASE
 Finding Average Problem
 Read a sequence of number, find the average of the number and print the average.
 Solution: Stepwise Analysis of Average Problem
 Start the processing
 Read a number
 Add the number
 Repeat reading until last data
 Calculate the average
 Print the average
 Stop the processing
Start

Read
Number Pre-
programming
Accumulate
Total Repetition Until End Of Data Phase
Calculate
Average

Print
Average

Stop
Start

X
Counter

Pre-
Average = Total programmin
Read Number Counter
g Phase
Print Average
Total = Total + Number
Counter = Counter + 1

Stop

End Of Data

X
PRE-PROGRAMMING
PHASE
TASK:

Draw a flow chart to print the sum of all even numbers


between 1 to n.
Step 1: Start Solution
Step 2: Read the limit of numbers, n

Step 3: Assign i=1

Step 4: Assign sum=0

Step 5: Repeat steps 6,7&8 until i=n reaches

Step 6: If i%2==0 goto step 7

Step 7: Compute sum=sum+i

Step 8: Compute i=i+1

Step 9: Print sum of even numbers, sum

Step 10: Stop


PRE-PROGRAMMING
PHASE

5. Writing the Algorithm (Pseudocode)


 Pseudocode means an imitation computer code.
 It is used in place of symbols or a flowchart to describe the logic of a program. Thus, it
is a set of instructions (descriptive form) to describe the logic of a program.
 Pseudocode is close to the actual programming language.
 Using the Pseudocode, the programmer can start to write the actual code.
START

READ
PRICE

READ
QUANTITY
Algorithm:
SALE = PRICE ´
QUANTITY Start

Read price, quantity


PRINT Sale = price x quantity
SALE Print Sale
End

STOP
Example: Flowchart & Algorithm
Order of Execution of Instructions : Payroll System
Structuring a Program
 Develop efficient computer solution to problems:
1. Use Modules
2. Use four logic structures
a. Sequential structure

• Executes instructions one after another in a sequence.


b. Decision structure

• Branches to execute one of two possible sets of instructions.


c. Loop structure

• Executes set of instruction many times.


d. Case structure

• Executes one set of instructions out of several sets.


3. Eliminate rewriting of identical process by using modules.
4. Use techniques to improve readability including four logic structure, proper naming of
variables, internal documentation and proper indentation.
a. Sequential Logic Structure
THANK YOU

You might also like