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

CS101E-PSP- Part I

Uploaded by

suryamgupta73
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)
7 views

CS101E-PSP- Part I

Uploaded by

suryamgupta73
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/ 60

Name of the Course : Problem Solving using C

Algorithms and Flowcharts

Course Co-ordinator: Dr. R.Chinnaiyan


Pro Vice Chancellor – Lingayas Vidyapeeth Deemed to be University
Term: September 2024 – December-2024
Course Code: CS 101 E
Module 2

Algorithms and Flowcharts

*
Learning Outcomes

COURSE OUTCOMES

The students undergoing this course will be able to:


CO1: Know the basic concepts of programming languages as well
as operating system
CO2: Learn the basics of programming using C
CO3: undergo the functions and pointers
O4: Learn about the structures, unions as well as functions using
recursion
CO5: Know about the dynamic programming as well as file
handling

*
Module Content

Algorithms and Flowcharts

•The meaning of algorithms


•Flowcharts and their need
•Writing algorithms and drawing flowcharts for simple exercises

*
Problem Solving Aspects
Problem solving is a creative process. It is an act of defining a problem, determining
the cause of the problem, identifying, prioritizing, and selecting alternatives for a
solution and implementing a solution.

A Problem can be solved successfully only after making an effort to understand the
problem. To understand the problem, the following questions help:
• What do we know about the problem?
• What is the information that we have to process in order the find the solution?
• What does the solution look like?
• What sort of special cases exist?
• How can we recognize that we have found the solution?

*
Methodology
Different strategies appear to be good for different problems.
Some of the well known strategies are:

• Divide and Conquer


• Greedy Method
• Dynamic Programming
• Backtracking
• Branch and Bound

*
Program Development
The various steps involved in Program Development are:

• Defining or Analyzing the problem


• Design (Algorithm)
• Coding
• Documenting the program
• Compiling and Running the Program
• Testing and Debugging
• Maintenance

*
Algorithms
Definition :

An algorithm is a step-by-step description of the solution to a problem. It is


defined as an ordered sequence of well-defined and effective operations
which, when carried out for a given set of initial conditions, produce output,
and terminate in a finite time.

The term “ordered sequence” specifies, after the completion of each step in
the algorithm, the next step must be unambiguously defined.
An algorithm must be:
• Definite
• Finite
• Precise and Effective
• Implementation independent ( only for problem not for programming languages)

*
Algorithms
Example 1: Algorithm for finding factorial of a given number

Step 1: Start
Step 2: Initialize factorial to be 1, i to be 1
Step 3: Input a number n
Step 4: Check whether the number is 0. If so report factorial is 1 and go to step 9
Step 5: Repeat step 6 through step 7 n times
Step 6: Calculate factorial = factorial * i
Step 7: Increment i by 1
Step 8: Report the calculated factorial value
Step 9: Stop

*
Pseudo Code
Example 1: Pseudo Code for finding factorial of a given number
Step 1: START
Step 2: DECLARE the variables n, fact, i
Step 2: SET variable fact =1 and i =1
Step 3: READ the number n
Step 4: IF n = 0 then
Step 4.1: PRINT factorial = 1
Step 4.2: GOTO Step 9
Step 5: WHILE the condition i<=n is true, repeat Step 6 through Step 7
Step 6: COMPUTE fact = fact * i
Step 7: INCREMENT i by 1
Step 8: PRINT the factorial value
Step 9: STOP
*
Flowchart
Definition

Flowchart is a diagrammatic representation of an


algorithm. It uses different symbols to represent
the sequence of operations, required to solve a
problem. It serves as a blueprint or a logical
diagram of the solution to a problem.

*
Flowchart Symbols
Typical flowchart symbols are given below

*
Flowchart

*
Algorithms

Aim: To write a C program for adding two numbers


Algorithm
1) Start the program
2) Declare the variables a, b and c
3) Read the values of a, b
4) c= a +b
5) Write the value of c
6) Stop the program

*
Flowchart

*
Algorithms

Aim: To write a C program for performing


arithmetic operations
Algorithm
1) Start the program
2) Declare the variables a, b and c
3) Read the values of a, b
4) c= a +b ,d=a-b,e=a*b,f=a/b,g=a%b;
5) Write the value of c,d,e,f,g
6) Stop the program

*
Flowchart

*
Algorithms

Aim: To write a C program for Biggest Number


among 2 numbers
Algorithm
1) Start the program
2) Declare the variables a, b
3) Read the values of a, b
4) If (a>b) big is a.
Else big is b.
5) Write the Biggest value
6) Stop the program

*
Flowchart

*
Algorithms

Aim: To write a C program for finding the area


and circumference of circle
Algorithm
1) Start the program
2) Declare the variables r, area,cricum
3) Read the values of r
4) Area = pi*r*r
5) Circum= 2*pi*r
6) Write the value of area and circum
7) Stop the program

*
Flowchart

*
Introduction to C
Language
C is a general purpose high level programming
language. Because of its flexibility and efficiency it is
widely used for software development. Its features
allow the development of well-structured programs.

The data types and control structures are directly


supported by most computers, resulting in the
construction of efficient programs.

*
Evolution of C Language
ALGOL was the first computer language to use a block
structure. In 1967, Martin Richards developed a language
called BCPL (Basic Combined Programming Language)
primarily, for writing system software.
In 1970, Ken Thompson created a language using many
features of BCPL and called it ‘B’. ‘B’ was used to create
early versions of UNIX operating system at Bell
Laboratories. Both BCPL and B were “typeless” system
programming languages.

