0% found this document useful (0 votes)
6 views32 pages

C++ CH-1 contd

Chapter 1 introduces programming, defining it as the process of writing and maintaining source code in programming languages. It explains the distinction between low-level and high-level languages, the importance of algorithms, and problem-solving techniques in programming. Additionally, it covers tools like pseudocode and flowcharts for planning and representing algorithms.

Uploaded by

rajizerihun140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views32 pages

C++ CH-1 contd

Chapter 1 introduces programming, defining it as the process of writing and maintaining source code in programming languages. It explains the distinction between low-level and high-level languages, the importance of algorithms, and problem-solving techniques in programming. Additionally, it covers tools like pseudocode and flowcharts for planning and representing algorithms.

Uploaded by

rajizerihun140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Chapter 1

Introduction to Programming

The best way to start learning a programming language is by


writing a program!!!
What is a program?
• Computer programs, software programs, or
just programs are the instructions that tells
the computer what to do.

• Computer programming (programming or


coding) is the process of writing, testing,
debugging/troubleshooting, and maintaining
the source code of computer programs
Intro
• A computer program consists of two elements:
– Code – action
– Data – characteristics
• Computer programs (also know as source code)
is often written by professionals known as
Computer Programmers. Source code is written
in one of programming languages.
Programming language
• A programming language is an artificial language
that can be used to control the behavior of a
machine, particularly a computer.
• Programming languages, like natural language,
are defined by syntactic and semantic rules which
describe their structure and meaning respectively.
• The syntax of a language describes the possible
combinations of symbols that form a syntactically
correct program.
• The meaning given to a combination of symbols is
handled by semantics.
Programming language
• A main purpose of programming languages is to
provide instructions to a computer.
• Available programming languages come in a
variety of forms and types.
• Programming languages can be divided in to two
major categories:
– Low-level languages
– High-level languages
Low-level Languages
Machine language
It is the lowest level programming language in which all instructions and
data are written in a binary machine code, i.e. 0s and 1s.
Computers only understand one language and that is binary language or
the language of 1s and 0s.
•For example:
00101010 000000000001 000000000010
10011001 000000000010 000000000011
In the initial years of computer programming, all the instructions were
given in binary form. Although the computer easily understood these
programs, it proved too difficult for a normal human being to remember
all the instructions in the form of 0s and 1s.
Therefore, computers remained mystery to a common person until
other languages such as assembly language was developed, which were
easier to learn and understand.
Low-level language
Assembly language
 correspondences symbolic instructions and executable machine codes
and was created to use letters (called mnemonics) to each machine
language instructions to make it easier to remember or write.
For example:
 ADD A, B – adds two numbers in memory location A and B
However, no matter how close assembly language is to machine code,
computers still cannot understand it.
The assembly language must be translated to machine code by a separate
program called assembler.
The machine instruction created by the assembler from the original
program (source code) is called object code. Thus assembly languages are
unique to a specific computer (machine).
High-level Languages
• Use naturally understandable languages
• IDE’s are used for rapid development like C+
+, .Net environment
• they permitted a programmer to ignore many
low-level details of the computer's hardware.
• closer the syntax, rules, and mnemonics to
"natural language“.
• High-level languages are more English-like and,
therefore, make it easier for programmers to
"think" in the programming language.
High-level language
• High-level languages also require translation to
machine language before execution.
• This translation is accomplished by either a
compiler or an interpreter.
– Compilers translate the entire source code program
before execution.
– Interpreters translate source code programs one line at a
time. Interpreters are more interactive than compilers.
• E.g, FORTRAN (FORmula TRANslator), BASIC
(Bingers All Purpose Symbolic Instruction Code),
PASCAL, C, C++, Java.
Problem Solving Techniques
• Computer solves varieties of problems that can be
expressed in a finite number of steps leading to a
precisely defined goal by writing different programs.
• A program is not needed only to solve a problem but
also it should be reliable, (maintainable) portable and
efficient.
• In computer programming two facts are given more
weight:
– The first part focuses on defining the problem and logical
procedures to follow in solving it.
– The second introduces the means by which programmers
communicate those procedures to the computer system so
that it can be executed.
Cont’d
There are system analysis and design tools, particularly
flowchart and structure chart, that can be used to define
the problem in terms of the steps to its solution.
The programmer uses programming language to
communicate the logic of the solution to the computer.
Before a program is written, the programmer must clearly
understand what data are to be used, the desired result, and
the procedure to be used to produce the result.
The procedure, or solution, selected is referred to as an
algorithm.
An algorithm is defined as a step-by-step sequence of
instructions that must terminate and describe how the data
is to be processed to produce the desired outputs.
Algorithms
• Simply, Algorithm is a sequence of instructions.
• An algorithm is the plan for writing a program.
• Algorithms are a fundamental part of computing.
• The steps required for solving a problem are listed by
using an algorithm tool.
Algorithm tools make program solutions:
– more clear,
– more understandable,
– easier to remember.
• Algorithms are written according to rules so that other
programmers are also able to read and understand the
solution easily.
Algorithms
An algorithm must satisfy the following requirements:

Unambiguousness: i.e. it must not be ambiguous. Every step


