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

BIT104 SLM Library - SLM - Unit 01

Uploaded by

pavanmay227597
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)
19 views

BIT104 SLM Library - SLM - Unit 01

Uploaded by

pavanmay227597
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/ 24

Principles of C Programming Unit 1

Unit 1 Introduction to Principles of Programming

Structure:
1.1 Introduction
Objectives
1.2 Programming Languages
Machine Level Languages
Assembly Level Languages
High Level Languages
1.3 Programming Domain
Scientific Applications
Business Applications
Artificial Intelligence
Systems Programming
World Wide Web and Web Application
Data Mining
1.4 Program Development Cycle
1.5 Case study: Converting Temperature from Centigrade to
Fahrenheit
1.6 Program Translation Process and Execution Process
Program Translation Process
Program Execution Process
1.7 Problem solving using Algorithms and Flowcharts
Algorithm
Flow Charts
1.8 Performance Analysis and Measurements
1.9 Summary
1.10 Terminal Questions
1.11 Answers
1.12 Exercises

1.1 Introduction
Presently computers are part of our day to day life. We use them to solve
complex problems in scientific application, at the workplace to perform day
to day activities and solve complex problem, or look for driving directions,
have fun and communicate. They have several applications in the business

Sikkim Manipal University B2074 Page No.: 1


Principles of C Programming Unit 1

world, education, shopping complex, the entertainment industry,


telecommunications, research and finance, among others
Despite the fact that computers are used everywhere in our day to day life,
very few people know how they really work. Programs (Software) installed in
the computers makes the computers available for different types of
applications and users.
Programs are means to organize and control the computer working through
sequences of instructions. Every computer follows a sequence of
instructions, which are available in binary form in the computer’s main
memory.
“To write a program” means writing sequences of instructions in order to
organize the work of the computer to perform specific tasks. These
sequences of instructions are called “computer programs”.
Programmers are the people who create this sequence of instructions
depending upon the requirement of application. A programming language
allow us to write programs.
Before we start with the concepts of C programming languages, we must
have a basic understanding of the programs and their requirement and
development environment. In this unit, we briefly describe the major
programming languages and their levels. Then, we will discuss the various
domain of programming languages, which will give an idea about the
different areas where the different kind of programming languages are used.
We will also discuss the popular methodologies of programming with case
study. We will illustrate the concept of program translation using compiler
and interpreter. We will also discuss about algorithm and flow chart, which
are useful tools for representation of the problem. Finally, performance
analysis and measurement will be discussed.
Objectives:
After studying this unit, you should be able to:
 discuss various domains of programming language
 describe various categories of programming languages
 discuss the two popular design methodologies of programming
 explain program development cycle with case study
 develop algorithms and flowcharts for solving the problem

Sikkim Manipal University B2074 Page No.: 2


Principles of C Programming Unit 1

 illustrate program translation and execution technology


 describe the performance analysis and measurements

1.2 Programming Languages


Writing a program requires writing a list of instruction for the computer to
perform. A programming language is used for writing down the set of
instructions (i.e. program) that a computer will follow. The three main
distinguishing levels of computer programming languages are:
a) Machine level languages
b) Assembly level languages
c) High level languages
Let us discuss all these levels in detail.
1.2.1 Machine Level Languages
In initial days, machine level language was used to write programs in
computer. In machine languages 0’s and 1’s are used to write a program. A
program instruction in machine language may look like something like this:
10010101100101001111101010011011100101
Advantages
 This language is directly related to the CPU of the computer; and hence
program execution is very fast.
 As the programs are written in machine language, there is no need to
convert theses programs by using any compiler, assembler or interpreter.
Disadvantages
 It is machine dependent language which means that each computer
family has its own machine language.
 Finding error and debugging of machine language is very tough.
 Programs are not portable, as they are machine dependent.
1.2.2 Assembly Level Languages
Assembly level languages are an improvement over the machine level
languages. In assembly level languages, Names and Symbols are used to
represent the program instruction. Writing program in assembly languages
is easy compared to machine languages. An assembly language program
looks like:

Sikkim Manipal University B2074 Page No.: 3


Principles of C Programming Unit 1

MOV AL, 5; Copies 5 into register AL


ADD AL, BL; Add content of AL and BL registers and stores result in AL
The usage of symbolic words and symbolic names greatly increases the
degree of readability of program. Each computer family has its own
assembly language. In order to write programs in a particular computer, the
corresponding Assembly language has to be learnt.
The instructions written in assembly language are not directly understood by
computers. This is because computers understand only 0s and 1s. So the
program written in assembly language should be converted into their
machine level languages. Assemblers are system programs, which convert
assembly language program into machine language programs.
1.2.3 High Level Language
High level languages allow programmers to write a program closer to
language understood by human beings. High level languages are
independent of the CPU on which they will be executed. The instruction in a
high level computer language would look something like this:
x=y+5;
This statement means “add value of variable y and constant value of 5, and
store the results in variable x”.
High level language has replaced machine and assembly level languages in
various areas of programming because they provide benefits like:
1. Readability of familiar notations
2. Machine independence
3. Availability of program libraries
4. Consistency check during implementation that can detect errors
The first high level language was FORTRAN, developed in 1957 by a team
led by Backus at IBM. Example of high level languages are C, C++, prolog,
Perl, Python, java, C#, R, Ruby, Ruby on Rail and so on.
Programmers find it easier to express problem solution in high level
languages, but computer do not understand these languages. Thus, before
the execution of high level language, they need to be converted into
machine languages. The program that does these translations is compilers
and Interpreter. We will discuss compilers and interpreter in detail in section
1.6.

Sikkim Manipal University B2074 Page No.: 4


Principles of C Programming Unit 1

Self-Assessment Questions
1. ________ are who create sequence of instructions depending upon the
requirement of application.
2. In ________ languages, Names and Symbols are used to represent
the program instruction.
3. __________ level languages allows programmer to write program
closer to language understand by human being.

1.3 Programming Domain


Computers are used in different areas like in scientific application, business
application, Artificial intelligence and so on. With increasing diversity in
computer use, different programming languages have been developed. In
this section, we will discuss some domains of programming along with their
related programming languages.
1. Scientific Application
2. Business Application
3. Artificial Intelligence
4. System Programming
5. World wide web and Web Application
6. Data Mining
1.3.1 Scientific Applications
Typically, scientific applications have simple data structures but require
large numbers of floating-point arithmetic computations. The most common
data structures are counting loops and selections. FORTRAN is the first
language which was used in scientific applications. Other high level
languages like ALGOL 60 and most of its descendants were also intended
to be used in this area. Other widely used languages in scientific application
are C, matlab and python.
The NumPy (numeric programming extension for Python) includes standard
mathematical libraries. NumPy turns Python into a sophisticated yet easy to
use numeric programming tool, which can often replace existing codes
written in FORTRAN or C. Python also supports animation, 3D visualization,
parallel processing and so on.

Sikkim Manipal University B2074 Page No.: 5


Principles of C Programming Unit 1

1.3.2 Business Applications


In business application, we need to be able to represent data in a manner
that can be understood by human users, as well as need precise ways of
describing and storing decimal numbers, and character data. Business
applications can range from large line-of-business systems to specialized
tools. Consider all the applications that run on either client computers or
servers, including commercial off-the-shelf products, customized third-party
systems, and internally developed systems. The COBOL (Common
Business-Oriented Language) was the first high-level language used in
business application. Presently along with COBOL, many programming
languages used in this domain which include PL/I (Programming Language
one), C, C#.NET, JAVA, and Ruby. Spreadsheet system and database
systems are widely used technologies in business application.
1.3.3 Artificial Intelligence
Artificial intelligence (AI) is a vast area of computer applications. AI focuses
on nontrivial problems that require reasoning and are often solved by
humans. Artificial Intelligence is characterized by the use of symbolic rather
than numeric computations. Symbolic computation means that symbols
consisting of names are manipulated, rather than numbers. Artificial
intelligence has been used in a wide range of fields including medical
diagnosis, stock trading, speech recognition, robot control, law, remote
sensing, scientific discovery and toys.
LISP (List Processor) is the first widely used programming language for AI
applications by McCarthy et al., which appeared in 1959. It is based on
mathematical concepts. An alternative approach to LISP is logic
programming using Prolog language, which was developed by Clocksin and
Mellish. C programming language may be used for some AI application
development.
1.3.4 Systems Programming
System Software consists of a variety of programs that support the
operation of a computer. It makes possible for the user to focus on an
application or other problems to be solved, without needing to know the
details of how the machine works internally. It includes mainly the Operating
system, device drivers, assemblers, compilers, file management tools,
system utilities, and debuggers. The Operating System (OS) manages the

