Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

UNIT – I

Introduction to Algorithms and Programming Languages


&
Introduction to C

Introduction to Algorithms and Programming Languages:


Algorithm – Key features of Algorithms, Flow Charts, Programming Languages
– Generations of Programming Languages
Introduction to C:
Introduction – Structure of C Program – Writing the first C Program – Files used
in C Program – Compiling and Executing C Programs - Programming Example.

A typical programming task can be divided into two phases:


• Problem solving phase
• Implementation phase
Problem solving phase:
• Produce an ordered sequence of steps that describe solution of problem.
• This sequence of steps is called an algorithm.
Implementation phase:
• Implement the program in some programming language.

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

Used at the beginning and end of the algorithm


Start/Stop to show start and end of the program.

Indicates processes like mathematical


Process
operations.

Input/ Output Used for denoting program inputs and outputs.

Stands for decision statements in a program,


Decision where answer is usually Yes or No.

Arrow Shows relationships between different shapes.

On-page Connects two or more parts of a flowchart,


Connector which are on the same page.

Off-page Connects two parts of a flowchart which are


Connector spread over different pages.

Guidelines for Developing Flowcharts:


➢ Flowchart can have only one start and one stop symbol
➢ On-page connectors are referenced using numbers
➢ Off-page connectors are referenced using alphabets
➢ General flow of processes is top to bottom or left to right
➢ Arrows should not cross each other
Advantages of flowchart:
1. The Flowchart is an excellent way of communicating the logic of a
program.
2. It is easy and efficient to analyze problem using flowchart.
3. During program development cycle, the flowchart plays the role of a guide
or a blueprint. Which makes program development process easier.
4. After successful development of a program, it needs continuous timely
maintenance during the course of its operation. The flowchart makes
program or system maintenance easier.
5. It helps the programmer to write the program code.
6. It is easy to convert the flowchart into any programming language code as
it does not use any specific programming language concept.

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.

Ex: Flowchart for Addition of two numbers

4|Page
2.

Ex: Flowchart for finding greatest of two numbers.

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 Assembly High Level


Language Language 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.

Assembly language and Assembler:


• A low level first generation computer language, popular during early 1960s,
which uses abbreviations or mnemonic codes (mnemonic means mind full) for
operation codes and symbolic addresses. This symbolic instruction language is
called Assembly language.
• One of the first steps in improving the program preparation was to substitute
mnemonics for operation codes. The mnemonics are different among makes
and models of computer.
• Second step was symbolic addressing to express an address in terms of symbols
convenient to the programmer.

7|Page
• The mnemonics are converted into binaries with the help of a translator known
as Assembler.

• The program written using mnemonics is called Source Program or assembly


language program, the binary form of the source program equivalent is called
Object Program.
• Assembler is used to convert assembly language into the machine language.
• Assembly language programs are commonly used to write programs for
electronic controls using microprocessors e.g., compilers, operating systems,
animation in computer graphics and so on.
• Assembly language is relatively easy for the human beings compared to
machine language. Programs writing are faster compared to machine language.
• Assembly language programmer should know details of the architecture of the
machine. Assembly language programs are not portable.

Higher level languages:


• Instructions which are written using English language with symbols and digits
are called high level languages and is closer to our natural language.
• The commonly used high level languages are FORTRAN, BASIC, COBOL,
PASCAL, PROLOG, C, C++, JAVA etc.
• The complete instruction set written in one of these languages is called a high
level language program or computer program or source program.
• In order to execute the instructions, the source program is translated into
binary form by a compiler or interpreter.
• It is also necessary to create an executable program to execute the instructions
given in a source program by linking the input and output devices with your
program.
• A linker (another program) is used to link library routing and generate an
executable program from an object program.

Compiler:

High level Machine


Language Compiler Language
• Compiler is a translator that converts the program instructions from human
understandable form (high level language) to machine understandable form
(machine language) and the translated program instruction is called object
code.
• Every programming language requires its own compiler to translate the
program.

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:

High level Interpreter Machine