*
Evolution of C Language

C was developed by Dennis Ritchie at Bell


Laboratories in 1972. It was evolved from
ALGOL, BCPL, and B. C uses many
concepts of these languages and new
features like data types. UNIX operating
system was coded almost entirely in C.

*
ANSI C - Standard

To assure that the C language remains standard, in


1973, American National Standards Institute (ANSI)
appointed a technical committee to define a
standard for C. The committee approved a version of
C in 1989 which is now known as ANSI C. It was
then approved by the International standards
Organization (ISO) in 1990. The standard was
updated in 1999.

*
Characteristics of C
Language
⚫ C language is well suited for structured modular programming
⚫ C is a robust language with rich set of built-in functions and operators
⚫ C is smaller which has minimal instruction set and programs written
in C are efficient and fast
⚫ C is highly portable (code written in one machine can be moved to
other)
⚫ C is highly flexible
⚫ C allows access to the machine at bit level (Low level (Bitwise)
programming)
⚫ C supports pointer implementation - extensive use of pointers for
memory, array,
structures and functions

*
Character Set

*
Variable and Constants
A variable is a named data storage location in computers memory. By
using a variable name we are referring to the data stored in the location. A
value that changes during the
execution of the program is called variable.
Rules of variables declaration in C:

1.The variable name is a combination of alphabets, digits, underscore etc.


2.The first character in the variable name must be an alphabet.
3.A keyword cannot be used as variable name
4.No comma or blanks are allowed in variable name.
5.No special symbols ($, #) other than underscore can be used in variable
name.
6.Uppercase & lowercase letters are distinct i.e. case sensitive
7.A variable name can’t start with digit.
8.The maximum length of variable name should be 8 characters long in
DOS based program & in ANSI C it supports up to 31 characters long.
*
Constants

Identifier which doesn’t change value during the execution of a


program is called constant. The value associated with the storage
always be constant.

C has 4 basic types of constant:

Integer constant Floating point constant Character constant String


constant

*
Integer Constants

*
Floating Point Constants

*
Char and String Constants

*
Symbolic Constants

*
Datatypes

• A data types defines a set of values that a


variable can store along with a set of operation
that can be performed on the variable. There are
basically two types of data types:

1.Fundamental
2.Derived

*
Datatypes

*
Datatypes

*
Variable Declaration

*
Assignment Statement

*
Precedence of Operators

*
Mathematical Functions

Dr.R.Chinnaiyan , Pro Vice Chancellor


Lingaya’s Vidyapeeth Deemed to be University , Faridabad –Haryana - INDIA
*
Structure of C Program

*
Structure of a C Program

*
Structure of C Program

*
Building a C Program

*
Keywords
Keywords are reserved identifiers and they cannot be used
as names for the program variables. The keywords are also
called as reserved words. The meaning of the keywords
already given to the compiler. There are 32 keywords
available in C.

*
Variable and Constants
A variable is a named data storage location in computers
memory. By using a variable name we are referring to the
data stored in the location. A value that changes during the
execution of the program is called variable.
Rules of variables declaration in C:

1.The variable name is a combination of alphabets, digits, underscore etc.


2.The first character in the variable name must be an alphabet.
3.A keyword cannot be used as variable name
4.No comma or blanks are allowed in variable name.
5.No special symbols ($, #) other than underscore can be used in variable name.
6.Uppercase & lowercase letters are distinct i.e. case sensitive
7.A variable name can’t start with digit.
8.The maximum length of variable name should be 8 characters long in DOS based
program & in ANSI C it supports up to 31 characters long.
*
Constants

Identifier which doesn’t change value during the execution of a


program is called constant. The value associated with the
storage always be constant.

C has 4 basic types of constant:

Integer constant
Floating point constant
Character constant
String constant

*
Types of Constants

*
Datatypes
A data types defines a set of values that a variable
can store along with a set of operation that can be
performed on the variable. There are basically two
types of data types:

1. Fundamental
2. Derived

*
Datatypes

*
Datatypes

*
Algorithms
C supports a rich set of operators. An operator is a
symbol that tells the computer to perform
mathematical or logical operations. Operators are
used in programs to manipulate data.
C operators can be classified into a number of
categories. They include

Arithmetic operators, relational operators, logical


operators, assignment operator, increment and
decrement operator, conditional operator, bitwise
operators, comma operator, special operators,

*
Operators

*
Operators

*
Priority of Operators

*
Expressions

Expression is a combination of
operands, operators, function calls that
evaluates to a value. The three types of
expressions are

1.Arithmetic expression (uses arithmetic operators),

2.Relational expression (uses relational operators),

3.Logical expression (uses logical operators).

*
Algorithms

*
Typedef
C automatically converts any intermediated values
to the proper type so that the
expression can be evaluated without losing any
significance. This auto
known implicit type conversion
Type casting is a way to convert a variable from one
data type to another data type. For
example, if you want to store a long va
int. One can convert values from one type to another
explicitly using the
follows:
(type_name) expression

*
Typedef
Consider the following example where the
cast operator causes the division of one
integer variable by another to be performed
as a floating-point operation:

#include <stdio.h>
main()
{
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean : %f\n", mean );}
*
Summary
Thus we have discussed the basic concepts of C Language.
1. Algorithms
2. Flowcharts
3. Structure of C program
4. Data types
5. Variables, Constants
6. Opeartors
7. Expressions

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy