0% found this document useful (0 votes)
10 views45 pages

C Programming: DR - Valliappan Raman

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)
10 views45 pages

C Programming: DR - Valliappan Raman

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

C Programming

Dr.Valliappan Raman
Introduction to
LECTURE 1 Programming, Algorithms &
Flow Charts
PROGRAM & PROGRAMMING

 A program is a set of logically related instructions that is


arranged in a sequence that directs the computer in solving a
problem.

 The process of writing a program is called programming.


 Sof tware is a collection of computer programs and related
data that provides the instructions for telling a computer what
to do and how to do it.
 Computer sof tware can be broadly classified into two
categories :
(a) . system sof tware
&
(b) . application sof tware
PROGRAMMING – WHY?

 Computers are used for many dif ferent purposes in many


dif ferent situations.
 But, how can they be so versatile?
 Answer: They can be programmed
 The ability for a computer to be programmed allows it to do
whatever their programs tell them what to do.
 A program is a set of instructions that tell a computer what to
do.
 A computer cannot do anything unless it has a program to tell
it what to do.
 In this class, we will focus on writing these programs.
PROGRAMMING – WHAT?

 Programs are used to operate the components of a computer,


solve problems or satisfy a want/need.
 How long will it take me to get home if I drive x miles per hour?
 I want to be able to tell my friends what I am doing right now.
 Computer Programming is both an Art and a Science
 Every aspect of a program must be carefully designed
 As an art, programming takes creativity and problem solving.
 There is often no one correct way to solve a problem.
 As a science, there are formal and proven methods to go
about creating a programming.
 In this course, you will learn both the art and science of
programming.
HARDWARE AND SOFTWARE

 Programs can also be called sof tware.


 Software refers to the computer programs that a computer uses to
complete a task.
 Hardware refers to the physical components that a computer
is made of.
 A computer is not one device, but a system of devices working in
tandem.
 Each device plays a part.
 Major components:
 Central Processing Unit
 Main Memory
 Secondary Storage Devices
 Input Devices
 Output Devices
CENTRAL PROCESSING UNIT (CPU)

 The CPU is the heart and brain of the computer.


 The CPU continuously does the following things:
1. Fetch an instruction
2. Follow the instruction
3. Produce some resulting data
 The CPU has two parts:
 Control Unit
 Coordinates the computer’s operations
 Determines where to get the next instruction
 Regulates the other major components of the computer
 Arithmetic and Logic Unit (ALU)
 Designed to perform mathematical operations
MAIN MEMORY
 Main memory holds information that the CPU needs to access
quickly.
 Namely, the instructions to be executed.
 When a program is running, some or all of its instructions are in
main memory.
 Memory is divided into sections called bytes that hold equal
amount of data.
 Each section is made up of 8 bits.
 A Bit is the most basic unit of information a computer can hold. It is a
switch that is either on (1) or off (0)
 Each byte is assigned and can be accessed by its address.
 A Memory Address is a unique identifying number associated with a byte
in memory.
 Main memory typically is volatile.
 Volatile Memory – is memory that when it loses power, the contents are
erased.
SECONDARY STORAGE
 Secondary Storage is memory that can hold data for a long
period of time.
 Programs are usually stored in secondary storage and loaded
into main memory as needed.
 This forms a hierarchy typically called the memory hierarchy.
 Common forms of secondary storage:
 Hard Drive
 Disk Drive
 Solid State Drive
 Removable Storage
 Floppy Disk
 CD-ROM
 USB Drives
 Other files can be stored in secondary storage:
 Documents
 Pictures
 Whatever else you save on your computer
OUTPUT DEVICES

 Output is any data the computer sends to the outside world.


 An Output Device formats data and presents it to the outside
world.
 Common Output Devices:
 Monitor
 Printer
 Disk Drive
 USB Drive
SOFTWARE

 Sof tware refers to the programs that run on a computer.


 Two main categories (for this class):
 Operating System (OS)
 A set of programs that manages a computer’s hardware devices and
controls their processes.
 Most modern operating systems are capable of running multiple programs
at once.
 UNIX, Linux, Mac OS X, and Windows are examples
 Application Software
 Programs that make the computer useful for the user
 Solve specific problems or supply a service
 Word processors, spreadsheets, databases, etc.
 This is what we will be developing in this class.
SYSTEM SOFTWARE

 System sof tware is a collection of programs that


interfaces with the hardware.
 Categories of system sof tware :
APPLICATION SOFTWARE

 Application software is written to enable the


computer to solve a specific data processing task.
 Categories of application software :
PROGRAMMING LANGUAGE

 A programming language is composed of a set of


instructions in a language understandable to the
programmer and recognizable by a computer.

 Programming languages can be classified as


(a) High-level language - BASIC, COBOL &
FORTRAN(application programs).
(b) Middle level language - C (application &
system programs).
(c) Low level language – assembly language
(system programs).
COMPILER
 For executing a program written in a high-level language, it
must be first translated into a form the machine can
understand. This is done by a sof tware called the compiler.
 The compiling process consists of two steps:
a . The analysis of the source program and
b . The synthesis of the object program in the
machine
language of the specified machine.
 Compiler action :
INTERPRETER

 During the process of translation There is another type of


sof tware that also does translation. This is called an
interpreter.
 Dif ferences between compiler and interpreter :
COMPILING & EXECUTING HIGH LEVEL
LANGUAGE

 The compiling process consists of two steps: the analysis of


the source program and the synthesis of the object program in
the machine language of the specified machine.

 The analysis phase uses the precise description of the


source programming language.

 A source language is described using (a) lexical rules,


(b)syntax rules, and (c)semantic rules.
THE PROCESS OF COMPILATION
EXECUTION STEPS OF A
PROGRAM
 Steps :
1. Translation of the program resulting in the object
program.
2. Linking of the translated program with other object
programs needed for execution, thereby resulting in
a binary program.
3. Relocation of the program to execute from the
specific memory area allocated to it.
4. Loading of the program in the memor y for the
purpose of execution .
LINKER

 Linking resolves symbolic references between


object programs. It makes object programs known
to each other.
 Linking makes the addresses of programs known to
each other so that transfer of control from one
subprogram to another or a main program takes
place during execution.
 In FORTRAN/COBOL , all program units are
translated separately.
RELOCATION

 Relocation is more than simply moving a program


from one area to another in the main memory.

 Relocation means adjustment of all address-


dependent locations, such as address constant, to
correspond to the allocated space, which means
simple modification of the object program so that it
can be loaded at an address different from the
location originally specified.
LOADER

 Loading means physically placing the machine


instructions and data into main memory, also
known as primary storage area.
 The functions performed by the loader are :
a. Assignment of load-time storage area to the
program
b. Loading of program into assigned area
c. Relocation of program to execute properly from
its load time storage area
d. Linking of programs with one another
PROGRAM EXECUTION

 When a program is compiled and linked, each


instruction and each item of data is assigned an
address.
 At execution time, the CPU finds instructions and
data from these addresses.
 The program counter, is a CPU register that holds the
address of the next instruction to be executed in a
program.
 The CPU has random access capability to any and all
words of the memory, no matter what their
addresses.
BLOCK DIAGRAM OF PROGRAM EXECUTION
THIRD,FORTH & FIFTH GENERATION
LANGUAGE
 Third generation programming language specifies
how to perform a task using a large number of
procedural instructions and is file oriented.
 Fourth generation programming language specifies
what task has to be performed using fewer
instructions and is database oriented.
 Fifth generation programming language resembles
human speech and eliminates the need for the user
or programmer to learn a specific vocabulary,
grammar ,or syntax.
CLASSIFICATION OF PROGRAMMING
LANGUAGES
STUCTURED PROGRAMMING

 Structured programming involves top–down


analysis for program solving, modularization of
program structure and organizing structured code
for individual module.
 Top-down analysis breaks the whole problem into
smaller logical tasks and defines the hierarchical
link between the tasks.
 Modularization of program structure means making
the small logical tasks into independent program
modules that carries out the desired tasks.
 Structured coding is structured programming which
consists of writing a program that produces a well
organized module.
PROGRAM DEVELOPMENT CYCLE

 Many programmers follow a sequence of Steps to create their


programs.
1. Analyze – Define the Problem
 Make sure that you understand what the program should do. What
should the user be able to enter? How? How does the program
come up with an answer? What does the program output? How?
 User – a person who uses a computer program.
 End User – the user that the program was made for.
2. Design – Plan a Solution for the Problem
 Develop a PRECISE sequence of steps to solve the problem
 An algorithm is a precise sequence of steps to solve a problem.
ALGORITHM

 An algorithm is ‘an effective procedure for solving a


problem in a finite number of steps’.
 A well-designed algorithm has termination and
correctness properties.
 The four common ways of representing an algorithm
are the Step-form, Pseudo-code, Flowchart and
Nassi-Schneiderman .
 algorithms show these three features:
a. Sequence (also known as process)
b. Decision (also known as selection)
c. Repetition (also known as iteration or looping)
VARIABLE & SUBROUTINE

 A variable, which has a name, is a container for a


value that may vary during the execution of the
program.
 A subroutine is a logical collection of instructions
that is invoked from within a larger program to
perform a specific task.
 The subroutine is relatively independent of the
remaining statements of the program that invokes
it & can be invoked several times from several
places during a single execution.
 After completing the specific task, a subroutine
returns to the point of invocation in the larger
program.
PSEUDO CODE & FLOW CHART

 Like step-form, Pseudo-code is a written statement of an


algorithm using a restricted and well-defined vocabulary.
 A flowchart comprises of a set of standard shaped boxes
that are interconnected by flow lines to represent an
algorithm.
 There should be a logical start and stop to the flowchart.
 The usual direction of the flow of a procedure or system
is from left to right or top to bottom.
 The intersection of flow lines should be avoided.
 Flowcharts facilitate communication between
programmers and users.
EXAMPLE: PSEUDO CODE

 Problem:
Write an algorithm to
find out whether a given
number is a prime
number or not.
 Solution:
The algorithm for
checking whether a given
number is a prime
number or not is as
follows:
FLOW CHARTS : SYMBOLIC REPRESENTATION

 The START and STOP are represented by an ellipse-


like figure :
 Decisions construct by the rhombus-like figure :

 The processes by rectangles :

 Input / Output by parallelograms :

 Lines and arrows connect these blocks.


FLOW CHARTS:SYMBOLIC REPRESENTATION
FLOW-CHART ADVANTAGES

 Communication
 Effective analysis
 Proper documentation
 Efficient coding
 Proper debugging
 Efficient program maintenance
FLOW-CHARTS LIMITATIONS

 Complex logic
 Alterations and modifications
 Reproduction
 Loss of objective
FLOWCHART EXAMPLE

 Problem:
Draw a flowchart to
find the roots of a
quadratic equation.
FLOW-CHART EXAMPLE

 Problem:
Prepare a flowchart to
read the marks of a
student and classify them
into different grades. If
the marks secured are
greater than or equal to
90, the student is awarded
Grade A; if they are
greater than or equal to
80 but less than 90, Grade
B is awarded; if they are
greater than or equal to
65 but less than 80, Grade
C is awarded; otherwise
Grade D is awarded.
ALGORITHM : DESIGNING STATEGY

1. Identify the outputs needed.


2. Identify the input variables available.
3. Identify the major decisions and conditions.
4. Identify the processes required to transform inputs
into required outputs.
5. Identify the environment available.
TOP DOWN DEVELOPMENT STEP

 The top-down development phase plans out the way


the solution has to be done by breaking it into
smaller modules and establishing a logical
connection among them.
 Stepwise refinement :
a. Work out each and every detail for each small piece
of manageable solution procedure.
b. Decompose any solution procedure into further
smaller pieces and iterate until the desired level of
detail is achieved.
CONT.

c. Group processes together which have some


commonality.
d. Group variables together which have some
appropriate commonality.
e. Test each small procedure for its detail and
correctness and its interfacing with the other small
procedures.
TRACING AN ALGORITHM TO DEPICT LOGIC

 An algorithm can be traced by verifying every


procedure one by one to determine and confirm the
corresponding result that is to be obtained.
 Example:
CONVERSION

Specification for Converting Algorithms into


Programs:
The general procedure to convert an algorithm into a
program is to code the algorithm using a suitable
programming language, check the program code by
employing the desk-check method and finally
evaluate and modify the program, if needed.
PROGRAM DEVELOPMENT CYCLE

3. Write the Code – Implement a solution


 The instructions in a programming language collectively called
code.
 Your code should be a translation of your algorithm developed into
the programming language.
 In this class we use Java, but there are many other programming
languages: C, C++, C#, Ruby, Python, Visual Basic, etc.
 This is the major focus of this course, but note that you need to be
able to think algorithmically in order to do this.
 Meaning, you need to be able to logically solve the problem in order to
write a program for it.
PROGRAM DEVELOPMENT CYCLE
4. Testing and Debugging – Locate and remove any errors in the
program
 Testing is the process of finding errors in a program
 Debugging is the process of removing errors in a program.
 An error in a program is called a bug.
 We will talk more specifically about the kinds of errors that can occur
in a program once we start programming.
5. Complete All Documentation – Organize the material that
describes the program.
 Documentation is any material whose purpose is to allow another
person or programmer to use or understand the program
 Two kinds of documentation:
1. External Documentation – Material outside of the code files that describe
the program.
2. Internal Documentation – Lines inside of a code file that do nothing except
describe details of the program. In Java, these are called comments.

THANK YOU

You might also like