Handout - C++ - 1st Week

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

OBJECTIVES

• The students should be able to understand the Program Development Cyle and Programming Tools.
• Writing a Pseudocode, Algorithm and Flowchart

Program Development Cycle

• Software refers to a collection of instructions for the computer.


• The computer only knows what to do what the programmer tells it to do.
• Therefore, the programmer has to know how to solve problems.

Program Development Cycle – STEPS

1. Analyze Requirement: Define the problem, review the requirements.


2. Design Solution: Plan the solution to the problem (Solution algorithm or Program Logic)
3. Validate Design: Check Program design for accuracy.
4. Implement Design (Code): Translate the algorithm into a programming language (here, C).
5. Test Solution (Test and Debug): Locate and remove any errors in the program.
6. Complete the Documentation: Organize all the materials that describe the program.

Performing a Task on the Computer

Input – Process – Output

Program Planning

• A recipe is a good example of a plan


• Ingredients and amounts are determined by what you want to bake
• Ingredients are input
• The way you combine them is the processing
• What is baked is the output

Programming Tools

1. Algorithms
2. Pseudocode
3. Flowchart

ALGORITHMS, PSEUDOCODE AND FLOWCHARTS

A typical programming task can be divided into two phases:


Problem solving phase - produce an ordered sequence of steps that describe solution of problem this sequence of
steps is called an algorithm
Implementation phase - implement the program in some programming language

STEPS IN PROBLEM SOLVING

• First produce a general algorithm (one can use pseudocode)


• Refine the algorithm successively to get step by step detailed algorithm that is very close to a computer
language.
• Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is
very similar to everyday English. (To be discussed in the next slides).

WHAT IS AN ALGORITHM?

Computer Programming 1 Prepared by: Prof. Marvin Ramos


• An algorithm is a set of ordered steps for solving a problem.

Examples:

• An algorithm for preparing breakfast.


• An algorithm for calculating the Prelim, Midterm and Final Grades
• An algorithm for drawing a curve and an algorithm for converting.

Breaking up a big task in to smaller steps to make it easy to perform.


Brushing teeth, making tea, getting ready for school/office are examples of some sort of algorithms
Each step of the algorithm is called as Instruction
A step-by-step series of instructions for solving a problem (a recipe is an example of an algorithm)
Algorithms are key to solving many problems efficiently

ALGORITHM IN REAL LIFE 

Consider the following …


Problem: Baking a Cake
How to solve:
1. Start
2. Preheat the oven at 180oC
3. Prepare a baking pan
4. Beat butter with sugar
5. Mix them with flour, eggs and essence vanilla
6. Pour the dough into the baking pan
7. Put the pan into the oven
8. End

WHY DO WE NEED TO BUILD ALGORITHMS?

If we wish to build a house, we need to design it first.


- Can you think of some possible consequences of not designing a house before building it?
• Similarly, computer programs (especially large and complex ones) need to be designed before they are written.
- Can you think of some possible consequences of not designing a program before building it?
One of the things considered when designing a computer program is the algorithm which it will be based on.

ALGORITHMS IN PROGRAM DESIGN 

• A computer program is built to solve a certain problem.

Examples:

1. A program to calculate the grade obtained given a mark.


2. A program to convert a Gregorian date to an Islamic date.
3. A program to produce a document.
Below are steps (in fact, an algorithm) for building a  program to solve a particular problem: 
• Analyze the problem
• Design a computer solution to the problem by developing an algorithm.
• Write a computer program based on the algorithm.
• Test the program.

Computer Programming 1 Prepared by: Prof. Marvin Ramos


HOW TO SPECIFY AN ALGORITHM?

• An algorithm must be specific enough so that it can be conveniently translated into a computer program
(using C++, for example). An algorithm can be specified:

• Textually
For example, using pseudocode.
• Graphically
For example, using flowcharts or UML activity charts

PSEUDOCODE

• An outline of a program, written in a form that can easily be converted into real programming statements. It
resembles the actual program that will be implemented later. However, it cannot be compiled nor executed.

• Pseudocode normally codes the following actions:


- Initialization of variables
- Assignment of values to the variables
- Arithmetic operations
- Relational operations

• The idea is to represent the algorithm in a form that is in between pure English and actual Running code
• Actually, it’s one of the ways for expression of Algorithm.
• Pseudo means “pretended” / “not real”
• Code refers to a computer programming language

Pseudocode - format

• Write only one statement per line


• Capitalize initial keyword
• Indent to show hierarchy
• End multi-line structures
• Keep statements language independent
• It should not be paragraphs or free-flowing sentences.
• It also should not contain any language- specific syntax.
• it should reflect what’s in your final program, not the ideas you scrapped along the way

