Week1 L1 Introduction

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Academic year:2022-2023

Semester Number:2

Programming Languages

Dr: Yasser Abdelhamid


Course Informations

• Books

Java Software Solutions Foundations of


Program Design 8th , John Lewis • William
Loftus
Course Informations

• Grades
Final exam 50
Mid-term exam 20
Coursework 30
What is this class about?
• The course is designed to develop overview of
structure elements.
• Basics of algorithms. introduction to Object-
oriented programming elements, and Java
programming elements.
• Understanding of the programming using Java
syntax.
Course overview
• Structured program development:
- Problem solving decision structure
- Repetition structures
• Subprograms:
-Procedures and
-Functions
• Structured data types:
-one/two dimension arrays
– Strings
– Dynamic data structures (pointers)
– Recursion.
Basic concepts
 Before we investigate the role of computers in
solving problems, we need to become familiar
with few basic concepts. Let’s explore what is
really meant by:
 A problem
 An algorithm
 A program
 After we define the above concepts, we will talk
about how these concepts can work together to
develop computer programs that can solve many
interesting problems.
Module 1: Introduction
Topic 1: The Big Picture
• After completing this topic , you will be able to:
1. Define the meaning of a problem.
2. Define the meaning of an algorithm.
3. Define the meaning of a program.
4. Visualize the relationship among problems,
algorithms, and programs.
Module 1: Introduction
 In this course, we are more interested in
computational problems.
 Example of Computational Problems
 A common example of computational problems is
the sorting problem.
 For example: given the input sequence
(31, 41, 59, 26, 41, 58), the solution of the sorting
problem should sort these numbers to produce the
following output
(26, 31, 41, 41, 58, 59).
Module 1: Introduction
Is it only one solution?

 It is very important to understand that, there can


be more than one “correct” solution to the same
computational problem.

 For example, there can be many ways to


compute the directions between two points
A and B in a map.
What is programing and program development life cycle ?

• Programing is the process of problem solving


• Step 1: Analyze the problem
- Understand the overall problem
- Define problem requirements
• Does a program require user interaction
– If yes, what is the input?
• What is the expected output?
- Design steps (algorithm) to solve the problem
Algorithm: step by step problem-solving process.

Copyright © 2014 Pearson Education, Inc.


What is programing and program development life cycle ?

• Step 2: Implement the algorithm


– Implement the algorithm in code
– Verify that the algorithm works
• Step 3: Maintenance
– Use and modify the program if the problem domain
changes
– If the problem is complex, divided it to sub-problems
– Analyze each sub-problem as above

Copyright © 2014 Pearson Education, Inc.


Example

• Write a program to find area of rectangle?


The area of rectangle is given by the following formula:
area=rect length* rect width
Input
Rectangle length, Rectangle width
Process
area=rect length* rect width
Output
Print out the area.

Copyright © 2014 Pearson Education, Inc.


Program design and algorithms
• Implementation
• Test
• Maintenance

Copyright © 2014 Pearson Education, Inc.


Definition of Algorithm
 A solution to a problem
 A way of doing things
 Step by step instructions on how to solve a
problem

14
 Let N=0;
 For each child in room then
 Set N=N+1

15
Module 1: Introduction
 Definition of an Algorithm?
 Since our focus in this course is on computational
problems, an algorithm can be informally defined
as “any well-defined computational procedure
that takes some value, or set of values, as input
and produces some value, or set of values, as
output ”

 Thus, an algorithm can be viewed as a sequence of


computational steps that transform the input into
the output.
Copyright © 2014 Pearson Education, Inc.
Module 1: Introduction
 What's an Algorithm?

Copyright © 2014 Pearson Education, Inc.


Module 1: Introduction
• There are two commonly used tools to help to
document program logic (the algorithm).
– flowcharts
– Pseudocode.

Copyright © 2014 Pearson Education, Inc.


Pseudocode
• pseudocode is the plain English representation of a
computer program or algorithm, which specifies the
flow and operation of the program.
• It is generally used to represent the structural flow
of a program, and it is not associated with any
specific programming language.
• It a perfect tool to represent algorithms for various
problems.
• There is no "correct" way to write it - as long as it
makes sense.

Copyright © 2014 Pearson Education, Inc.


