Unit - I Introduction To Algorithms and Programming Languages & Introduction To C Introduction To Algorithms and Programming Languages
Unit - I Introduction To Algorithms and Programming Languages & Introduction To C Introduction To Algorithms and Programming Languages
Unit - I Introduction To Algorithms and Programming Languages & Introduction To C Introduction To Algorithms and Programming Languages
Algorithm:
Algorithm is a method of representing the step-by-step logical procedure for
solving a problem. A program written in a non-computer language is called an
algorithm. It is a step-by-step method of performing any task.
These are one of the most basic tools that are used to develop the program
solving logic. They can have steps that repeat or require decisions until the task is
completed. While writing an algorithm we can concentrate only on the logic
Properties:
An algorithm must possess the following properties:
Finiteness:
An algorithm must terminate in a finite number of steps.
Definite:
By definite we mean that each step of algorithm must be precisely defined such
that there is no ambiguity or contradiction.
Effectiveness:
Each step must be effective, easily converted into program statement and can
be performed exactly in a finite amount of time.
Generality:
The algorithm must be complete so that it will work successfully in solving all
the problems of particular type for which it is defined.
Input/Output:
Each algorithm must take zero, one or more quantities an input data and
produce one or more output values.
1|Page
Advantages of algorithm:
1. It is a step-wise representation of a solution to a given problem, which
makes it easy to understand.
2. An algorithm uses a definite procedure.
3. It is not dependent on any programming language, so it is easy to
understand for anyone even without programming knowledge.
4. Every step in an algorithm has its own logical sequence so it is easy to
debug.
5. By using algorithm, the problem is broken down into smaller pieces or
steps hence, it is easier for programmer to convert it into an actual
program
Disadvantages of algorithm:
1. Writing algorithm takes a long time.
2. An Algorithm is not a computer program, it is rather a concept of how
a program should be.
Ex:
Algorithm 1: Addition of two numbers.
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Algorithm 2: Swapping of two numbers without using third variable.
Step 1: Start
Step 2: Enter A, B
Step 3: Print A, B
Step 4: A = A + B
Step 5: B= A - B
Step 6: A =A - B
Step 7: Print A, B
Step 8: End
Flowchart:
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.
➢ Shows logic of an algorithm.
➢ Emphasizes individual steps and their interconnections.
2|Page
Flowchart Symbols:
Here is a chart for some of the common symbols used in drawing flowcharts.
Symbol Symbol Name Purpose
3|Page
Disadvantage of flowchart:
1. The flowchart can be complex when the logic of a program is quite
complicated.
2. Drawing flowchart is a time-consuming task.
3. Difficult to alter the flowchart. Sometimes, the designer needs to redraw
the complete flowchart to change the logic of the flowchart or to alter the
flowchart.
4. Since it uses special sets of symbols for every action, it is quite a tedious
task to develop a flowchart as it requires special tools to draw the
necessary symbols.
5. In the case of a complex flowchart, other programmers might have
a difficult time understanding the logic and process of the flowchart.
6. It is just a visualization of a program, it cannot function like an actual
program.
Flowchart Categories:
1.
4|Page
2.
5|Page
3.
Ex:
6|Page
Introduction to Programming Languages:
• Language is way of communication between two persons.
• Computer Languages are communication between computer and person.
• Computers can understand only machine instructions. Instructions are to be
given in machine understandable language.
A programming language is defined by a set of rules. It is a formal constructed
language, designed to communication instructions to a computer. Programming
languages can be used to create programs to control the behavior of the machine.
A program is a list of instructions or statements for directing the computer to
perform a required data-processing task.
TYPES:
Computer
Language
Machine language:
• At the lowest level computer understands only 0 and 1.
• Programs expressed in terms of binary language are called machine language
and is the only one language computer can understand.
• A computer’s programming language consists of strings of binary numbers (0’s
and 1’s).
• A machine language programmer
➢ has to know the binary code for each operation to be carried out,
➢ must also be familiar with the internal organization of the computer,
➢ must also keep track of all the addresses of main memory locations that are
referred to in the program.
• The machine language format is slow and tedious as users could not remember
these binary instructions.
7|Page
• The mnemonics are converted into binaries with the help of a translator known
as Assembler.
Compiler:
8|Page
• A compiler is also used to translate source program into an object program.
Compiler converts source program into object program in terms of stages called
passes. Normally, most of the compilers use two passes to convert source
program into the machine language program.
Interpreter:
9|Page
Assembler – converts assembly level instructions to machine level instructions.
Programmers can write the code using symbolic instruction codes that are
meaningful abbreviations of mnemonics. It is also known as low-level language.
Advantages :
1. It is easier to understand if compared to machine language.
2. Modifications are easy.
3. Correction & location of errors are easy.
Disadvantages :
1. Assembler is required.
2. This language is architecture /machine-dependent, with a different
instruction set for different machines.
10 | P a g e
5. Fifth Generation Language :
The fifth-generation languages are also called 5GL. It is based on the concept
of artificial intelligence. It uses the concept that that rather than solving a problem
algorithmically, an application can be built to solve it based on some constraints, i.e.,
we make computers learn to solve any problem. Parallel Processing &
superconductors are used for this type of language to make real artificial intelligence.
Example: PROLOG, LISP, etc.
Advantages :
1. Machines can make decisions.
2. Programmer effort reduces to solve a problem.
3. Easier than 3GL or 4GL to learn and use.
Disadvantages :
1. Complex and long code.
2. More resources are required & they are expensive too.
Introduction to C :
The root of all modern languages is “ ALGOL “ which was introduced in 1960’s.
In 1963 CPL (Combined Programming Language ) was developed by Cambridge
University but the drawback of CPL was it is too large for using many application. So
in 1967 “Martin Richards“ develop a language called BCPL ( Basic Combined
Programming Language )
In 1970 Ken Thompson developed a language B with some features of BCPL
specially used in system programming.
But these languages have some limitations to support development of both the
business application and system software.
In 1972 “ Dennis Ritchie “ developed a programming language “ C “ at AT &
T Bell laboratories which is the successor of B. After in 1983 ANSI formed a committee
and standardized a C language which is referred as ANSI C.
Why to use C :
• C was initially used for system development work in particular the programs
that make up the operating system. It was initially implemented for the system
that used UNIX operating system.
• C was adopted as a system development language because it produces a code
that runs really as fast as the code written in assembly language. Some
examples of the use of C might be
1. Operating System
2. Language Compilers
3. Assemblers
4. Text Editors
Editors are used to type the source program and store it on a disc.
C language uses a Turbo C editor in MS-DOS i.e. TC. UNIX system uses Vi editor.
11 | P a g e
5. Print Spoolers
6. Network Drivers
7. Modern Programs
8. Utilities
9. Database
10. Language Interpreters
Where is ‘ C ‘ useful :
• The operating system such as UNIX , LINUX was written in C
• The compilers and interpreters for other languages was also written in C
• The areas where C language is used
1. Embedded Systems
2. System Programming
3. Artificial Intelligence
4. Computer Graphics
5. Space Research
6. Image Processing
7. Game Programming
Features of ‘ C ‘:
• C is a system programming language which provides flexibility for writing
compilers , os etc.
• It is a high level language.
• C provides variety of data types both Built-in and User define.
• It contains set of operators that helps in writing the program efficiency.
• C language has an important feature called Extendibility
• The programs can create and maintain a unique library functions.
• C has 40 keywords among which 32 were standard keywords and the remaining
8 are reserve keywords.
• It is highly portable i.e. programs written in one environment can be executed
on other environment.
• C is often called as middle level language having features of both high level
language and assembly level.
• C language is rich in built-in functions or standard functions (library
functions).
• C language is a structured programming language , very simple and easy to
learn.
12 | P a g e
Steps in learning C language:
Vocabulary
Character Keywords, Instructions
Set Reserved Functions Programs Applications
Words, [syntax rules]
Identifiers
Structure of a “ C “ program :
Structure of a C program : Example of a C program
Documentation Section /* Program for addition of 2 numbers */
Link Section (or) Preprocessor #include<stdio.h>
Directives #include<conio.h>
Global Declaration Section
Main() Function Section Main() Function Section
main()
main() {
{ int a,b,c;
Local variable declaration clrscr();
Executable Statements printf(“Enter a and b values:”);
} scanf(“%d%d”,&a,&b);
c=a+b;
printf(“sum is %d”,c);
}
Sub Program Section (or)
User-defined function Section
Function 1
Function 2
-----------------
Function n
Documentation Section :
This section consists of a comment tags giving the name of the program , the
author and other details of the program which the programmer would like to use
later. The comments are enclosed using /* comment */ (or) //comment.
Ex : /* program for addition of 2 numbers */
13 | P a g e
Global Declaration Section :
The variables that declare outside of the main function are called Global
variables. These variables can be accessed anywhere by any function throughout the
program. The global variables are automatically initialized with zero hence there is no
chance of garbage value. The global variable declaration can be done as
datatype variable1,variable2,……;
Ex : int a,b;
14 | P a g e
Compiling and Executing C Program:
➢ To compile and run a C language program, you need a C compiler.
A compiler is a software that is used to compile and execute programs.
➢ To set up a C language compiler in your Computer/laptop, there are two ways:
1. Download a full-fledged IDE like Turbo C++ or Microsoft Visual C++ or DevC++,
which comes along with a C language compiler.
2. you can use any text editor to edit the program files and download the C compiler
separately and then run the C program using the command line.
Step 1: Open turbo C IDE(Integrated Development Environment), click on File and
then click on New
15 | P a g e
Step 4: Compile the program using Alt + F9 OR Compile > Compile
Step 5: Press Ctrl + F9 to Run (or select Run > Run in menu bar ) the C program.
Programming Example:
1. To print a message "Hello World" on the screen.
/*Program to print a message "Hello World" */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello World");
getch();
}
Output:
Hello World
16 | P a g e