Special Keywords

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.

Pseudo code Vocabulary

1. Input/output
2. Iteration
3. Selection/Decision
4. Processing
Computer Programming 1 Prepared by: Prof. Marvin Ramos
Pseudocode – Example

1. Write a program that obtains two numbers from the user. It will print out the sum of those numbers.

Pseudocode

- DISPLAY the message to enter the first integer


- READ user’s first integer input
- DISPLAY the message to enter the second integer
- READ user’s second integer input
- COMPUTE first integer + second integer GIVING sum
- DISPLAY an output message that explains the answer as the sum
- DISPLAY the sum

PSEUDOCODE & ALGORITHM EXAMPLE

Example 1: Write an algorithm and pseudocode 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.

Pseudocode: Detailed Algorithm Flowchart

• Input a set of 4 marks Step 1: Input M1,M2,M3,M4

• Calculate their average by Step 2: GRADE 


summing and dividing by 4 (M1+M2+M3+M4)/4

Step 3: if (GRADE < 50) then


• if average is below 50
Print “FAIL”
Print “FAIL”
else
else Print “PASS”
endif

Comparison Operators

• Comparison operators are used to compare two values (or variables). This is important in programming,
because it helps us to find answers and make decisions. The return value of a comparison is either 1 or 0,
which means true (1) or false (0).

Example

(5==3) False
(5!=3) True
(5>3) True
(5<3) False
Computer Programming 1 Prepared by: Prof. Marvin Ramos
(5>=3) True
(5<=3) False

FLOWCHARTS

• (Dictionary) A schematic representation of a sequence of operations, as in a manufacturing process or


computer program.

• (Technical) A flowchart is a graphical representation of the sequence of operations in a program.

• Information system flowcharts show how data flows from source documents through the computer
to final distribution to users.
• Program flowcharts show the sequence of instructions in a single program or subroutine. Different
symbols are used to draw each type of flowchart.
• An algorithm can be represented graphically using a flowchart.

FLOWCHART SYMBOLS

Purpose of Flowcharting

• An aid in developing the logic of a program.

Computer Programming 1 Prepared by: Prof. Marvin Ramos


• Verification that all possible conditions have been considered in a program.
• Provides means of communication with others about the program.
• A guide in coding the program.
• Documentation for the program.

Example of Flowchart: Ordering a Burger

Algorithm 1 (Standard Process) Algorithm 2 (Improved Process)


1. Approach Counter 1. Approach Counter
2. Order Combo Meal
2. Order Burger 3. Pay Cashier
3. If You Want Fries Then
1. Order Fries
4. Else, Go To Next Step
5. If You Want Drink Then
1. Order Drink
6. Else, Go To Next Step
7. Pay cashier

Computing The Sum, Average, and Product of Three Numbers

Pseudocode Flowchart
- READ X, Y, Z
- COMPUTE SUM (S) As X + Y + Z
- COMPUTE Average (A) As S / 3
- COMPUTE Product (P) As X * Y * Z
- WRITE (Display) the Sum, Average,
and Product

Example of Flowchart and Pseudocode: Computing Interest on a Loan

Pseudocode Flowchart
- READ Name, Balance, Rate
- COMPUTE Interest as balance x Rate
- WRITE (DISPLAY) Name And Interest

Computer Programming 1 Prepared by: Prof. Marvin Ramos


Example 2: Write an algorithm, pseudocode and draw a flowchart to convert the length in feet to centimeter.
1fott =30cm

Algorithm
Pseudocode Algorithm
• Input the length in feet (Lft) Step 1: Input Lft
• Calculate the length in cm (Lcm) by multiplying Lft with 30 Step 2: Lcm 
• Print length in cm (LCM) Lft x 30
Step 3: Print Lcm

Example 3: Write an algorithm, pseudocode and draw a flowchart that will read the two
sides of a rectangle and calculate its area.
Flowchart
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 4: Write the pseudocode, algorithm and flowchart to check the two numbers. Display the output if the
number is greater than or less than.

Pseudocode Algorithm Flowchart


• BEGIN Step 1: Start
• READ a,b Step 2: get a,b value
• IF (a>b) THEN Step 3: check if(a>b) print a is greater
• DISPLAY a is greater Step 4: else b is greater
• ELSE Step 5: Stop
• DISPLAY b is greater
• END IF
• END

Flowchart – sequence control Flowchart – selection control Flowchart – repetition control


structure structure structure

Computer Programming 1 Prepared by: Prof. Marvin Ramos

You might also like