C++ CH-1 contd
C++ CH-1 contd
Introduction to Programming
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
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
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!