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

PROGRAMMING LOGIC AND DESIGN | Dr. EVELYN A.

RODRIGUEZ

WEEK 4 – INTRODUCTION TO PROGRAMMING LOGIC


Prepared by: Dr. EVELYN A. RODRIGUEZ

INTRODUCTION TO PROGRAMMING LOGIC

WHAT IS AN ALGORITHM
An algorithm is a finite set of instructions that specify a sequence of operations to be carried
out in order to solve a specific problem or class of problems.
Example. “Apply the shampoo to wet hair. Massage gently. Leave on for a few
minutes. Rinse off.”

Criteria for Algorithm


A pseudocode that does not have the properties outlined below is not an algorithm and in
general will not give the desired result when a program based on it is presented to a computer.
1. Input: There are zero or more quantities which are externally supplied.
2. Output: At least one quantity is produced.
3. Definiteness: Each instruction must be clear and unambiguous.
4. Finiteness: If we trace out the instructions of an algorithm, then for all cases, the
algorithm will terminate after a finite number of steps.
5. Effectiveness: Every instruction must be sufficiently basic that it can, in principle, be carried
out by a person using only pencil and paper.

FLOWCHART
Initially, algorithms are represented as flowcharts. Flowchart is a traditional graphical tool
using standardized symbol. It is a diagram representing the logical sequence in which combination of
steps or operations is to be performed. It is the most widely used graphic method for describing
computer operations.

Pseudocode is the textual presentation of an algorithm’s flowchart. It is a kind of stylized


English that could be translated more or less readily into a program.

Basic Symbols Used in Flowcharting


1. Terminal Symbol - used to signify the beginning and end of flowchart.

2. Preparation / Initialization Symbol – signifies the preparation of data; used to select initial
conditions; used to represent instructions or group of instructions that will alter or modify a
program’s course of execution.

TURBO C/C++ PROGRAMMING 1


PROGRAMMING LOGIC AND DESIGN | Dr. EVELYN A. RODRIGUEZ

3. Input / Output Symbol – shows input and output. Data are to be read into the computer
memory from an input device or data are to be passed from the memory to an output device.

4. Processing Symbol – performs any calculations that are to be done.

5. Decision Symbol – signifies any decisions that are to be done.

6. On-page Connector – a non-processing symbol that shows the entry or exit point of the
flowchart.

7. Off-page Connector – a non-processing symbol that designates entry to or exit from one
page when a flowchart requires more than one page.

8. Flowlines – signifies the process that is to be executed next.

TURBO C/C++ PROGRAMMING 2


PROGRAMMING LOGIC AND DESIGN | Dr. EVELYN A. RODRIGUEZ

Most Common Flowchart Symbols

Operators Used in Flowcharting

• Arithmetic Operators
Operators Meaning
+ Addition
- Subtraction
* Multiplication
/ Division

• Relational Operators
Operators Meaning
= Equal
> Greater than
< Less than
<> Not Equal
≥ Greater than or Equal to
≤ Less than or Equal to

TURBO C/C++ PROGRAMMING 3


PROGRAMMING LOGIC AND DESIGN | Dr. EVELYN A. RODRIGUEZ

• Logical Operators
Operators Meaning
&& AND
|| OR
! NOT

CONTROL STRUCTURES
1. IF Statement
The IF statement is a conditional statement.

Flowchart: Pseudocode:
True False
Condition IF (Condition) then
{
Statement 1 Statement 1
}
Statement 2
Statement 2

2. IF-ELSE Statement
The IF-ELSE statement is a conditional statement that provides the computer with a choice
between two options.

Flowchart: Pseudocode:

True False IF (Condition) then


Condition {
Statement 1
}
Statement 1 Statement 2 ELSE
{
Statement 2
Statement 3
}
Statement 3

TURBO C/C++ PROGRAMMING 4


PROGRAMMING LOGIC AND DESIGN | Dr. EVELYN A. RODRIGUEZ

3. SWITCH Statement
The SWITCH statement is basically an expansion of the IF-ELSE statement. It provides a
mechanism for choosing an option among several possibilities. An example of flowchart with SWITCH
statement is shown below.

Flowchart:
Start
A

Fill kettle with water


Put 1 teaspoon of coffee in mug

Turn on stove

Put kettle on stove With Y Put 2 teaspoons of


sugar? sugar in mug

F N
Water is
boiling?
With Y Put 1 tblspoons of
T milk? milk in mug
Turn off stove
N
Fill mug with boiled water Stir contents

