You are on page 1of 41

Programming (1)

Lecture 1

Dr. Esraa M. Elhariri


Computer Science Department,
Faculty of Computers and Information
Fayoum University
2021-2022
Course Agenda
Introduction to Basics
01 02 03 Variables and athematic
Programming
operators

Logical operators 05 Conditional 06 Applications


04
Statements

07 Mid-Term Exam 08 Loops - 1 09 Loops - 2

10 Applications 11 1D Arrays 12 Applications

Programming (1) 2021-2022


Grading

Programming (1)
1 2021-2022
Lecture Outlines
 Types of Problems
 Problem Solving Steps
 Specification of needs
 Problem analysis
 Design and algorithmic representation
 Implementation
 Testing and verification
 Documentation
 Problem Solving Strategies
 Algorithm Design Techniques
 Review Flowcharts
Programming (1)
1 2021-2022
Introduction

• Problem is any question or matter involving doubt,


uncertainty, or difficulty that requires logical
thought or mathematics to solve it.

Programming (1) 2021-2022 5


Yaah !
Idea

Last
Think step !!

Design
Algorithm
Write Code
Introduction to Programming

 Programming is a problem solving activity. When you write a


program, you are actually writing an instruction for the computer
to solve something for you.

 Problem solving is the process of transforming the


description of a problem into a solution by using our
knowledge of the problem domain and by relying on our ability
to select and use appropriate problem-solving strategies,
techniques and tools.

Programming (1) 2021-2022 7


Types of Problems
Problems

Algorithmic Solutions Heuristic Solutions

Problems that can be solved Problem solution requiring reasoning


with a series of actions. based on knowledge and experience,
◦ Balancing checkbook, baking and a process of trial and error.
a cake ◦ Solutions cannot be reached through
◦ Alphabetizing 10,000 names a direct set of steps
◦ How to buy the best stock or expand
the company
◦ Difficult to evaluate the solution
Programming (1) 2021-2022
Types of Problems
Problems

Algorithmic Solutions Heuristic Solutions


Computers are better at solving People are better at solving
complicated calculus problems heuristic problems.
or alphabetizing 10,000 names.
◦ Artificial intelligence is the
◦ This class deals with field of computer science that
algorithmic solutions deals with heuristic problems

Programming (1) 2021-2022


Problem Solving Strategies

 Brainstorming

 Abstraction

 Trial and Error

 Divide and Conquer

Programming (1) 2021-2022


Introduction to Problem Solving

 As mentioned, programming is a problem-solving activity. If you are


a good problem solver, you have the potential to become a good
programmer.

 Problem solving methods vary with subject area. Business students


learn to solve problems with a systems approach, while engineering
and science students use the engineering and scientific method.

 Programmers use the software development method.

Programming (1) 2021-2022 11


Software Development Method

 Specify the problem requirements.

 Analyze the problem.

 Design the algorithm to solve the problem.

 Implement the algorithm.

 Test and verify the completed program.

 Documentation

 Maintain and update the program.

Programming (1) 2021-2022


Software Development Method

 Specifying the problem requirements forces you to state the


problem clearly and unambiguously to gain a precise
understanding of what is required for its solution.

 Objective: eliminating unimportant aspects and zero in on the


root problem.
 This goal may not be as easy to achieve as it sounds. You may, for
instance, need more information from the person who posed the problem.

Programming (1) 2021-2022


13
Software Development Method

 Analyzing the problem involves identifying the problem inputs


(the data you have to work with), outputs (the desired results),
and any additional requirements or constraints on the solution.

 At this stage, you should also determine the format in which the
results should be displayed (for example, as a table with specific
column headings) and develop a list of problem variables and
their relationships.

 These relationships may be expressed as formulas.

Programming (1) 2021-2022


14
Software Development Method

 If Steps 1 and 2 are not done properly, you will solve the wrong
problem.

 Read the problem statement carefully:

(1) to obtain a clear idea of the problem and,

(2) to determine the inputs and outputs.

Programming (1) 2021-2022


15
Example

 Compute and display the total cost of apples given the number of
pounds of apples purchased and the cost per pound of apples.
Next, summarize the information contained in the underlined phrases:

 Problem Inputs:

 quantity of apples purchased (in pounds)

 cost per pound of apples (in dollars per pound)

 Problem Output:

 total cost of apples (in dollars)

Programming (1) 2021-2022


Example

 Once you know the problem inputs and outputs, develop a list of
formulas that specify relationships between them.

 The general formula:


Total cost = Unit cost X Number of units

computes the total cost of any item purchased.

 Using the variables for our particular problem, we get the


formula:

Total cost of apples= Cost per pound X Pounds of apples

Programming (1) 2021-2022


Software Development Method

 Designing the algorithm to solve the problem requires you to


develop a list of steps called an algorithm to solve the problem
and to then verify that the algorithm solves the problem as
intended.

 Writing the algorithm is often the most difficult part of the


problem-solving process.

Programming (1) 2021-2022


18
Algorithm

 An algorithm is a sequence of well-defined instructions/steps for


solving a computational problem, i.e., for obtaining a required
output for any legitimate input in a finite amount of time.

 Always terminated giving correct result

 Efficient in terms of time and space.

Programming (1) 2021-2022


Examples of Algorithm Design Techniques

 Brute-Force:
◦ Try all possibilities
 Don’t attempt to solve every detail of the problem at the
beginning; instead, discipline yourself to use top-down design.

 Divide and Conquer:


1. Divide: Break the given problem into sub problems of same
type.
2. Conquer: Recursively solve these sub problems
3. Combine: Appropriately combine the answers

Merge Sort is a sorting algorithm. The algorithm divides the


array in two halves, recursively sorts them and finally merges
the two sorted halves.

Programming (1) 2021-2022


