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

IP Unit 1

The document discusses algorithms and provides examples. It defines an algorithm as a set of rules or instructions to solve a problem in a step-by-step manner. An algorithm should be clear, have well-defined inputs and outputs, be finite, and language independent. Examples of algorithms include finding the sum and average of numbers and determining the largest of two numbers. Flowcharts can represent algorithms graphically using standard symbols. Advantages of algorithms include being easy to understand while disadvantages include being time-consuming to write.

Uploaded by

Harsh Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

IP Unit 1

The document discusses algorithms and provides examples. It defines an algorithm as a set of rules or instructions to solve a problem in a step-by-step manner. An algorithm should be clear, have well-defined inputs and outputs, be finite, and language independent. Examples of algorithms include finding the sum and average of numbers and determining the largest of two numbers. Flowcharts can represent algorithms graphically using standard symbols. Advantages of algorithms include being easy to understand while disadvantages include being time-consuming to write.

Uploaded by

Harsh Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Algorithm

The word Algorithm means “a process or set of rules to be followed in


calculations or other problem-solving operations”. Algorithm refers to a
set of rules/instructions that step-by-step define how a work is to be
executed upon in order to get the expected results. The Algorithm
designed are language-independent, i.e. they are just plain instructions
that can be implemented in any language.
Characteristics of an Algorithm
Clear and Unambiguous: Algorithm should be clear and unambiguous. Each of its
steps should be clear in all aspects and must lead to only one meaning.
Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined
inputs.
Well-Defined Outputs: The algorithm must clearly define what output will be
yielded and it should be well-defined as well.
Finite-ness: The algorithm must be finite, i.e. it should not end up in an infinite
loops or similar.
Feasible: The algorithm must be simple, generic and practical, such that it can be
executed upon will the available resources. It must not contain some future
technology, or anything.
Language Independent: The Algorithm designed must be language-independent,
i.e. it must be just plain instructions that can be implemented in any language, and
yet the output will be same, as expected.
Advantages\Disadvantages of Algorithms
Advantages of Algorithms:
• It is easy to understand.
• Algorithm is a step-wise representation of a solution to a given
problem.
• In Algorithm the problem is broken down into smaller pieces or steps
hence, it is easier for the programmer to convert it into an actual
program.
Disadvantages of Algorithms:
• Writing an algorithm takes a long time so it is time-consuming.
• Branching and Looping statements are difficult to show in
Algorithms.
Example(Sequential\Control statement)
Algorithm of the sum and the average of three numbers:
Step 1: Start
Step 2  :Read the three number suppose "a","b","c" form the user.
Step 3: Declared a variable "sum"  and "Avg".
Step 4 : sum=a+b+c;
Step 5: Avg=sum/3
Step 6:Display "sum " and "Avg".
Step 7 :End .
Algorithm to find Largest of 2 numbers :
Step 1: Start
Step 2: Declare variables n1, n2
Step 3: If n1 is greater than n2, Display "n1 is Maximum"
else
Display "n2 is Maximum"
Step 4: Stop
Example(Looping statement)
Algorithm:
1.Start
2.Declare and initialize variable, i,num, s=0.
3.Enter the value of num i.e. number upto which sum is to be calculated
4.Use Loop to add number. Any of while, do-while or for loop can be used.
5.Print the sum
6.Stop
Pseudo Code:
int i, num, s=0
input num
i=0
do
s=s+i
i=i+1
till the value of i<=num
print s as the sum of numbers.
Flow Chart
• Flowchart is a graphical representation of an algorithm.
• Flowchart is a diagrammatic representation of sequence of
logical steps of a program.
• Flowcharts use simple geometric shapes to depict processes
and arrows to show relationships and process/data flow
Flowchart Symbols
Terminal: The oval symbol indicates Start, Stop and Halt in a
program’s logic flow. A pause/halt is generally used in a program
logic under some error conditions. Terminal is the first and last
symbols in the flowchart. 

 
Flow Chart Symbol
Input/Output: A parallelogram denotes any function of
input/output type. Program instructions that take input from input
devices and display output on output devices are indicated with
parallelogram in a flowchart.

Processing: A box represents arithmetic instructions. All


arithmetic processes such as adding, subtracting, multiplication
and division are indicated by action or process symbol. 
Flow Chart Symbol
Decision Diamond symbol represents a decision point. Decision
based operations such as yes/no question or true/false are
indicated by diamond in flowchart.

Flow lines: Flow lines indicate the exact sequence in which