Pseudocode
• It is not a programming language.
• It is just a learning and reasoning tool, which is
used by programmers and developers to underline
how to write the actual code.
• Pseudocode can not be executed or compiled by
any compiler, interpreter, or assembler.
• Unlike programming language code, pseudocode
does not follow a strict structure and syntax. Here,
the programmer can write the code syntax as he
pleases.

Copyright © 2014 Pearson Education, Inc.


Pseudocode
How to Write Pseudocode?
1. START: To begin the pseudocode.
2. INPUT: Take input from the user.
3. PRINT: To print the output on the screen.
4. READ/GET: Input format while reading data from the file.
5. SET, INIT: Initialize a value.
6. INCREMENT, BUMP: Increase the value of the variable,
equivalent to a++.
7. DECREMENT: Decrease the value of the variable,
equivalent to a–.
8. COMPUTE, CALCULATE, DETERMINE: To calculate the
expression result.
Copyright © 2014 Pearson Education, Inc.
Example
• Write an algorithm to calculate Summation of two
Numbers
Algorithm:
Begin
Declare x,y,sum=0
Input x,y
Sum=x+y
Print sum
End

Copyright © 2014 Pearson Education, Inc.


Example
• Write an algorithm to calculate average of three
numbers
Algorithm:
Begin
Declare x, y,z, sum=0, avg=0
Input x, y, z
Sum=x+y+z
Avg=sum/3
Print avg
End

Copyright © 2014 Pearson Education, Inc.


Example
• Write an algorithm to take your birth year and print
your age
Algorithm:
Start
Declare x, age=0
Read birth year in x
Set age = 2022 - x
Print age
End

Copyright © 2014 Pearson Education, Inc.


Example
• Write an algorithm to take the length of a rectangle
sides and calculate its perimeter and area.
Algorithm:
Start
Declare w, h, area=0, perimeter=0
Read width and height in w and h
area = h * w
perimeter = 2 * (h + w)
Print rectangle area = area and perimeter = perimeter
End

Copyright © 2014 Pearson Education, Inc.


Example
Write an algorithm that take a number from the user and if the
entered number is positive subtract 5 from it. if it is negative
add 5 to it then print the result in either cases.
Algorithm:
Start
Declare num
Read num
If num>0
num=num-5
else
num=num+5

Print num
End
Copyright © 2014 Pearson Education, Inc.
Example
• Write an algorithm to print “passed” if student grades
equal or more than 60 and print ”faild” in other cases.
Start
Declare grade
Read grade
If grade>=60
Print passed”
else
Print””Faild”
end

Copyright © 2014 Pearson Education, Inc.


Example
• Write an algorithm to check if number entered by
user odd or even
Start
Declare variable Number
Read Number
If Number%2 == 0 then
print “Even Number”
else
print “Odd Number”
end

Copyright © 2014 Pearson Education, Inc.


Example
• Write an algorithm to print the largest among three different
numbers entered by user.
• Pseudocode of the algorithm:
Start
Declare variables a,b,c
Read a,b,c
If a>b then
if a>c then
print “a”
else
print “c”
Else
If b>c then
Print “b”
Else
Print”c”
end Copyright © 2014 Pearson Education, Inc.
??
Got it

Algorithm
Program 30

(Pseudo-code)
What is a program?
• Before a machine such as a computer can perform a task, an algorithm
for performing that task must be discovered and represented in a form
that is compatible with the machine so the machine can understand it.

• A representation of an algorithm is called a program.

• The process of developing a program is known as “programming”

• In order to program a computer, we need a programming language.

• A programming language is a special language used to write computer


programs.

Copyright © 2014 Pearson Education, Inc.


What is a program?

Copyright © 2014 Pearson Education, Inc.


Putting it all together
• Given a real-life computational problem. Let us consider
again the Sorting Problem.

• The sorting algorithm should provide us with well-defined


computational steps that can be followed to sort the input
sequence of numbers.

• Once the algorithm is designed, we need to encode the


steps of the algorithm into a language that a computer can
understand. This step is usually known as programming the
computer.

Copyright © 2014 Pearson Education, Inc.


Putting it all together
• As we mentioned before, in order to program the computer
we need to use a “programming language” that a computer
can understand.

• There are many programming languages that can be used to


encode the sorting algorithm. Selecting the appropriate
programming language depends on the nature of the
application. We will discuss this in more details in a later
chapter.

• After choosing the appropriate programming language, we


use this language to write a program that performs the
computational steps specified in the sorting algorithm.
Copyright © 2014 Pearson Education, Inc.

You might also like