Sikkim Manipal University B2074 Page No.: 6


Principles of C Programming Unit 1

computer hardware (i.e. various Input/output devices of the computer) and


enables the user to interact with the computer. A compiler is system
software that is used to convert programs written in high level language into
machine language. Assemblers translate mnemonic instructions into
machine code of the computer.
There were many languages used for development of system software like
PL/S (Programming Language / System), Bliss, and so on. Presently, most
of the system software is written in general programming language like C.
The Unix OS is written in C language. The Unix OS is easy to move to
different machines.
1.3.5 World Wide Web and Web Application
The World Wide Web is collection of hyper link documents. World Wide
Web was invented by Tim Berners-Lee in 1989. Tim Berners-Lee had
specified the three fundamental technologies that remain the foundation of
today’s Web and which you may have seen appear on parts of your Web
browser. These are:
HTML or Hypertext Mark-up Language: It is the standard protocol for
formatting and displaying documents on the World Wide Web. HTML is
used to create web pages. It is the set of markup symbols or codes inserted
in a web page intended for display on a World Wide Web browser page.
The markup tells the Web browser how to display a Web page's words and
images for the user.
URI (Uniform Resource Identifier): It is the generic term for all types of
names and addresses that refer to objects on the World Wide Web. A URL
(Uniform Resource Locator) is one kind of URI.
HTTP: Hypertext Transfer Protocol: It is the protocol used by the World
Wide Web. HTTP defines how messages are formatted and transmitted,
and what actions Web servers and browsers should take in response to
various commands. For example, when you enter a URL in your browser,
this actually sends an HTTP command to the Web server directing it to fetch
and transmit the requested Web page.
Web Application is application software that is run on the browser. Web
Applications are supported by various collections of languages, mark-up
languages such as HTML, general programming languages like java, C#

Sikkim Manipal University B2074 Page No.: 7


Principles of C Programming Unit 1

and Scripting languages like JavaScript, PHP to provide the computation


capability in presentation of HTML document. Servlet, JSP, ASP and
ASP.Net are languages used to develop web applications. Ruby on Rails, or
simply Rails, is an open source web application framework written in Ruby.
1.3.6 Data Mining
Data Mining represents a process developed to examine large amounts of
data routinely collected. Data mining is used in most areas where data is
collected as in the case of marketing, health, communications, and so on.
For example, retail stores routinely use data mining tools to learn about
purchasing habits of their customers. The most popular languages used in
Data Mining are R, Python SQL (Structured Query Language),
Pig/Hive/Hadoop-based languages, matlab, SAS, and Microsoft Analysis
Service.
R is a free software programming language and software environment for
statistical computing and graphics. R is an integrated suite of software
facilities for data manipulation, calculation and graphical display. Presently,
the R language is widely used among data miners for developing statistical
software and data analysis.
In Short, Generally languages are at least one domain specialized:
FORTRAN and Python are most highly suited to scientific programming,
Smalltalk for GUI programming, Java and ASP.NET for Internet
programming, C for UNIX system programming, Visual Basic for Microsoft
Windows. Some languages are particularly narrow in applicability; these are
called Domain-specific languages. For example, SNOBOL and Perl are text
processing languages. Prolog is useful for implementing rule-based systems.
SQL is a database query language; XML is a data representation language.
Self-Assessment Questions
4. The ____________ manages the various I/O devices of the computer
and enables the user to interact with the machine.
5. The _________ is collection of hyper link document.
6. COBOL stands for ___________.
7. The Unix OS is written in ________ language.

Sikkim Manipal University B2074 Page No.: 8


Principles of C Programming Unit 1

1.4 Program Development Cycle


Programming is a form of problem-solving. It involves identifying the
problem, analyzing it, designing a framework for the solution of the problem
and writing algorithm, writing the program, testing the program and finally,
writing a documentation for it. Good programming required to follow
program development steps:
1. Identify and Analyze the problem
2. Design the solution and Algorithm
3. Implement the Algorithm
4. Test and Verify the completed problem
5. Documentation and Maintenance of program
1. Identify and Analyze the problem: The first step to successful
programming is getting to know your problem. You should have clear picture
of problem and what you want to do. To analyze the problem and decide
what it supposed to accomplish, you need a precise definition of the
problem and a clear understanding what is required for its solution. This can
be done by asking yourself questions (enquiry) and brainstorming.
Analysis: Analyzing the problem involves in identifying the problem
(a)inputs, that is, data required for the program (b) Outputs, that is, desire
result of the program; (c) any additional requirements or constraint on the
solution. At this stage, we should also determine the required format in
which the result should be displayed (for example, as table with specific
column headings). We should develop the list of problem variables and their
relationships and these relationships may be expressed as formulas.
2. Design the solution and Algorithm: Once we have figured out the
problem, we need to design a way to solve it. At this stage, we do not write
any code. We only draw up a framework which we will follow during the
algorithm design stage. The list of steps required to solve the problem is
called algorithm. Writing Algorithm is the most difficult part of the problem
solving process. Let us discuss various approaches and terms used in
designing of the program.
Modular Programming: The Modular approach of programming involves
breaking a program down into sub components called modules. Each
module is composed of an independent or self-contained set of instructions.
Modules are also referred to as routines, subroutines or procedures. Each

Sikkim Manipal University B2074 Page No.: 9


Principles of C Programming Unit 1

module is designed to perform a specific task in the overall program, such


as to calculate the gross pay of an employee in a payroll program.
The advantage of modular programming is the ability to write and test each
module independently and we can reuse those modules in other programs.
Program Design Methodology: You can use top-down or bottom up
approaches in order to design a program.
The Top-Down Approach: With the top-down approach, we start with our
top-level program, then divide and sub-divide it into many modules. The
division process is known as stepwise refinement. Fig.1.1 shows the top-
down approach. As we design each module, we will discover what kind of
sub modules we will need and then go on to program those modules. After
programming our smaller sub modules, we will group them together into the
larger module.
The key to stepwise refinement is that each module should end up doing
only one task. Bottom-level modules can be completed using only a few
lines of code.
One advantage of the top-down approach is that we see the full picture of
problem at first. When we write our high-level modules, we do not need to
bother about the smaller details. Our lower-level modules are designed with
our higher-level modules in mind, and we already know in great detail what
we want to achieve with them. This ensures compatibility between high and
lower levels. Therefore, the top-down approach is suitable for larger
problems.

Figure 1.1: The Top-Down approach

Sikkim Manipal University B2074 Page No.: 10


Principles of C Programming Unit 1

The Bottom-Up approach: The bottom-up approach, as the name


suggests, starts from the bottom of our structure and slowly gets to the top.
Fig. 1.2 shows the bottom-up approach. To be more precise, we start by
creating the smallest modules, put them together to form larger modules,
and continue the process until we have our whole thing built. This approach
to programming is especially useful when we already have pre-made
modules (that is modules which are already present).
When we use top-down design approach, each of our individual modules
was designed to work for the above module only. When we use bottom-up
design approach, each of our individual modules serves a purpose on its
own. Therefore, the smaller modules can be used more than one larger
module, facilitating code reuse. Moreover, general-purpose modules are
easier to read and understand than specific ones, so the bottom-up
approach also enhances code readability.
In the bottom-up approach, we do not get to see the full picture at first.
When we write our low-level modules, we do not have our higher-level ones
in mind. This may lead to poor connection between the higher- and lower-
level modules. It is also more difficult to monitor and manage the project.
Therefore, the bottom-up approach is better suited for smaller problems.

Figure 1.2: The Bottom-Up approach

We are going to use top down design. In top-down design, we first list major
steps or sub problem that needed to solve. Once we know the sub problem,
we can solve each one individually and write the algorithm for it. In general,
most computer algorithms consists of at least one of the following steps:

Sikkim Manipal University B2074 Page No.: 11


Principles of C Programming Unit 1

Algorithm for a Programming Problem:


1. Get the data
2. Perform the computation
3. Display the results.
We will discuss about algorithm, its advantages and disadvantages in detail
in section 1.7.
3. Implement the Algorithm: Implementation involves writing algorithm
steps as program statements. We must convert each algorithm step into
one or more statements in a programming language.
4. Test and Verify the completed problem: Testing and verifying the
program requires testing the completed program to verify that it works as
desired. It involves writing test cases with different sets of data and then run
the program several times using different sets of data to make sure that it
works correctly for every situation provided for the algorithm.
5. Documentation and Maintenance: Documentation is an important part
of program development. Documentation is the written text that
accompanies computer software program. It either explains how it operates
or how to use it.
Maintaining and updating program involves modifying a program to remove
previously undetected errors and to keep it up to date as company policies
change. A disciplined approach is essential to create a program that is easy
to read, understand and maintain.

1.5 Case Study: Converting Temperature from Centigrade to


Fahrenheit
1. Identify your problem and analysis:
You have to write a program to convert temperature from centigrade degree
to Fahrenheit. You may ask yourself questions like these:
 What Input is required for the program?
 What will be the output of the program?
 What will be the formula for the conversion form degree centigrade to
Fahrenheit?
When you are answering these questions, you should look at the problem
from the end user's angle. You are programming for your end user, so you
Sikkim Manipal University B2074 Page No.: 12
Principles of C Programming Unit 1

should understand user needs. Here are some sample answers for the
above questions:
 Input required for the problem is: Temperature in degree centigrade -
Centigrade
 Output produced by the problem is: Temperature in Fahrenheit -
Fahrenheit
Once you know the input and outputs, you list out the formulas required for
the solution of problem.
Formula for conversion of centigrade to Fahrenheit is as:
Fahrenheit =1.8 * Centigrade + 32;
2. Solution Design and algorithm:
The algorithm for the problem to convert from degree centigrade to
Fahrenheit is as:
1. Get temperature in degree.
2. Convert the degree to Fahrenheit
3. Display the temperature in Fahrenheit.
We have to decide whether any steps of the algorithm, need further
refinement or whether are perfectly clear as stated. Step 1 and 3 are basic
steps, require no further refinement. Step 2 is fairly straightforward, but
some detail might help.
Step 2 - refinement:
2.1 The temperature in Fahrenheit is 1.8 times the temperature in
centigrade and then addition of 32.
We list the complete algorithm with refinements below, shows you how it all
fits together.
Algorithm with refinements:
1. Get temperature in degree centigrade.
2. Convert the degree centigrade to Fahrenheit
2.1 The temperature in Fahrenheit is summation of 1.8 times the
temperature in centigrade and then addition of 32.
3. Display the temperature in Fahrenheit.
3. Implementation: To implement the solution, we will write the program in
C language. Program 1.1, is a C program along with a sample output of the

Sikkim Manipal University B2074 Page No.: 13


Principles of C Programming Unit 1

program after execution. The bold lines in the program correspond to the
algorithm steps. Do not worry about detail understanding of program for now.
We will study how to write program in C language from next chapter
onwards. The printf function, which is used to display the result and scanf
function, which is used to get input from the user. The printf and scanf
function are defined in library called stdio.h.
centi and fah are variable corresponding to Centigrade degree and
Fahrenheit degree temperature respectively.
Program 1.1: problem to convert from degree centigrade to Fahrenheit
#include<stdio.h>
int main(void)
{
// input centigrade in variable centi
//output Fahrenheit in stored in variable fah
float centi, fah;
clrscr();
printf(“Enter temperature in centigrade :”);
scanf(“%f”,&centi); // input - centigrade
fah= (1.8 * centi ) + 32; // conversion to Fahrenheit
pritnf(“\nTemperature in Fahrenheit = %f”,f); // display
result
getch();
}
Sample Output
Enter temperature in centigrade :32
Temperature in Fahrenheit = 89.59998