21
Examples of Algorithm Design Techniques

 Greedy:
 Find the local optimal step at each stage.
 Not necessarily globally optimal.
• Example:
 With a goal of reaching the largest-sum, at each step, the
greedy algorithm will choose what appears to be the
optimal immediate choice, so it will choose 12 instead of 3
at the second step, and will not reach the best solution,
which contains 99.

Programming (1) 2021-2022


Examples of Algorithm Design
Techniques
 Dynamic Programming: is a bottom-up approach we solve all
possible small problems and then combine them to obtain solutions
for bigger problems.

 Used for optimization problems

 A set of choices must be made to get an optimal solution

 Find a solution with the optimal value (minimum or maximum)

 There may be many solutions that lead to an optimal value.

 Our goal: find an optimal solution

Programming (1) 2021-2022


Example
2
4
3
d (0)
45 
 16
1 8 (1)
3 d 45
2

14
1 -5
7

5 4
6
Example
2
4
3
d ( 2)
45  12
1 8
3
2

14
1 -5
7

5 4
6
Example
2
4
3 d ( 3)
45 6
1 8
3
2

14
1 -5
7

5 4
6
Computer Instructions

 Computer instructions fall into three main categories:

1) Input instructions, used for supplying data from the


"outside world" to a program;

2) Processing instructions, used for manipulating data inside


the computer. These instructions allow us to add, subtract,
multiply, and divide;

3) Output instructions, used for getting information out of the


computer to the outside world.

Programming (1) 2021-2022


Case Study

• Problem: Compute the straight-line distance between


two points in a plane.

Programming (1) 2021-2022 28


Specification of Needs

 Specifying the problem requirements requires you to state the


problem clearly and to gain the understanding of what to be solved
and what would be the solution.

 When specifying problem requirement, we ask ourselves the


following questions:

 What the problem is.

 What the solution should provide.

 What is needed to solve it.

 If there are constraints and special conditions.


Programming (1) 2021-2022 29
Case Study

• Problem: Compute the straight-line distance between


two points in a plane.

 What the problem is.

 What the solution should provide.

 What is needed to solve it.


 If there are constraints and special conditions.

Programming (1) 2021-2022 30


Problem Analysis

 Analyzing the problem requires us to identify the following:

 Input(s) to the problem, their form and the input media to be


used

 Output(s) expected from the problem, their form and the


output media to be used

 Special constraints or conditions (if any)

 Any formulas or equations to be used

Programming (1) 2021-2022 31


Case Study

 Input?
 Point 1 coordinate
 Point 2 coordinate
 Output?
 Distance between points
 Constraint/condition?
 Nil
 Formula/equation?
 Pythagorean theorem

Programming (1) 2021-2022 32


Designing algorithm

 Designing algorithm to solve the problem requires you to


develop a list of steps, arranged in a specific logical order
which, when executed, produces the solution for a problem.

 Using top-down design (also called divide and conquer):

 You first list down the major tasks

 For each major task, you further divide it into sub-tasks


(refinement step)

 When you write algorithm, write it from the computer’s point of


view.
Programming (1) 2021-2022 33
Designing Algorithm cont..

 An algorithm must satisfy these requirements:


 It may have an input(s), but must have an output(s)

 It should not be ambiguous (there should not be different


interpretations to it. Every step in algorithm must be clear as what
it is supposed to do)

 It must be general (it can be used for different inputs)

 It must be correct and it must solve the problem for which it is


designed
 It must execute and terminate in a finite amount of time

 It must be efficient enough so that it can solve the intended


problem using the resource(1)
Programming currently available on the computer.
2021-2022 34
Case Study

Major Task:
1.Read Point 1 coordinate
2.Read Point 2 coordinate
3.Calculate distance
4.Display the computed distance

However, looking at the above algorithm, we can still further refine step
3, by introducing the formula to calculate the distance.

After refinement:
1.Read Point 1 coordinate
2.Read Point 2 coordinate
3.Distance = (𝑠𝑖𝑑𝑒1)2 + (𝑠𝑖𝑑𝑒 2)2
4.Display the computed distance
Programming (1) 2021-2022 35
Remember, the order of the steps in algorithm is

very important. Consider the following, will the

result be the same?


1. Display the distance
2. Read Point 1 coordinate
3. Compute distance
4. Read Point 2 coordinate

Programming (1)
36 2021-2022
Implementation

 The process of implementing an algorithm by writing a computer


program using a programming language (for example, using C++
language).

 The output of the program must be the solution of the intended


problem.

 The program must not do anything that it is not supposed to do (Think


of those many viruses, buffer overflows, Trojan horses, etc. that we
experience almost daily. All these result from programs doing more
than they were intended to do)

Programming (1) 2021-2022 37


Testing and Verification

 Program testing is the process of executing a program to


demonstrate its correctness.

 Program verification is the process of ensuring that a program


meets user- requirement.

 After the program is compiled, we must execute the program


and test/verify it with different inputs before the program can be
released to the public or other users.

Programming (1) 2021-2022 38


Documentation

 Writing description that explain what the program does.

 Important not only for other people to use or modify your


program, but also for you to understand your own program after
a long time.

 Can be done in 2 ways:


 Writing comments between the line of codes

 Creating a separate text file to explain the program

Programming (1) 2021-2022 39


Documentation cont…

 Documentation is so important because:

 You may return to this program in future to use the whole of or a part of it
again.

 Other programmer or end user will need some information about your
program for reference or maintenance.

 You may someday have to modify the program, or may discover some
errors or weaknesses in your program.

 Although documentation is listed as the last stage of software


development method, it is actually an ongoing process which should be
done from the very beginning of the software development process.

Programming (1) 2021-2022 40


See You Next Lecture

Programming (1)
41 2021-2022

You might also like