0% found this document useful (0 votes)
9 views45 pages

L3-L5 Algorithm, Flowchart

Uploaded by

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

L3-L5 Algorithm, Flowchart

Uploaded by

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

Introduction to Computing

L3-L5
Review
✓Problem solving

✓Logic and its importance in problem solving

✓Computational problems and its classifications

✓Computer organization and operating system

✓Different computer languages

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 2


Application software, System software & Hardware

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 3


Objectives
To learn and appreciate the following concepts

✓Introduction to algorithms and flowcharts


✓Algorithms and flowcharts for simple problems
✓Typical C program development environment
✓Introduction to C Language: Program Structure
✓Simple C Programs

✓Simple C programs

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 4


Session outcome
• At the end of session the student will be able to write

• Algorithms and flowcharts for solving arithmetical and logical problems

• Simple C programs

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 5


Algorithm
✓A step by step procedure to solve a particular problem

✓Named after Arabic Mathematician Abu Jafar Mohammed Ibn Musa Al Khowarizmi

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 6


Algorithmic Notations
Name of the algorithm [mandatory]
[gives a meaningful name to the algorithm based on the problem]
Start [Beginning of the algorithm]
Step Number [mandatory]
[indicate each individual simple task]
Explanatory comment [optional]
[gives an explanation for each step, if needed]
Termination [mandatory]
[tells the end of algorithm]

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 7


Properties of an algorithm

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 8


Steps to develop an algorithm

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 9


Compute the area of circle
Name of the algorithm : Compute the area of a circle
Step1: Start
Step 2: Input radius
Step 3: [Compute the area]
Area  3.1416 * radius * radius
Step 4: [Print the Area]
Print ‘Area of a circle =‘, Area
Step 5: [End of algorithm]
Stop

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 10


Interchange values of two variables
Name of the algorithm: Interchange values of 2 variables
Step 1: Start
Step 2: Input A,B
Step 3: temp  A
Step 4: AB
Step 5: Btemp
Step 6: Print ‘A=’ , A
Print ‘B=’ , B
Step 7: [End of Algorithm]
Stop

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 11


Largest of 3 numbers
Name of the algorithm: Find largest of 3 numbers
Step 1: Start Step 5: [Compare A and C for largest]
Step 2: [Read the values of A, B and C]
if A>C then
Read A, B, C
Print ‘A’ is largest’
Step 3: [Compare A and B] else
if A>B Go to step 5
Print ‘C’ is largest’
Step 4: [Otherwise compare B with C] Step 6: [End of the algorithm]
if B>C then
Stop
Print ‘B’ is largest’
else
Print ‘C’ is largest’
Go to Step 6

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 12


Factorial of a number

Name of the algorithm: Compute the factorial of a number

Step1: Start
Step 2: Input N
Step 3: fact 1
Step 4: for count=1 to N in step of 1 do
begin
factfact*count
end
Step 5: Print ‘fact of N=‘, fact
Step 6: [End of algorithm]
Stop

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 13


Tutorial on Algorithms
Write the algorithm to

• Find the area of triangle when three sides are given.


• Add two integers
• Find the sum of digits of a number
• Find the Sum and Mean of first N natural numbers

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 14


Flowcharts
✓In Computer Science, Flow chart is used to represent algorithm which basically provides a
solution to any computational problem.

• Flowchart: A graphical/pictorial representation of computation

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 15


Basic Flowchart Symbols
http://www.wiley.com/college/busin/icmis/oakman/outline/chap05/images/f5_02.gif

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 16


RAPTOR TOOL
Raptor is a simple-to-use problem
solving tool that enables the user to
generate executable flowcharts.

To introduce the computing


discipline in order to develop
problem solving skills and improve
algorithmic thinking.

The primary window consists of four


main areas:
1. Symbols
2. Watch Window
3. Workspace
4. Menu and Toolbar

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 17


Raptor symbols
The six symbols used in Raptor are displayed in the Symbol Window in
the upper left corner of the main window
•The assignment symbol is used to give a variable a
numeric or string value.
•The call symbol is used to make calls to outside procedures,
such as graphics routines.
•The input symbol is used for getting input from the user.
•The output symbol is used to display text to the Master
Console.
•The selection structure is used for decision making.
•The loop structure is used for iteration and repetition.
02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 18
Raptor: Watch Window

• The area immediately below the Symbols area is the Watch


Window.

• This area allows the user to view the current contents of any
variables and arrays as the flowchart is executing.

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 19


Raptor: Workspace
• The large, white area to the
right is the primary
Workspace.
• Users can build their
flowcharts in this area and
watch them update as they
execute.
• The workspace is tabbed.
• Most flowcharts have a
single tab named "main",
but programmer-defined
subcharts appear as tabs in
the workspace.

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 20


Raptor: Menu and Toolbars
• The final area is the menu
and toolbar.

• This area allows the user to


change settings and control
the view and execution of
individual flowcharts.

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 21


Area of the circle
Name of the algorithm:
Compute the area of a circle Flowchart

Step1: Input radius

Step 2: [Compute the area]


Area  3.1416 * radius*radius

Step 3: [Print the Area]


Print ‘Area of a circle =‘, Area

Step 4: [End of algorithm]


Stop

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 22


Comparing two numbers
Flowchart
Name of the algorithm: Comparing 2 numbers
Step 1: Start
Step 2: Input num1, num2
Step 3: if num1 > num2 then
Print num1 is bigger
else
Print num2 is bigger
Step 4: end

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 23