instructions are executed. Arrows represent the direction of flow
of control and relationship among different symbols of
flowchart. 
Advantages/Disadvantages of Flowchart
Advantages of Flowchart:
• Flowcharts are better way of communicating the logic of
system.
• Flowcharts act as a guide for blueprint during program
designed.
• With the help of flowcharts programs can be easily analyzed.
• It provides better documentation.
• Flowcharts serve as a good proper documentation.
Disadvantages of Flowchart:
• It is difficult to draw flowchart for large and complex
programs.
• In this their is no standard to determine the amount of detail.
• It is very difficult to modify the Flowchart.
Example
Problem: Flowchart for an algorithm which gets two numbers and prints
sum of their value.
Example
Problem: Flowchart for an algorithm to find greatest among two
numbers.
Introduction to Compiler
• A compiler is a translator that converts the high-level language into the machine
language.
• High-level language is written by a developer and machine language can be
understood by the processor.
• Compiler is used to show errors to the programmer.
• The main purpose of compiler is to change the code written in one language
without changing the meaning of the program.
• When you execute a program which is written in HLL programming language
then it executes into two parts.
• In the first part, the source program compiled and translated into the object
program (low level language).
• In the second part, object program translated into the target program through the
assembler.
IDE
• Integrated Development Environments (IDE) The process of
editing, compiling, running, and debugging programs is often
managed by a single integrated application known as an
Integrated Development Environment, or IDE for short. An
IDE is a windows-based program that allows us to easily
manage large software programs, edit files in windows, and
compile, link, run, and debug programs
Overview of compilation and execution
process in an IDE
What is a compilation?
The compilation is a process of converting the source code into object code. It is done
with the help of the compiler. The compiler checks the source code for the syntactical
or structural errors, and if the source code is error-free, then it generates the object
code.

The compilation process can be divided into four steps, i.e., Pre-processing, Compiling,
Assembling, and Linking.
The preprocessor takes the source code as an input, and it removes all the comments
from the source code. The preprocessor takes the preprocessor directive and
interprets it. For example, if <stdio.h>, the directive is available in the program, then
the preprocessor interprets the directive and replace this directive with the content of
the 'stdio.h' file.
Continue………..
Preprocessor
A preprocessor, generally considered as a part of compiler, is a tool that produces
input for compilers. It deals with macro-processing, augmentation, file inclusion,
language extension, etc.
Interpreter/Compiler
An interpreter, like a compiler, translates high-level language into low-level machine
language. The difference lies in the way they read the source code or input. A
compiler reads the whole source code at once, In contrast, an interpreter reads a
statement from the input, converts it to an intermediate code, executes it, then takes
the next statement in sequence. 
Assembler
An assembler translates assembly language programs into machine code.The output
of an assembler is called an object file, which contains a combination of machine
instructions as well as the data required to place these instructions in memory.
Continue……….
Linker
Linker is a computer program that links and merges various object
files together in order to make an executable file. All these files might
have been compiled by separate assemblers. The major task of a linker
is to search and locate referenced module/routines in a program and to
determine the memory location where these codes will be loaded,
making the program instruction to have absolute references.
Loader
Loader is a part of operating system and is responsible for loading
executable files into memory and execute them. It calculates the size
of a program (instructions and data) and creates memory space for it.
It initializes various registers to initiate execution.
Example(Hello Program)
#include <stdio.h>  
int main()  
{  
 printf("Hello javaTpoint");  
    return 0;  
}  In the above flow diagram, the following steps are taken to execute a
program:
• Firstly, the input file, i.e., hello.c, is passed to the preprocessor, and the
preprocessor converts the source code into expanded source code. The
extension of the expanded source code would be hello.i.
• The expanded source code is passed to the compiler, and the compiler
converts this expanded source code into assembly code. The extension of
the assembly code would be hello.s.
• This assembly code is then sent to the assembler, which converts the
assembly code into object code.hello.o
• After the creation of an object code, the linker creates the executable file.
The loader will then load the executable file for the execution.hello.exe
Introduction to Programming Language C
Introduction
The C Language is developed by Dennis Ritchie.
It can be defined by the following ways:
• Mother language
C programming is considered as the base for other programming languages, that is why
it is known as mother language.
• System programming language
A system programming language is used to create system software.
• Procedure-oriented programming language
A procedural language specifies a series of steps for the program to solve the problem .
A procedural language breaks the program into functions, data structures, etc
• Structured programming language
Structure means to break a program into parts or blocks so that it may be easy to
understand.
• Mid-level programming language
C is considered as a middle-level language because it supports the feature of both low-
level and high-level languages.
History of C Language
• C programming language was developed in 1972 by Dennis Ritchie at
bell laboratories of AT&T (American Telephone & Telegraph), located in
the U.S.A.
• Initially, C language was developed to be used in UNIX operating system.
It inherits many features of previous languages such as B and BCPL.
Features of C Language
C is the widely used language. It provides many features that are given below.
• Simple
• Machine Independent or Portable
• Mid-level programming language
• structured programming language
• Rich Library
• Memory Management
• Fast Speed
• Pointers
• Recursion
Language Year Developed By

