Programming
Fundamental
Week 01
Sobia Iftikhar
Sobia.Iftikhar@nu.edu.pk
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.
•Discussion of the course outline, Introduction to problem solving, What is algorithm, How
to write pseudo code, programming structures, problem solving with the sequential
structures and, Basic Flowchart, IPO and PAC.
Course outline
•problem solving with decisions and iterative structures, Basic Computer Organization, Intro
to IDE (compiled program, text editors, debuggers, etc), Program structure and Execution,
First Program with Input and Output.
•Constant, Variables, Keywords, Escape sequence, Format Specifiers, Data types, Data
Week 05
Week 10
manipulation, Library, Linking, Compiling & Loading.
Week
01
•Decision Control Structures: If statements and if-else statement, Basic switch statements,
Some working examples. Week 06 – MID 1 Week 11– MID 2
Week 12
Week 02
Week 07
•Nested if statements & switch statements 2. Logical & Conditional Operators 3. Working
examples.
Week 03
•Introduction to Loops Design, For, while and do-while loops, Some working examples.
Week 13
Week 08
•Nested Loops, Break and Continue Statement, Working examples.
•Introduction to 1D Arrays, Multiple subscripted arrays, Working examples.
Week 04
Week 14
Week 09
•Functions: Declaration, Definition and Calling, Passing values to functions, Passing arrays to
functions, Standard library string functions, 2D array of characters. Week 15- Revision
Week 16- FINAL EXAM
•Recursion 2. Introduction to Structures and Structure array 3. Working examples.
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:
• Identify the problem
• Understand the problem.
• Identify alternative ways to solve the problem.
1 2 3 4 5 6
• Select the best way to solve the problem from the list of alternative solutions.
• List instructions that enable you to solve the problem using the selected solution.
• Evaluate the 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 solutions
• Problems with heuristic 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 = 9
way that sum of vertical H = 2+3+4 = 9
is equal to sum of
horizontal?
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
for the problem solver is writing the instructions.
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. • Poor training
• Afraid to make decisions
• Incorrect problem definition
• Insufficient steps of algorithm
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
• Analyzing the problem.
• Developing the Hierarchy Input Process Output (HIPO) chart or Interactivity Chart (IC).
• Developing the Input-Process-Output (IPO) Chart.
• Drawing the Program flowcharts.
• Writing the algorithms.
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 needed 1000 - Estimated
gallon - Enter cost gas per gallon cost
- Cost gas per - Calculate estimated cost 2100
gallon - Display total gas and
estimated cost 2000
- End 3000
0000
THANK YOU