0% found this document useful (0 votes)
41 views

L03-L05 Algorithm, Flowchart

The document introduces algorithms, flowcharts, and simple C programs. It discusses writing algorithms and flowcharts to solve simple problems, and includes examples like calculating the area of a circle. It also covers the basic structure of C programs, and shows examples like a program to add two integers. The goal is for students to learn how to represent problems systematically using algorithms and flowcharts, and then implement simple logic in C.

Uploaded by

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

L03-L05 Algorithm, Flowchart

The document introduces algorithms, flowcharts, and simple C programs. It discusses writing algorithms and flowcharts to solve simple problems, and includes examples like calculating the area of a circle. It also covers the basic structure of C programs, and shows examples like a program to add two integers. The goal is for students to learn how to represent problems systematically using algorithms and flowcharts, and then implement simple logic in C.

Uploaded by

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

Introduction to Computing

L3-L5
Objectives
To learn and appreciate the following concepts

Introduction to algorithms and flowcharts


Algorithms and flowcharts for simple problems
Simple C programs

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 2


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

• Algorithms and flowcharts for simple problems

• Simple C programs

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 3


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

Named after Arabic Mathematician Abu Jafar Mohammed Ibn Musa Al


Khowarizmi

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 4


Algorithmic Notations
• Name of the algorithm [mandatory]
[gives a meaningful name to the algorithm based on the problem]
• Start [Begin of 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]

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 5


Properties of an algorithm

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 6


Steps to develop an algorithm

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 7


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

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 8


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
06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 9
Largest of 3 numbers
Name of the algorithm: Find largest of 3 numbers
Step 1: Start
Step 2: [Read the values of A, B and C]
Read A, B, C

Step 3: [Compare A and B]


if A>B Go to step 5

Step 4: [Otherwise compare B with C]


if B>C then
Print ‘B’ is largest’
else
Print ‘C’ is largest’
Go to Step 6
06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 10
Largest of 3 Numbers contd
Step 5: [Compare A and C for largest]
if A>C then
Print ‘A’ is largest’
else
Print ‘C’ is largest’
Step 6: [End of the algorithm]
Stop

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 11


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

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 12


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

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 13


Basic Flowchart Symbols

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 14


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

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 15


Comparing two numbers
Name of the algorithm: Comparing
Two numbers Flowchart
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

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 16


Swapping two numbers
Name of the algorithm: Swpping
Two 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
06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 17
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

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 18


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

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 19


General Structure of C program

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 20


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.

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 21


1. main () function section: Every C program must have one main function section.
This section contains two parts; declaration part and executable part
1. Declaration part: The declaration part declares all the variables used in the
executable part.
2. 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.
2. 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 section, except the main () function section may be absent when they are not
required.

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 22


Simple C program

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 23


Hello world program

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 24


Structure of C program

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 25


Scanf()
• Scanf is used to obtain the value from the user
• Eg: Scanf(“%d”, &integer1);

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 26


C program essentials

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 27


Printf()
• C provides the printf() to display the data on the monitor.
• It is included in stdio.h
• Examples are:
• Printf(“programming is an art”);
• Printf(“%d”, number);
• Printf(“%f%f”, p,q);

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 28


Adding two integers
#include <stdio.h>
int main( void )
{
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 function main */
06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 29
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

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 30


Summary
Introduction to algorithms and flowcharts
Algorithms and flowcharts for simple problems
Simple C programs

06/11/2023 CSE 1001 Problem Solving using Computers (PSUC) - 2018 31

You might also like