A End

TURBO C/C++ PROGRAMMING 5


PROGRAMMING LOGIC AND DESIGN | Dr. EVELYN A. RODRIGUEZ

SAMPLE EXERCISE: Given the problem, obtain the algorithm, flowchart, pseudocode, and equivalent
Turbo C++ program.

Problem #1: Compute for the area of the circle. Input the radius and print out the values of radius and
area.

Algorithm:
1. Input the value of radius (R).
2. Define the value of PI (equal to 3.1416).
3. Calculate the area of the circle using the formula AREA = PI*R*R.
4. Print the radius and the corresponding area.

Flowchart: Pseudocode: Equivalent Program:


Start
Compute_Area_of_Circle() #include <stdio.h>
{ #include <conio.h>
Input R #define PI 3.1416
Input R
Define PI=3.1416 void main()
Compute AREA=PI*R*R { int R;
Print R, AREA float AREA;
PI = 3.1416 } clrscr();
printf (“Enter radius:”);
scanf (“%d”,&R);
AREA=PI*R*R
AREA=PI*R*R;
printf (The radius is %d
and the area of the circle
Print R, AREA
is %f.”, R,AREA);
getch();
}
End

TURBO C/C++ PROGRAMMING 6


PROGRAMMING LOGIC AND DESIGN | Dr. EVELYN A. RODRIGUEZ

Problem #2: Given three numbers A, B, and C, draw a flowchart to compute and print out the sum,
average, and the product of these numbers.

Algorithm:
1. Read in the values of A, B, and C.
2. Determine the sum (SUM) of the three values read.
3. Compute the average (AVE) by dividing the sum by 3.
4. Multiply the first value by the second value then by the third value to determine the product
(PROD) of the three values.
5. Print out the computed values of SUM, AVE, and PROD.

Flowchart: Pseudocode: Equivalent Program:

Start
Compute_SUM_AVE_and_PROD() #include <stdio.h>
{ #include <conio.h>
Input A, B, C
Read A, B, C Compute SUM = A+B+C void main()
Compute AVE = (A+B+C)/3 { int A, B, C, SUM, PROD;
Compute PROD = A*B*C float AVE;
SUM = A+B+C Print SUM, AVE, and PROD clrscr();
} printf (“Enter 1st value:”);
scanf (“%d”,&A);
printf (“Enter 2nd value:”);
AVE = (A+B+C)/3 scanf (“%d”,&B);
printf (“Enter 3rd value:”);
scanf (“%d”,&C);
PROD = A*B*C SUM = A+B+C;
AVE = (A+B+C)/3;
PROD = A*B*C;
Print SUM, AVE, printf (The SUM is %d,
PROD the AVE is %f, and the
PROD is %d.”, SUM,
AVE, PROD);
getch();
End }

TURBO C/C++ PROGRAMMING 7


PROGRAMMING LOGIC AND DESIGN | Dr. EVELYN A. RODRIGUEZ

Problem #3: Input the student information as follows: student number, student name, course, section
and the midterm and final term grades. Compute the final grade by dividing the sum of midterm grade
and final term grade. Determine the remarks whether passed (75% and above) or if otherwise, failed.
Display the student name, final grade, and remarks.

Algorithm:
1. Input student number (SNo), student name (SName), course (C), section (Sec), midterm
grade (MG), and final term grade (FG).
2. Compute the final grade (FinalGrd) using the formula: FinalGrd = (MG + FG)/2
3. If FinalGrd is greater than or equal to 75, the remark is Passed, otherwise Failed.
4. Output the student name (SName), final grade (FinalGrd), and remarks.

Flowchart:

TURBO C/C++ PROGRAMMING 8


PROGRAMMING LOGIC AND DESIGN | Dr. EVELYN A. RODRIGUEZ

Pseudocode:

ComputeFinalGrade()
{ Input SNo, SName, C, Sec, MG, FG
Compute FinalGrd = (MG + FG)/2

If FinalGrd>=75
{
Output Sname, FinalGrd, “Passed”
}
Else
{
Output Sname, FinalGrd, “Failed”
}
}

Homework: Choose a partner. Prepare an algorithm, flowchart, and pseudocode of the Enrollment
Process at MinSU Bongabong Campus. The deadline is next meeting, April 5, 2022. I will randomly pick
5 partners who will present their assignment to the class. If your presentation is excellent, the reward
is exemption to our next quiz.

TURBO C/C++ PROGRAMMING 9

You might also like