in an algorithm must be clear as to what it is supposed to do and
how many times it is expected to be executed.
Generality: i.e. it should have to be general, not to be specific.
Correctness: it must be correct and must solve the problem for
which it is designed.
Finiteness: it must execute its steps and terminate in finite
time.
Tools of Algorithms
Pseudocodes is English like language for representing the solution
to a problem.
•Pseudo code is independent of any programming language. Pseudo
code (or a flow chart) is the first step in the process of planning the
solution to a problem (also called developing an algorithm).
•Pseudo code is a compact and informal high-level description of a
computer algorithm that uses the structural conventions of
programming languages, but typically omits details such as
subroutines, variables declarations and system-specific syntax.
•The purpose of using pseudocode is that it may be easier for humans
to read than conventional programming languages
•No standard for pseudocode syntax exists, as a program in
pseudocode is not an executable program.
Example: 1
#1. Write a program that obtains two integer
numbers from the user. It will print out the sum of
those numbers.
Pseudo code:
– Prompt the user to enter the first integer
– Prompt the user to enter a second integer
– Compute the sum of the two user inputs
– Display an output prompt that explains the answer as
the sum
– Display the result
Example: 2
#2. Write an algorithm to determine a student’s final
grade and indicate whether it is passing or failing. The
final grade is calculated as the average of four marks.

Pseudo code:
- Input a set of 4 marks
- Calculate their average by summing and dividing by 4
- if average is below 50
Print “FAIL”
else
Print “PASS”
Example: 2
#2. Write an algorithm to determine a student’s final
grade and indicate whether it is passing or failing. The
final grade is calculated as the average of four marks.

Detailed Algorithm
Step 1: Input M1,M2,M3,M4
Step 2: GRADE = (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Flowcharts
• A flowchart is a graphical way of representing
the solution to a problem
• The advantage of flowchart is it doesn’t depend
on any particular programming language, so that
it can used, to translate an algorithm to more
than one programming language.
• Flowchart uses different symbols (geometrical
shapes) to represent different processes.
Example 1:
• Draw flow chart of an algorithm to add two
numbers and display their result.
• Algorithm description
– Read two numbers (A and B)
– Add A and B
– Assign the sum of A and B to C
– Display the result ( c)
Example: 2
Example 3
• Write an pseudo code and draw a flow chart to check a number
is negative or not.

• Algorithm description.
1. Start
2. Read a number x
3. If x is less than zero then
Write “Negative”
Else
Write “Not Negative”
4. stop
Example 4
Write the algorithm description (Pseudo code) and Draw flow chart of an
algorithm to find the following sum.
Sum = 1+2+3+…. + 50

• Algorithm description (Pseudo code)


1. Start
2. Initialize Sum to 0 and Counter to 1
2.1 If the Counter is less than or equal to 50
• Add Counter to Sum
• Increase Counter by 1
• Repeat step 2.1
2.2 Else
• Exit Loop
3. Write Sum
4. Stop
Example 5
Write an algorithm and draw a flowchart that will read the two
sides of a rectangle and calculate its area.

Pseudocode
-Input the width (W) and Length
(L) of a rectangle
-Calculate the area (A) by
multiplying L with W
- Print A
Algorithm
Step 1: Input W,L
Step 2: A = L x W
Step 3: Print A
Example 6
Write an algorithm and draw a flowchart to calculate 2 to the power
of 4.

Algorithm:

Step 1: Base = 2
Step 2: Product = Base
Step 3: Product = Product * Base
Step 4: Product = Product * Base
Step 5: Product = Product * Base
Step 6: Print Product

What happens if you want to calculate 2 to the power of 1000?


Quiz
1. Write an algorithm description (Pseudo code) and draw a flow
chart to check a number is even or odd.

Pseudocode
-Read input of a number
-If number mod = 0
-Print "Number is Even"
-Else
-Print "Number is Odd"
Quiz
1. Swap the contents of two variables using a
third variable.
Exercises
For each of the problems below, develop a flow chart
1) Receive a number and determine whether it is odd or even.
2) Obtain two numbers from the keyboard, and determine and display which (if either) is
the larger of the two numbers.
3) Receive 3 numbers and display them in ascending order from smallest to largest
4) Add the numbers from 1 to 100 and display the sum
5) Add the even numbers between 0 and any positive integer number given by the user.
6) Find the average of two numbers given by the user.
7) Find the average, maximum, minimum, and sum of three numbers given by the user.
8) Find the area of a circle where the radius is provided by the user.
9) Swap the contents of two variables using a third variable.
10) Swap the content of two variables without using a third variable.
11) Read an integer value from the keyboard and display a message indicating if this
number is odd or even.
12) Read 10 integers from the keyboard in the range 0 - 100, and count how many of
them are larger than 50, and display this result.
13) Take an integer from the user and display the factorial of that number
Assignment
Show pseudo code, algorithm and flowchart for each of the following questions
1: Write a program that calculates the sum of two input numbers and display the result.
2: Write a program to calculate the area of a circle and display the result. Use the formula: A=πr 2
where Pi is approximately equal to 3.1416.
3: Write a program that computes the average of three input quizzes, and then display the result.
4: Write a program that converts the input Fahrenheit degree into its Celsius degree equivalent. Use
the formula: C= (5/9)*F-32.
5: Create a program to compute the volume of a sphere. Use the formula: V= (4/3)* πr 3 where is pi
equal to 3.1416 approximately. The r 3 is the radius. Display result.
6: Write a program that converts the input Celsius degree into its equivalent Fahrenheit degree. Use
the formula: F= (9/5) * C+32.
7: Write and algorithm and draw a flowchart to read an employee name , overtime hours worked ,
hours absent and determine the bonus payment
Use formula bonus = 50 if (overtime – 1/3 absent )>50
= 30 if (overtime – 1/3 absent )>30
= 0 if (overtime – 1/3 absent <30
due date : next week’s this 31
class
Thanks!

You might also like