Language Language
• An interpreter is a program which takes the source program line by line and
converts into machine code but execute each line by line as it is entered.
• The translation of the source program takes place for every run and is slower
than the compiled code.
• An interpreter must accompany the object code to run a program. Programming
languages BASIC, LISP, JAVA use interpreters.

Generation of Programming Languages:

There are five generations of Programming languages.


They are:
1. First Generation Language :
The first generation languages are also called machine languages/ 1G
language. This language is machine-dependent. The machine language statements
are written in binary code (0/1 form) because the computer can understand only
binary language.
Advantages :
1. Fast & efficient as statements are directly written in binary language.
2. No translator is required.
Disadvantages:
1. Difficult to learn binary codes.
2. Difficult to understand – both programs & where the error occurred.

2. Second Generation Language :


The second-generation languages are also called assembler languages/ 2G
languages. Assembly language contains human-readable notations that can be
further converted to machine language using an assembler.

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.

3.Third Generation Language :


The third generation is also called procedural language /3 GL. It consists of the
use of a series of English-like words that humans can understand easily, to write
instructions. Its also called High-Level Programming Language. For execution, a
program in this language needs to be translated into machine language using
Compiler/ Interpreter. Examples of this type of language are C, PASCAL, FORTRAN,
COBOL, etc.
Advantages :
1. Use of English-like words makes it a human-understandable language.
2. Lesser number of lines of code as compared to above 2 languages.
3. Same code can be copied to another machine & executed on that machine
by using compiler-specific to that machine.
Disadvantages :
1. Compiler/ interpreter is needed.
2. Different compilers are needed for different machines.

4. Fourth Generation Language :


The fourth-generation language is also called a non – procedural
language/ 4GL. It enables users to access the database. Examples: SQL,
Foxpro, Focus, etc. These languages are also human-friendly to understand.
Advantages :
1. Easy to understand & learn.
2. Less time required for application creation.
3. It is less prone to errors.
Disadvantages :
1. Memory consumption is high.
2. Has poor control over Hardware.
3. Less flexible.

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.

Steps in learning English Language:

Vocabulary Sentences Essays or


Alphabets Paragraphs Books
[Words] [grammer Story
rules]

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 */

Preprocessor Directives or Link Section :


It provides instructions to the compiler to link the functions for executing a
program. It is also used to define symbolic constants of the program. To write a
program this section is used to include predefined or user-define header files. A
header file has an extension .h and these are included using “ #include “.
Ex : #include<stdio.h> (standard input/output header file)
#inclide<conio.h> (console input/output header file)
#define PI 3.14 (assigns symbolic name PI to the constant value 3.14)

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;

main() function Section :


Every C program must have only one main() function. Without this section the
program will not run. This is the starting point for program execution. This section
contains two parts
1. Declaration Part
2. Execution Part
Declaration part :
Variables which are used in the main() program are declared by using the
variable declaration statement called as Local Variable Section. Local variables
can be declared as :
datatype variable1,variable2,……….;
Ex : int a,b,c;
float x,y;
char ch;
Execution part :
This section has reading, writing and processing statements having
input/output functions, conditional statements, looping statements, function
calling statements etc. this section executes all the statements and there must
be atleast one statement in the execution section.
Ex : printf(“hai welcome”);
c=a+b;
Both Declaration and Execution section must appear in between the opening
“ { “ brace and closing “ } “ brace. The closing brace of the main() function is the
logical end of the program.
All the statements in the declaration and executable section must end with a
semicolon “ ; “.

Sub program Section or User-define function Section :


This section contains all user-defined functions that are called in main
function. Every sub program function section has local variable declaration section
and executable section. Instead of writing the group of statements again and again in
the main() function program, we can write it once in the function sub program and
call the function again and again in the main() program according to the user
requirement.

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

Step 2: Creating a Source Code

Source code is a file with C programming instructions in a high-level language.


To create source code, we write the program instructions. The instructions written in
the source code must follow the C programming language rules.
Step 3: Saving the program (shortcut key - press function key F2)

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

You might also like