Swapping two numbers
Name of the algorithm: Swapping 2 numbers Flowchart

Step1: Input two numbers


Step 2: [swapping]
temp=a
a=b
b=temp
Step 3: [Print]
Print ‘after swapping=‘, a, b
Step 4: [End of algorithm]
Stop

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 24


Find Factorial of given no
Name of the algorithm: Compute the factorial of a
number

Step1: Start
Step 2: Input data
Step 3: factorial 1
Step 4: for i=1 to N in step of 1 do
begin
factorialfactorial*i
end
Step 5: Print ‘fact of N=‘, factorial
Step 6: [End of algorithm]
Stop
02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 25
Key features of flow chart
✓Diagrammatic / visual / graphical representation of computation of an algorithm/pseudo code

✓Easier to understand and analyze the problem and it’s solution before programming

✓Machine independent

✓ Well suited for any type of logic

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 26


Tutorial
• Write the flowchart to find the area of triangle when three sides are given
• Write the flowchart to add two integers

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 27


Summary
✓What is Algorithm and Flowchart

✓Writing algorithms and drawing flowcharts for simple problems

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 28


Programming for Problem Solving
Steps in Problem Solving

Problem solving is a part of our day to


day activity.

• Algorithm
• Flowchart
• Code
• Documenting

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 29


Typical C program development environment

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 30


Typical C program development environment
➢C programs typically go through six phases to be executed. These are: edit, preprocess, compile,
link, load and execute
➢ Phase 1 : creating a program
➢Phases 2 and 3: Preprocessing and Compiling a C Program
➢Phase 4: Linking
➢Phase 5: Loading
➢Phase 6: Execution

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 31


02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 32
General Structure of C program

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 33


General Structure of C program
1. Documentation section:
The documentation section consists of a set of comment lines giving the name of
the program, the author and other details, which the programmer would like to
use later.
2. Link section: The link section provides instructions to the compiler to link
functions from the system library such as using the #include directive.
3. Definition section: The definition section defines all symbolic constants such
using the #define directive.
4. Global declaration section: There are some variables that are used in more than
one function. Such variables are called global variables and are declared in the
global declaration section that is outside of all the functions. This section also
declares all the user-defined functions.

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 34


General Structure of C program
5. main () function section: Every C program must have one main function section. This
section contains two parts; declaration part and executable part
a) Declaration part: The declaration part declares all the variables used in the
executable part.
b) Executable part: There is at least one statement in the executable part. These two
parts must appear between the opening and closing braces. The program
execution begins at the opening brace and ends at the closing brace. The closing
brace of the main function is the logical end of the program. All statements in the
declaration and executable part end with a semicolon.
6. Subprogram section: If the program is a multi-function program then the subprogram
section contains all the user-defined functions that are called in the main () function.
User-defined functions are generally placed immediately after the main () function,
although they may appear in any order.
All sections, except the main () function section may be absent when they are not
required.
02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 35
Hello world program

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 36


Formatting in C
• Statements are terminated with semicolons.

• Indentation to be used for increased readability.

• Free format: white spaces and indentation is ignored by compiler.

• New line is represented by \n (Escape sequence).

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 37


Formatting in C
• C is case sensitive – pay attention to lower and upper case letters when typing !

• All keywords and standard functions are lower case


• Typing MAIN, Main etc instead of main is a compiler error

• Strings are placed in double quotes

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 38


C program for reading a number and display it on the screen
#include<stdio.h>
int main()
{
int num;
printf("\nEnter the number: ");
scanf("%d", &num);
printf("The number read is: %d", num);
return(0);
}

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 39


scanf()
• scanf ( ) is used to obtain the value from the user through an input device (keyboard).

• Eg: scanf(“%d”, &number);

int main(){
int num;
printf("\nEnter the number: ");
scanf("%d", &num);
printf(“The number read is: %d", num);
return(0);
}
02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 40
C program essentials: Format specifiers

Similarly, the format specifiers are used with printf( ) function for printing/displaying.
02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 41
Input/ Output functions
• C provides the printf( ) to display the data on the output device (monitor).

• It is included in stdio.h

• Examples are:
int main() {
• printf("programming is int
an art");
num;
printf("\nEnter the number: ");
• printf("%d", number);
scanf("%d", &num);
• printf("%f%f", p, q); printf("The number read is: %d", num);
return(0);
}

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 42


Adding two integers
#include <stdio.h>
int main()
{/* start of function main */
int sum; /* variable in which sum will be stored */
int integer1; /* first number to be input by user */
int integer2; /* second number to be input by user */
printf( "Enter first integer\n" );
scanf( "%d", &integer1 ); /* read an integer */
printf( "Enter second integer\n" );
scanf( "%d", &integer2 ); /* read an integer */
sum = integer1 + integer2; /* assign total to sum */
printf( "Sum is %d\n", sum ); /* print sum */
return 0; /* indicate that program ended successfully */
} /* end of function main */

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 43


Syntax and Logical errors
➢Syntax errors: violation of programming language rules (grammar).
➢Detected by the compiler

➢Eg: printf (“hello world”) // semicolon missing

➢Logical errors: errors in meaning:


➢Programs are syntactically correct but don’t produce the expected output

➢User observes output of running program

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 44


Summary
✓Problem solving !

✓Typical C program development environment

✓Program structure

✓Formatting

✓Input / Output

✓Simple C programs

02-08-2025 CSS 1001 Programming for Problem Solving (PPS) 45

You might also like