Algol 1960 International Group

BCPL 1967 Martin Richard

B 1970 Ken Thompson

Traditional C 1972 Dennis Ritchie

K&RC 1978 Kernighan & Dennis


Ritchie

ANSI C 1989 ANSI Committee

ANSI/ISO C 1990 ISO Committee

C99 1999 Standardization


Committee
Structure of C Language Program
1 ) Comment line
2) Preprocessor directive
3 ) Global variable declaration
4) main function( )
{ Local variables;
Statements;
}
User defined function{
}
}
/*First c program with return statement*/
#include<stdio.h>
Void main ()
{ printf ("welcome to c Programming language.\n");
}
C Identifiers
An identifier is a collection of alphanumeric characters that begins either with an
alphabetical character or an underscore, which are used to represent various
programming elements such as variables, functions, arrays, structures, unions, labels,
etc. Rules for naming identifiers are:
1) name should only consists of alphabets (both upper and lower case), digits
and underscore (_) sign.
2) first characters should be alphabet or underscore
3) name should not be a keyword
4) since C is a case sensitive, the upper case and lower case considered differently, for
example code, Code, CODE etc. are different identifiers.
5) identifiers are generally given in some meaningful name such as value,
Net salary, age, data etc.
Character set
A character denotes any alphabet, digit or special symbol used to represent information.
Valid alphabets, numbers and special symbols allowed in C are The alphabets, numbers
and special symbols when properly combined form constants, variables and keywords.
Variables in C
A variable is a name of the memory location. It is used to store data. Its value
can be changed, and it can be reused many times. It is a way to represent
memory location through symbol so that it can be easily identified.
Rules for defining variables
• A variable can have alphabets, digits, and underscore.
• A variable name can start with the alphabet, and underscore only. It can't
start with a digit.
• No whitespace is allowed within the variable name.
• A variable name must not be any reserved word or keyword, e.g. int, float,
etc.
Syntax
type variable_list;
Example
int a;  
float b;  
char c;
Data Type in C
A data type specifies the type of data that a variable can store such as integer, floating,
character, etc.

Types Data Types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type enum

Void Data Type void


Basic Data Type
Keywords in C
A keyword is a reserved word. You cannot use it as a variable name, constant
name, etc. There are only 32 reserved words (keywords) in the C language.
A list of 32 keywords in the c language is given below

auto break case char const continue default do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigne void volatile while


d
Keyword Identifier

Keyword is a pre-defined word. The identifier is a user-defined word

It must be written in a lowercase letter. It can be written in both lowercase and


uppercase letters.

Its meaning is pre-defined in the c Its meaning is not defined in the c


compiler. compiler.

It is a combination of alphabetical It is a combination of alphanumeric


characters. characters.

It does not contain the underscore It can contain the underscore


character. character.
Constant In C
A constant is a value or variable that can't be changed in the program, for
example: 10, 20, 'a', 3.4, "c programming" etc.
Primary Constants:
Integer constants
Are whole number which have no decimal point. Types of integer constants
are: Decimal constant: 0-------9(base 10) etc.
Real Constants
Real constant is also called floating point constant for Ex.: +325.34
Character constant
represented as a single character enclosed within a single quote.
String constant
Set of characters are called string and when sequence of characters are
enclosed within a double quote (it may be combination of all kind of symbols)
is a string constant.
Secondary constants:
Like Array , pointers ,structure etc.(Derived from Primary Constant).
printf() and scanf() in C
The printf() and scanf() functions are used for input and output in
C language. Both functions are inbuilt library functions, defined
in stdio.h (header file).
printf() function
The printf() function is used for output. It prints the given
statement to the console.
Syntax
printf("format string",argument_list);
scanf() function
The scanf() function is used for input. It reads the input data from
the console.
Syntax
scanf("format string",argument_list); 
 
Program to print cube of given number
#include<stdio.h>    
int main(){    
int number;    
printf("enter a number:");    
scanf("%d",&number);    
printf("cube of number is:%d ",number*number*number);    
return 0;  
}    
Output
enter a number:5
cube of number is:125

You might also like