Combine PDF
Combine PDF
TO
COMPUTERS
AND LOGIC
PROGRAMMING LOGIC AND DESIGN
UNIT 1 (LESSON 1 – 3)
What is Algorithm?
It is a finite sequence of well-defined,
computer-implementable instructions, typically to
solve a problem.
What is Algorithm?
Algorithm is a step-by-step procedure for solving a
problem.
What is Algorithm?
Algorithm is not the complete code or program.
Classification
of Algorithm Branching (Selection)
according to
Structures
Loop (Repetition)
Define Define the problem.
How to Write Describ Describe the steps needed to manipulate the inputs to
produce the desired outputs.
e
an Algorithm?
Display Display the outputs.
2. The way the if-else, for, while loops are indented in a program, indent
the statements likewise, as it helps to comprehend the decision
control and execution mechanism. They also improve the readability to
a great extent.
How to write a Pseudocode?
3. Use appropriate naming conventions.
7. Check whether all the sections of a pseudo-code are complete, finite, and
clear to understand and comprehend.
• Start
• Declare variable N as year of
birth, A as age;
• Read input of user then
store to N;
• A = 2022 - N;
• Print A;
• Stop
Pseudocode Example 3:
Calculation of Simple
Work Pay
•Begin
•Input hours
•Input rate
•pay = hour * rate
•Print pay
•End
Pseudocode Example
4:Calculation of Work
Pay with Overtime Pay
•Begin
•Input hours, rate
•If hours <= 40 then
• pay = hours * rate
•Else
• pay = (40 * rate) + ((hours - 40) * rate * 1.5)
•Print pay
•End
What is a
Flowchart?
It is a type of diagram that
represents an algorithm or
process, showing the steps as
boxes of various kinds, and
their order by connecting these
with arrows.
A flowchart is a pictorial or
diagrammatically
representation of the flow of
steps in to solve a given
What is a problem.
Flowchart? Frank Gilberth introduced
flowcharts in 1921, and they
were called “Process Flow
Charts” at the beginning.
Flowchart Symbols
Flowchart Symbols
Flowchart Symbols
Flowchart Symbols
1. All boxes of the flowchart are connected with Arrows or flow lines.
2. Flowchart symbols have an entry point on the top of the symbol with
no other entry points. The exit point for all flowchart symbols is on the
bottom except for the Decision symbol.
3. The Decision symbol has two exit points; these can be on the sides
General of the bottom and one side.
Rules for 4. Generally, a flowchart will flow from top to bottom. However, an
upward flow can be shown as long as it does not exceed 3 symbols.
Flowcharting
5. Connectors are used to connect breaks in the flowchart.
Seatwork 1
Draw a flowchart to sort A, B,
and C in ascending order.
Review of C Programming
MAIN()
{
VARIABLE DECLARATION
BODY OF PROGRAM
}
Example
/* A simple C Program */
#include<stdio.h>
#include<conio.h>
main()
{
printf(“This is the output of my first C program. \n”);
getch();
}
// end of Program D.1
OUTPUT:
This is the output of my first C program.
Data Types
Type Description
Char Characters
Int Integer
Float Floating point
Double Double floating point
void Valueless
Arithmetic Operators
OPERATOR ACTION
> Greater Than
>= Greater Than Equal
< Less Than
<= Less Than Equal
== Equal
!= Not Equal
Logical Operators
OPERATOR ACTION
&& AND
|| OR
! NOT
If
If else
Statements Nested If
Nested If else
Switch case
For loop
Do while loop
Single
dimensional
array
Array
Multi-dimension
al array
Write a C program that will Print Floyd’s Triangle.
A user will input how many numbers of rows of
Floyd's triangle to print.
4
STRUCTURE OF A PROGRAM
1
2
3
4 1
5
5
STRUCTURE OF A PROGRAM
O Save the file as "Hello.cpp" in your project
directory.
O Select "Build" menu ⇒ Build (Ctrl-F9) or
Select "Build" menu ⇒ Run (Ctrl-F10).
O After running the program you should get
the output on the next slide.
6
STRUCTURE OF A PROGRAM
7
Explanation of the Program
1. These are called comments, they are NOT executable and are
ignored by the compiler; but they provide useful explanation
and documentation to other readers. A comment starts with
//, extending to the end of the line. Comments may also start
with /* and end with */.
2. The "#include" is called a preprocessor directive. This tells the
preprocessor to include the "iostream" header file to support
input/output operations. The "using namespace std;"
statement declares std as the default namespace used in this
program. The names cout and endl belong to the std
namespace.
3. The main() function is the entry point of program execution.
main() is required to return an int (integer). 8
Explanation of the Program
4. "cout" refers to the standard output (or Console OUTput).
The symbol << is called the stream insertion operator (or
put-to operator), which is used to put the string "hello,
world" to the console. "endl" denotes the END-of-Line or
newline, which is put to the console to bring the cursor to
the beginning of the next line.
5. Terminates the main() function and returns a value of 0 to
the operating system. Typically, return value of 0 signals
normal termination; whereas value of non-zero (usually 1)
signals abnormal termination.
6. The semicolon is a statement terminator. Each individual
statement must be ended with a semicolon and it
indicates the end of one logical entity.
9
C++ Quickstart
Let's create our first C++ file.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Omitting Namespace
C++ Output (Print text)
The cout object, together with the << operator, is used to output
values/print text:
You can add as many cout objects as you want. However, note that it does
not insert a new line at the end of the output:
C++ User Input
cin is a predefined variable that reads data from the keyboard
with the extraction operator (>>).
C++ New Lines
To insert a new line, you can use the \n character: