0% found this document useful (0 votes)
18 views26 pages

Lecture-2 - 3 (Pic) .PDF 5

Uploaded by

sudeshmehar3
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)
18 views26 pages

Lecture-2 - 3 (Pic) .PDF 5

Uploaded by

sudeshmehar3
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/ 26

PROGRAMMING IN ‘C’

 To write a program (tells what to do) for a computer, we must use a


computer language. Over the years computer languages have
evolved from machine languages to natural languages. The
following is the summary of computer languages
1940‘s--Machine Languages
1950‘s--Symbolic Languages
1960‘s--High Level Languages
 In the earliest days of computers, the only programming
languages available were machine languages. Each computer has
its own machine language which is made of streams of 0‘s and
1‘s. The must be in streams of 0‘s and 1‘s. This is also referred
as instructions in machine language binary digits. These are so
named as the machine can directly understood the programs
 Advantages:
1. High speed execution
2. The computer can understood instructions immediately
3. No translation is needed.
 Disadvantages:
1. Machine dependent
2. Programming is very difficult
3. Difficult to understand
4. Difficult to write bug free programs
5. Difficult to isolate an error
 In the early 1950‘s Admiral Grace Hopper, a mathematician and naval officer,
developed the concept of a special computer program that would convert programs
into machine language. These early programming languages simply mirrored the
machine languages using symbols or mnemonics to represent the various language
instructions. These languages were known as symbolic languages. Because a
computer does not understand symbolic language it must be translated into the
machine language. A special program called an Assembler translates symbolic
code into the machine language. Hence they are called as Assembly language.
 Advantages:
1. Easy to understand and use
2. Easy to modify and isolate error
3. High efficiency
4. More control on hardware
 Disadvantages:
1. Machine Dependent Language
2. Requires translator
3. Difficult to learn and write programs
4. Slow development time
5. Less efficient
 The symbolic languages greatly improved programming efficiency they still required
programmers to concentrate on the hardware that they were using working with
symbolic languages was also very tedious because each machine instruction had to be
individually coded. The desire to improve programmer efficiency and to change the
focus from the computer to the problems being solved led to the development of high-
level languages.
 Advantages:
1. Easy to write and understand
2. Easy to isolate an error
3. Machine independent language
4. Easy to maintain
5. Better readability
6. Low Development cost
7. Easier to document
8. Portable
 Disadvantages:
1. Needs translator
2. Requires high execution time
3. Poor control on hardware
4. Less efficient
Feature Machine Assembly High Level

Form 0‘s and 1‘s Mnemonic codes Normal English

Machine Dependent Dependent Dependent Independent

Translator Not Needed Needed(Assembler) Needed(Compiler)

Execution Time Less Less High

Languages Only one Different Different Languages


Manufactgurers

Nature Difficult Difficult Easy

Memory Space Less Less More


 These are the programs which are used for converting the programs
in one language into machine language instructions, so that they can
be executed by the computer.
1. Compiler: It is a program which is used to convert the high level
language programs into machine language
2. Assembler: It is a program which is used to convert the assembly
level language programs into machine language
3. Interpreter: It is a program, it takes one statement of a high level
language program, translates it into machine language instruction
and then immediately executes the resulting machine language
instruction and soon.
COMPILER INTERPRETER

A Compiler is used to compile an An interpreter is used to translate each


entire program and an executable line of the program code immediately as
program is generated through the it is entered
object program
The executable program is stored in a disk The executable program is generated in
RAM and the interpreter is required for
for future use or to run it in another each run of the program

computer

The compiled programs run faster The Interpreted programs run slower

Most of the Languages use compiler A very few languages use interpreters.
 The procedure for turning a program written in C into machine
Language. The process is presented in a straightforward, linear
fashion but you should recognize that these steps are repeated many
times during development to correct errors and make improvements
to the code.
 The following are the four steps in this process
1. Writing and Editing the program
2. Compiling the program
3. Linking the program with the required modules
4. Executing the program
Sl. No. Phase Name of Code Tools File
Extension

1 TextEditor Source Code C Compilers .C


Edit, Notepad
Etc..,
2 Compiler Object Code C Compiler .OBJ

3 Linker Executable C Compiler .EXE


Code

4 Runner Executable C Compiler .EXE


Code
 Writing and Editing Programs
The software used to write programs is known as a text editor. A text editor helps us enter,
change and store character data. Once we write the program in the text editor we save it using
a filename stored with an extension of .C. This file is referred as source code file.
 Compiling Programs
The code in a source file stored on the disk must be translated into machine language. This is
the job of the compiler. The Compiler is a computer program that translates the source code
written in a high-level language into the corresponding object code of the low-level language.
This translation process is called compilation. The entire high level program is converted into
the executable machine code file. The Compiler which executes C programs is called as C
Compiler. Example Turbo C, Borland C, GC etc.,
The C Compiler is actually two separate programs:
The Preprocessor
The Translator
 The Preprocessor reads the source code and prepares it for the translator. While preparing the
code, it scans for special instructions known as preprocessor commands. These commands
tell the preprocessor to look for special code libraries. The result of preprocessing is called
the translation unit.
 After the preprocessor has prepared the code for compilation, the translator does the actual
work of converting the program into machine language. The translator reads the translation
unit and writes the resulting object module to a file that can then be combined with other
precompiled units to form the final program. An object module is the code in the machine
language.

 Linking Programs
The Linker assembles all functions, the program‘s functions and
system‘s functions into one executable program.

 Executing Programs
To execute a program we use an operating system command, such as
run, to load the program into primary memory and execute it.
Getting the program into memory is the function of an operating
system program known as the loader. It locates the executable
program and reads it into memory. When everything is loaded the
program takes control and it begin execution.
 Algorithm – Sequential instructions needed to solve any
problem.
 Algorithm is translated into Flowchart and program
according to the requirement.
 Problem should be divided into smallest modules possible,
while developing Algorithm.
 It is called the Modular approach, which makes the program
more flexible for future modifications.
 Algorithm must be written in such a way that they can be
easily converted into computer instructions.
The desired features of an algorithm are:
 Each step of the algorithm should be simple.
 It should be unambiguous in the sense that the logic is
clear.
 It should be effective i.e., it must lead to a unique
solution of the problem.
 It must end in a finite number of steps.
 It should be as efficient as possible.
Not all procedures can be called an algorithm. An algorithm
should have the following characteristics −
 Unambiguous − Algorithm should be clear and unambiguous.
Each of its steps (or phases), and their inputs/outputs should be
clear and must lead to only one meaning.
 Input − an algorithm should have 0 or more well-defined inputs.

 Output − an algorithm should have 1 or more well-defined


outputs, and should match the desired output.
 Finiteness − Algorithms must terminate after a finite number of
steps.
 Feasibility − should be feasible with the available resources.

 Independent − an algorithm should have step-by-step directions,


which should be independent of any programming code.
 Flowchart – A collection of diagrammatically
represented symbols connected with arrowheads, which
represents a sequence of instructions for information
processing.
 John von Neumann – said to have made the first
Flowchart in 1945.
 Flow charts are easy-to-understand diagrams that show
how the steps of a process fit together.
 It is nothing but a diagrammatic representation of the
various steps involved in designing a system.
 A flowchart is a diagrammatic representation that
illustrates the sequence of operations to be performed to get
the solution of a problem.
 Some of the boxes, which are used in flowcharts, are
 The following are some guidelines in flowcharting:
 (a) In drawing a proper flowchart, all necessary requirements should be
listed out in logical order.
 (b) The flowchart should be clear, neat and easy to follow. There should
not be any room for ambiguity in understanding the flowchart.
 (c) The usual direction of the flow of a procedure or system is from left
to right or top to bottom.
 (d) Only one flow line should come out from a process symbol.
 (e) Only one flow line should enter a decision symbol, but two or three
flow lines, one for each possible answer, should leave the decision
symbol.
 (f) Only one flow line is used in conjunction with terminal symbol.
 (g) Write within standard symbols briefly. As necessary, you can use
the annotation symbol to describe data or computational steps more
clearly.
 (h)If the flowchart becomes complex, it is better to use connector
symbols to reduce the number of flow lines. Avoid the intersection of
flow lines if you want to make it more effective and better way of
communication.
 (i) Ensure that the flowchart has a logical start and finish.
 (j) It is useful to test the validity of the flowchart by passing through it
with a simple test data.
1. Start
2. Initialize the three numbers let say a,b,c
3. Check if a is greater than b.
4. If true, then check if a is greater than c.
◦ If true, then print ‘a’ as the greatest number.
◦ If false, then print ‘c’ as the greatest number.
4. If false, then check if bis greater than num3.
◦ If true, then print ‘b’ as the greatest number.
◦ If false, then print ‘c’ as the greatest number
5. Stop
Flowchart to find greater of three numbers
 1. Develop an Algorithm to add three numbers and
convert it into Flowchart.
 2. Develop an Algorithm to read two numbers and print
the bigger number. Convert it into Flowchart.
 3. Develop an Algorithm to find the biggest of the three
given numbers and convert it into Flowchart.
 4. Develop an Algorithm to find the sum of 10 given
numbers and convert it into Flowchart.
 5. Develop an Algorithm to find the average of 5
numbers and convert it into Flowchart.

You might also like