4. Test and verify the completed problem


How do you know the sample output of the program is correct? We are
going to examine the program output with its desire result. As result of
execution of program, a centigrade degree with input value of 32 is
converted to output value of 89.59998 Fahrenheit, as it should be. We
should verify that the program works properly, enter a few more test values
of centigrade degree and verify the output.

Sikkim Manipal University B2074 Page No.: 14


Principles of C Programming Unit 1

Self-Assessment Questions:
8. The _________approach of programming involves breaking a program
down into sub components called modules.
9. State True/False: Testing involves writing test cases with different sets
of data.

1.6 Program Translation Process and Execution Process


1.6.1 Program Translation Process:
Computers only understand sequences of 1s and 0s. High Level languages
are more for human understanding and thus make the task of programming
simpler. However, since the computer only understands low level binary
code (often called machine code); there must be a translation process to
convert these high level languages to machine code. This is often done by
language translators. Compiler and interpreter come under categories of
language translators that are software packages that translate high level
languages into machine code.
Compiler: It is a program which translates a high level language program
into a machine language program. Thus, compiler is language translator
which translates source code written high level language into object code
which is in binary form. Many high level language like C and C++, uses
compiler for translation processes. Fig. 1.3 shows the process of
compilation.

High Level Machine Code


Language code Compiler (Object Code)
(Source Code)

Figure 1.3: Compilation Process

Interpreter: An interpreter is a program which translates statements of a


program into machine code and executes it, one statement of the program
at a time. Interpreter reads one statement of program, translates it and
executes it. Then it reads the next statement of the program again
translates it and executes it. In this way it proceeds further till all the
statements are translated and executed. Interpreter does not translate the

Sikkim Manipal University B2074 Page No.: 15


Principles of C Programming Unit 1

source program into object code. Well-known examples of interpreters are


Basic interpreter, Lisp interpreter, UNIX shell command interpreter, and
SQL interpreter.
Interpreter is preferred when:
1. Programmer is working in interactive mode and needs to view and
update variables
2. Running speed is not important
3. Commands have simple formats, and thus can be quickly analysed and
executed
4. Modification or addition to user programs is required as execution
proceeds.
The main differences between compiler and interpreter are:
 The interpreter takes one statement then translates it and executes it
and then takes another statement. While the compiler translates the
entire program in one go and then executes it.
 Compiler generates the error report after the translation of the entire
program, while an interpreter will stop the translation after it gets the first
error.
 Compiler takes a larger amount of time in analyzing and processing the
high level language code. In contrast the interpreter takes lesser time for
the same process.
 Besides the processing and analyzing time the overall execution time of
a program is faster for the compiler when compared to the interpreter.

1.6.2 Program Execution Process


In the execution of program, two more system software play an important
role. They are linker and loader, which are discussed below.
Linker: Built-In functions are those whose definition is pre-defined and
stored in the Header Files like "stdio.h" header file which contains definition
of printf(), scanf(),... Function. (Which is included in example program in
program 1.3 in first line #include<stdio.h>). These libraries are predefined
and these contain basic functions which are essential for executing the
program. Linker links built-in function with program. In another words, linker
links library code with the object code to produce the .exe file. If linker does
not find a library of a function then it informs the compiler and then the

Sikkim Manipal University B2074 Page No.: 16


Principles of C Programming Unit 1

compiler generates an error. Linker also links user defined library functions
with program. We will discuss user defined function later. Generally, the
compiler automatically invokes the linker as the last step in compiling a
program.
Loader: A loader is the part of an operating system that is responsible for
loading programs. It is one of the essential stages in the process of starting
a program, as it places programs into memory and prepares them for
execution. Loading a program involves reading the contents of executable
file, the file containing the program text, into memory, and then carrying out
other required preparatory tasks to prepare the executable for running.
Once loading is complete, the operating system starts the execution of
program.
Fig. 1.4 shows the complete process of translation of source code into
executable program and execution of program.

Object Code
Source Code Compiler (Convert.obj) Linker
(Convert.c)

Executable
Main Memory Loader program
(Convert.exe)

Figure 1.4: Source code translation into Executable code and Loading

Self-Assessment Questions
10. ________ is a language translator, which translate source code written
high level language into object code.
11. An interpreter is a program which translates statements of a program
into machine code and execute it, one statement of the program at a
time. (State True/False)

1.7 Problem solving using Algorithms and Flowcharts


1.7.1 Algorithm
The Algorithm is defined as “a step-by-step procedure to solve a given
problem”. It is procedural solution to the problem in hand. The algorithm has
the following characteristics:
1. It should be simple and precise.

Sikkim Manipal University B2074 Page No.: 17


Principles of C Programming Unit 1

2. The steps must be unambiguous so that the computer understands the


codes used for the steps properly.
3. It must have finite number of steps.
4. The algorithm should work on all legitimate inputs.
5. The required output should be obtained after the algorithm ends.
Example: Algorithm to find the area of rectangle. The area of rectangle
is given by the product of its length and its breadth. Mathematically, we can
express it as Area = Length * Breadth. Hence, in order to find the area of
rectangle, its length and breadth are required to be known.
Algorithm 1.1: Algorithm to find the area of rectangle.
Inputs: length and breadth
Output: area
Algorithm:
1. read length
2. read breadth
3. area = length * breadth
4. Display area
5. Stop

Advantages and Disadvantages of algorithms:


Advantages:
1. Algorithm is easy to understand, as it is a step-by-step representation of
a solution to a given problem.
2. Algorithms are independent of any programming language.
3. Algorithms make the creation of program in programming languages
easy.
Disadvantages:
1. There are no standard rules governing the construction of algorithms.
2. Writing algorithms consumes considerable amount of time.

1.7.2 Flow Charts


A flow chart is pictorial representation of an algorithm. The step-by-step
procedure is elegantly represented by graphical means and it increases the
degree of clarity in the understanding of the problem.

Sikkim Manipal University B2074 Page No.: 18


Principles of C Programming Unit 1

Symbols Used in Flow Chart:


The flow chart being a symbolic representation, standard symbols are used
for each specific operation. These symbols are used to represent the
sequence of operations and flow of data and documents used for
programming. Flow should be from top to bottom. The most commonly used
symbols are shown in the table 1.1
Table 1.1: Commonly Used Flow Chart Symbols

Shape Type and description


Terminator: A start or end of a process.

Process: A step in the process.

Predefined Process (Subroutine): Another process that


may be known or understood but isn't detailed in the
chart.
Decision: A point at which you must make a decision in
the process.

Connector: Cross-references or leads to another process


within the chart.

Data: Any kind of input or output, such as receiving or


dispersing information.
Document: Anything that is produced for people to read,
such as a printout.
Delay: A wait in the process.

Sikkim Manipal University B2074 Page No.: 19


Principles of C Programming Unit 1

Example of Flow charts:


1. Flow Chart to find the area of rectangle.

Start

Read length

Read breadth

Area = length * breadth

Display area

Stop

2. Flowchart to find Smallest of 2 numbers.

Start

Read a, b
Yes No
Is a<b?

Print a Print b

Stop

Sikkim Manipal University B2074 Page No.: 20


Principles of C Programming Unit 1

1.8 Performance Analysis and Measurements


Some algorithms are more efficient than others. We would prefer to choose
an efficient algorithm, so it would be nice to have metrics for comparing
algorithm efficiency. The efficiency of an algorithm can be decided by
measuring the performance of an algorithm. Performance of algorithms is
measured in terms of the amount of data the algorithm must process. We
can measure the performance of an algorithm by computing two factors.
 Time complexity is a function describing the amount of time an algorithm
takes in terms of the amount of input to the algorithm. "Time" can mean
the number of memory accesses performed, the number of comparisons
between integers, the number of times some inner loop is executed, or
some other natural unit related to the amount of real time the algorithm
will take. We try to keep this idea of time separate from "wall clock" time,
since many factors unrelated to the algorithm itself can affect the real
time (like the language used, type of computing hardware, proficiency of
the programmer, optimization in the compiler, and so on.). It turns out
that, if we chose the units wisely, all the other stuff does not matter and
we can get an independent measure of the efficiency of the algorithm.
 Space complexity is a function describing the amount of memory
(space) an algorithm takes in terms of the amount of input to the
algorithm. We often speak of "extra" memory needed, not counting the
memory needed to store the input itself. Again, we use natural (but
fixed-length) units to measure this. We can use bytes, but it's easier to
use, say, number of integers used, number of fixed-sized structures, etc.
In the end, the function we come up with will be independent of the
actual number of bytes needed to represent the unit. Space complexity
is sometimes ignored because the space used is minimal and/or
obvious, but sometimes it becomes as important an issue as time.

Self-Assessment Questions:
12. The __________ is defines as “A step-by-step procedure to solve a
given problem”.
13. A _______ is a pictorial representation of an algorithm.
14. __________ is a function describing the amount of memory an
algorithm takes in terms of the amount of input to the algorithm.

Sikkim Manipal University B2074 Page No.: 21


Principles of C Programming Unit 1

1.9 Summary
 Programs are means to organize and control the computers working
through sequences of instructions. Programmers are people who create
a sequence of instructions depending upon the requirement of the
application.
 There are different categories of programming languages: Machine
Language, Assembly Language, High level languages.
 Computers are used in different areas like in scientific application,
business application, and Artificial intelligence.
 Programming is a form of problem-solving. It involves identifying the
problem, analyzing it, writing algorithm, writing the program for it, testing
the program and finally, writing a documentation for it.
 Compiler is a program which translates a high level language program
into a machine language program.
 An interpreter is a program which translates statements of a program
into machine code and executes it, one statement of the program at a
time.
 The Algorithm is defines as “a step-by-step procedure to solve a given
problem”. A flow chart is a pictorial representation of an algorithm.
 Performance of algorithm is measured in terms of the amount of data
the algorithm must process. We can measure the performance of an
algorithm by computing two factors: Space and Time Complexity.

1.10 Terminal Questions


1. Explain in detail about any three computer application and their related
programming languages?
2. Write a short note on:
1. Program development cycle.
2. Program translation process.
3. Program Execution Process.
3. What is Algorithm? What are its various characteristics?
4. What is Flow Chart? List advantage and disadvantages of using flow
charts?

Sikkim Manipal University B2074 Page No.: 22


Principles of C Programming Unit 1

1.11 Answers
Self-Assessment Questions
1. Programmers
2. Assembly
3. High
4. Operating System
5. World Wide Web
6. Common Business-Oriented Language
7. C
8. Modular
9. True
10. Compiler
11. True
12. Algorithm
13. Flowchart
14. Space complexity

Terminal Questions
1. Computers are used in different areas like in scientific application,
business application, Artificial intelligence etc. (Refer 1.3 for more detail)
2. Programming is a form of problem-solving. (Refer 1.4 for more detail)
Computers are strange in that they only understand a sequence of 1s
and 0s. High Level languages are more for human understanding and
thus make the task of programming much easier. (Refer 1.6.1 for more
detail)
In the execution of program two more system software plays important
role they are linker and loader, which are discussed below. (Refer 1.6.2
for more detail)
3. The Algorithm is defines as “A step-by-step procedure to solve a given
problem”. (Refer 1.7.1 for detail)
4. A flow chart is pictorial representation of an algorithm. (Refer 1.7 for
detail)

Sikkim Manipal University B2074 Page No.: 23


Principles of C Programming Unit 1

1.12 Exercises
Write Algorithm and draw flow chart for the following problem.
1. To find the area of triangle.
Area = 0.5 * b * h;
2. To find the largest of three number.
3. To calculate net salary of employee. Assume necessary data.
4. To find the simple interest.
Simple interest= (principle * time-period * rate-Of-Interest)/ 100
5. To find type of triangle when its three sides are given
If a, b and c are the sides of triangle then,
If all sides are equal, it is equilateral triangle
If any two sides are equal, it is isosceles triangle
If all the sides are different, it is acute triangle.

Sikkim Manipal University B2074 Page No.: 24

You might also like