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

Module1-Introduction

c programming

Uploaded by

Haf hafeefa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Module1-Introduction

c programming

Uploaded by

Haf hafeefa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

BRIDGE COURSE

2024 ADMISSION

C- PROGRAMMING
DAY 1

21-08-2024
COMPUTER
▪Computer :PROGRAMMING
It is a machine which can automatically perform a
computation as instructed to do by a computer program.

▪Computational Problem: A problem whose solution can be obtained


using a computer.

▪Procedure to solve the computational problem – can be

represented by an algorithm/flowchart/Pseudocode.

▪Program : Representation of the procedure in a programming


language.

3
PROGRAMMING
LANGUAGE
▪A language which enable communication between a human

(programmer) and a computer.


▪Programmer : is a person who writes a computer program.

▪Computer program : is a sequence of

instructions/statements in a programming language

▪Example : C, C++, Java, Python …...

▪Example :

Given a number n, find the sum of the digits of n.

Given the instance of problem, n = 45362 – need


to find a solution.

4
PROGRAMMING
▪Process of developing or writing a computer program

to solve a computational problem.

▪Steps in programming

1. Analyze the problem


2. Formulate a
procedure

3. Represent it as an
algorithm/flowchart/p
seudocode
4. Convert this to an
equivalent program in
any programming
language 5
PROGRA
M
▪Computer should be instructed about how to process an instance of a
problem.

▪A finite sequence of statements which instructs computer how it should

process an input to produce a solution is called a program.

▪General practice is to use a semi formal representation of the

procedure as a pre representation of a program.


PRE REPRESENTATION OF A PROGRAM
▪Before developing a program, a programmer typically requires an unambiguous

representation of the procedure to obtain the required solution of an instance of


a computational problem.

▪ Algorithm:

English like description representing a step by step procedure
to solve a computational problem.
▪ Flowchart:


Diagrammatic representation showing a procedure to solve
a problem

▪ Pseudocode:


Short readable and semiformal english like description
ALGORITHM
FLOWCHART, ALGORITHM AND PSEUDO CODE

ALGORITHM
It is a complete step by step representation of the solution of the problem, represented in English
like Languages. An algorithm can be abstract or quite detailed. A detailed algorithm consists of every
step, equivalent to one instruction of a programming language. An algorithm should start with a word
“Start” and end with a word “Stop”.

Examples:
Algorithm to find the area of circle
1. Start
2. Read radius
3. area=3.14*radius*radius
4. Print area
5. Stop
Algorithm to find the sum of two numbers
1. Start
2. Read a, b
3. Sum=a+b
4. Print Sum
5. Stop

Algorithm to find the greatest of three given numbers


6.Start
7.Read three numbers to a,b and c
8.Check whether ‘a’ is greater than ‘b’; if so, goto step 5 (Else control automatically goes to the
step 4)
9.Check whether ‘b’ is greater than ‘c’; if so display ‘b’ as the greatest number and goto step 5 ;
else display ‘c’ as the greatest number and goto step 5
10.Check whether ‘a’ is greater than ‘c’; if so display ‘a’ as the greatest number else display ‘c’ as
the greatest number
11.Stop
Algorithm to find the average of N numbers
1. Start
2. Sum=0, Count=0
3. Read N
4. Read Num
5. Sum=Sum+Num
6. Count=Count+1
7. If Count<N , then goto step 4
8. Average=Sum/N
9. Print Average
10.Stop
Characteristic features of an
algorithm:

Algorithm must have a clearly defined starting point.

Can have one or more ending points.

Minimum one end point must be there.

If the steps are numbered then execution will be carried out in
the

increasing order of step numbers.


If no step numbers are given then execution will be in the
order in which they are arranged.
▪Example 1: Develop an algorithm to find the sum of two numbers taken
as input from user.
▪Input : Two numbers
▪Output: Sum of the numbers

Step 1 : Start
Step 2 : Input first number into A
Step 3 : Input second number into B
Step 4 : Perform addition of A and B and store
result in SUM. Step 5 : Display number in SUM as
solution.

Step 6 : Stop

12
Example 2 : Develop an algorithm to find the area of a
rectangle. Input : Length L, Breadth B

Output: AREA

Step 1 : Start
Step 2 : Input the length into L
Step 3 : Input breadth into B
Step 4 : Perform the multiplication of L and B and store
result in AREA Step 5 : Display number in AREA as solution.

Step 6 : Stop

13
Example 3 : Develop an algorithm to find the area and circumference
of a circle. Example 4 : Develop an algorithm to find the largest of 2
numbers.

Input : Two numbers

A, B Output: Largest

Number

Step 1 : Start
Step 2 : Input first
number into

A
Step 3 : Input second
number into B
14
Step 4 : Compare A and B and if A is greater then goto step 5 else
▪Qn: Develop an algorithm to find the maximum of three numbers input

by the user Step 1 : Start


Step 2 : Input three numbers to A, B and C.
Step 3 : Compare number in A and B and if A is greater than B perform step 4
else perform step 7
Step 4 : Compare A and C and if A is greater then goto step 5 else goto

step 6

Step 5 : Display A as solution and go to

step 10 Step 6 : Display C as solution

and go to step 10

Step 7 : Compare B and C and if B is greater then goto step 8

else goto step 9

Step 8 : Display B as solution and go to


1
step 10 Step 9 : Display C as solution 2
Example 5 : Develop an algorithm to find the remainder of
a division. Example 6 : Develop an algorithm to swap two
numbers.
Example 7 : Develop an algorithm to check if the number
is odd or even

Home work

Input : a, b, c
Output : Nature of roots, roots of QE

Algorithm for finding the roots of a quadratic


equation ax^2+bx+c=0.
Qn: To find the Sum of 5
numbers. Input : 5 numbers
Output : Sum of 5
numbers Method-1
{post-checking}
Step 1 : Start
Step 2 : Let sum = 0,
count = 0

Step 3 : Input number

to A Step 4 : Let sum

= sum + A Step 5 : Let

count = count +1

Step 6 : If count < 5 then goto

step 3 Step 7 : Print sum 17


Qn: To find the Sum of 5
numbers. Input : 5 numbers
Output : Sum of 5
numbers Method-2
{pre-checking} Step
1 : Start

Step 2 : Let sum = 0,


count = 0
Step 3 : If count = 5
then goto step 8
Step 4 : Input number
to A Step 5 : Let sum =
sum + A Step 6 : Let
count = count +1 Step
7 : Go to step 3
Step 8 : Print 18
Qn: Find the Average of N integer
numbers Input : value of N, N
numbers

Output : Avaerage of N numbers

Step 1 : Start
Step 2 : Let sum = 0, count = 0

Step 3 : Input value of N

Step 4 : Input number

to A Step 5 : Let sum =

sum + A Step 6 : Let

count = count +1

Step 7 : If count < N Go to

step
Step 410Step
: 8 : Average = 1
6
Qn: Read and display a sequence of N integer
numbers Input : value of N, N numbers
Output : Display the
numbers Step 1 : Start

Step 2 : Let i =1
Step 3 : Input value of N

Step 4 : Input number

to A[i] Step 5 : i= i+1

Step 6 : If i <= N Go to

step 4 Step 7 : Let i =1

Step 8 : Print

A[i] Step 9 : i

=i+1
1
Step 11 :: If
10 7
Qn: Average of N integer
numbers Input : value of N, Output : Display the
N numbers Average
Step 1 : Start
Step 2 : Let i =1, sum =0
Step 3 : Input value of N
Step 4 : Input number to
S[i] Step 5 : i= i+1
Step 6 : If i <= N Go to step
4 Step 7 : i= 1
Step 8 : Let sum = sum +
S[i]
StepStep
10 : If9 i: <
Let
= iN=Go
i +1
to step
8 Step 11 : Average = sum/N
Step 12 : Print Average

Step 13 : 6
Stop 2
Qn:Find the largest of N numbers.
Input : value of N, the N numbers
Output : Largest number

Step 1 : Start
Step 2 : Let count =0
Step 3 : Input N
Step 4 : Input Number
to A Step 5 : Let
Large = A
Step 6 : If count = N-1 then goto
step
Step 10
8 : Step
If A >7large
: Input Number to
then
Large
A = A Step 9 : Let goto step
count = count + 1 6
Step 10 : Print large
Step 11 :
Stop
22
Qn:Find N! { N! =
1*2*3*........*(N-1)*N }
Input : value of N
Output : N! (or factorial of N)

Step 1 : Start
Step 2 : Let count =1, factn = 1

Step 3 : Input N

Step 4 : If count > N then goto

step 8 Step 5 : Let factn =

factn * count Step 6 : Let

count = count + 1

Step 7 : goto

step 4 Step 8 :
23
Qn 1: Write an algorithm to check whether a given number is positive
or negative.

Qn 2: W rite an algorithm to find factors of a number.


Qn:To check whether a counting number is prime or not.
{ The number is prime if there is no factors between 2 and
sqrt(number)}
inclusive) (both

Input : A counting number


Output : A message showing that number is PRIME or NOT
Step 1 :
Start
Step 2 : Let count =1, factor = 2 Step 3
: Input Number
Step 4 : If factor > sqrt(Number) then goto step
9 Step 5 : Let R = Remainder (Number/factor)
Step 6 : If R=0 then Print “NOT PRIME”; goto step 10
Step 7 : Let factor = factor + 1
Step 8 : goto step 4
Step 9 : Print “PRIME”
Step 10 : 6
Stop 6
Qn: Given a sequence S of N elements and a KEY, find the index of KEY if present
in S

Input : value of N, the N numbers, KEY


Output : Display the index of KEY if present in S, else display Element Not Found
Step 1 : Start

Step 2 : Input the number of elements to N


Step 3 : Input the number to be searched in KEY Step 4 :
i=1
Step 5 : Input the number to
S[i] Step 6 : i = i + 1

Step 7 : If i <= N goto step 5

26
Qn: Given a sequence S of N elements and a KEY, find the index of KEY if
present in S

Input : value of N, the N numbers, KEY


Output : Display the index of KEY if present in S, else display Element Not
Found Step 8
:
i=1

Step 9 : if S[i] = KEY then goto


Step If i <= N then goto Step 9
11 : 13 Step
step Print10
“KEY
: inot
= i in
+ the
1 sequence” goto step 14
Step Step
Print “ KEY 13 :
is present in the
12 :
index i ”

Step 14 : Stop

27
28
FLOWCHART
 A flowchart is a diagram that visually represents the steps involved in solving a problem or
carrying out a process. It depicts the possible ways along which the control can flow.
 A flowchart can also be defined as a diagrammatic representation of an algorithm.
Flowchart uses many graphical symbols and thus, is more understandable.
The symbol used for different types of statements are as shown.
PSEUDO CODE
 It is a more formal representation than the algorithm. Here, we represent
every step in a formal way which is very close to the actual programming
language representation.
 In pseudocode, each of the steps will be written via operator and
statements equivalent to some programming language instructions.
 The only difference will be that the exact syntax of the programming
language will not be followed.
 All pseudocodes will start with the keyword “START” and complete with
keyword “STOP” or “END”.

Example: Pseudocode to find the area of circle


1. START
2.Read radius
3. area = 3.14 * radius * radius
4. print area
5. STOP
BASICS OF COMPUTER HARDWARE AND SOFTWARE
Computer is an advanced electronic device that takes raw data as input from the user
and processes these data under the control of a set of instructions (called program stored in memory) and
gives the result (output) and saves output for the future use.

Computer Architecture
The basic components of a modern digital computer are: Input Device, Output
Device, Central Processing Unit (CPU), and memory.
Central Processing Unit (CPU)
 CPU is the brain of the computer system.
 All major calculation and comparisons are made inside the CPU and it is
also responsible for activation and controlling the operation of other units.
 This unit consists of two major components that are arithmetic logic unit (ALU) and
control unit (CU).

 Arithmetic Logic Unit (ALU)


Arithmetic logic unit performs all arithmetic operations such as addition, subtraction,
multiplication and division. It also uses logic operation for comparison.

 Control Unit (CU)


 The control unit of a CPU controls the entire operation of the computer.
 It also controls all devices such as memory, input/output devices connected to the
CPU.
 Additionally, CPU also has a set of registers for temporary storage of data, instructions,
addresses and intermediate results of calculation.
 When the ALU, CU and the registers are all integrated on a single chip, it is called a
Microprocessor, thereby capturing the entire CPU on a single chip.

Input /Output Unit


 The input/output unit consists of devices used to transmit information between the external
world and computer memory.
 The information fed through the input unit is stored in computer's memory for processing
and the final result stored in memory can be recorded or displayed on the output medium.
Examples for input devices:
Mouse, Keyboard, Scanner, Joy Stick, Microphone, OCR (Optical Character
Reader), MICR (Magnetic Ink Character Reader) Examples for output devices:
Printer (Dot Matrix, Ink Jet, Laser, Line), Monitor
Memory Unit

 The Memory unit refers to the area where instructions and data are stored in a
computer.

 The processor reads instructions and data from the memory, executes them and
writes the result back to it.

 Different forms of memory are used in a computer system.

 They vary in their size, speed and location.

 Anything and everything in a computer is stored in the form of bits known


as binary language or machine language.

 8 bits make up a byte.

 The total number of bytes that a memory chip can hold at a time is termed as its
size or capacity.

 Information is stored and processed as a group of bytes, called computer word.


The word length is specific for each processor.
The Registers –
 Since the group of registers is in-built within the processor chip, they are the
fastest accessible units of memory by the processor. They are highly expensive and hence are
limited in number.
 Some registers are used to store data while some are reserved to store address. There are some
general purpose registers as well. Size of the registers depends on the processor.
Accumulator
It is a register found on all processors, which is mainly used to store the operand of an arithmetic
operation.
Program counter (PC)
Always stores the address of the next instruction to be executed by the processor.
The Cache Memory: -
 It is a small piece of high speed volatile memory located closer to the processor for fast
processing. Cache memory is made of high speed SRAMs.
 It acts as a buffer between the CPU and the RAM. It holds frequently accessed data and
instructions so that they can be easily supplied to the CPU when requested again. Cache memory
Primary Memory: -

 The memory chips that the processor can directly access are referred to as
primary memory. The three types of primary memory are RAM, ROM and CMOS. RAM is
volatile whereas ROM as CMOS are non-volatile memories. They are metal oxide semiconductor
memory cells built on silicon based ICs.

Random Access Memory (RAM):

 RAM is a volatile memory and loses its contents when the power is turned off. The circuit is so
designed that any random location can be directly accessed without the need to scroll up or down
the memory.

 The program to be executed is loaded into RAM from the non-volatile backup memory.

 The processor reads instructions and data from the RAM, executes them and writes data back to
the RAM. Hence RAM is also known as the main memory of the computer.

 Everything from the RAM is copied to the non-volatile backup memory before the computer is turned
off. The two main types of RAM are Dynamic Ram (DRAM) and static RAM (SRAM).
Read Only Memory (ROM):

 ROM is a non-volatile memory and hence retains its contents even when the computer is
switched off.

 It is used for storing software that is rarely changed during the life of the system, sometimes
known as firmware. Almost every computer comes with a small amount of ROM containing the
boot firmware which is essential for the boot-up.

 ROM (Read Only Memory)

 PROM (Programmable Read Only Memory)

 EPROM (Erasable Programmable Read Only Memory).

 EEPROM (Electrically Erasable Programmable Read Only Memory)


HARDWARE
 Hardware consists of the mechanical parts that make up the computer as a machine.
 The hardware consists of physical devices of the computer. The devices are required for input, output,
storage and processing of the data.
Keyboard, monitor, hard disk drive, floppy disk drive, printer, processor and motherboard are some of the
hardware devices.

SOFTWARE
 Software is a set of instructions that tells the computer about the tasks to be performed and how these
tasks are to be performed.
 Program is a set of instructions, written in a language understood by the computer, to perform a
specific task.
 A set of programs and documents are collectively called software.
Software is classified into two categories
 System Software
 Application Software
SYSTEM SOFTWARE AND APPLICATION SOFTWARE

SYSTEM SOFTWARE

 System Software is the type of software which is the interface between application software and
computer hardware.

 Low level languages are used to write the system software.

 System Software maintains the system resources and give the path for application software to run.
An important thing is that without system software, system cannot run. It is a general purpose
software.

Example: Operating System

Operating System

 OS is a system software which acts as an interface between the user of a computer and computer
hardware. It provides an environment within which the user can execute his programs.

Examples: Microsoft Disk Operating System (MS-DOS), Windows 7, Windows XP, Linux, UNIX,
and Mac OS X Snow Leopard.
APPLICATION SOFTWARE

 The software that a user uses for accomplishing a specific task is the application software.

 Application software may be a single program or a set of programs.

 It runs on the platform which is provided by system software. High level languages are used
to write the application software. It is a specific purpose software.
Examples
 Word Processing Software: For writing letter, reports, documents etc. (e.g. MS- WORD).
 Image Processing Software: For assisting in drawing and manipulating graphics (e.g. Adobe
Photoshop).
 The main difference between System Software and Application Software is that without system
software, system cannot run on the other hand without application software, system always runs.
SL. NO SYSTEM SOFTWARE APPLICATION SOFTWARE

System Software maintains the


1 system resources and give the path for Application software is built for
application software to specific tasks.
run

Low level languages are used to High level languages are used to
2
write the system software. write the application software.
3 Machine Dependent Machine independent

4 It is a general-purpose software. It is a specific purpose software.

Withoutsystemsoftware, Without application software


5 system can’trun. system always runs.

System software runs when


system is turned on and stop when While application software runsas per the
6 system is turnedoff. user’s request.

Compiler,OperatingSystem, Photoshop, Microsoft Office


7 Interpreter
TYPES OF LANGUAGES
Computer programs can be written in high and low level languages, depending on the task and the hardware being
used.

LOW LEVEL LANGUAGE (MACHINE LANGUAGE)


A program written in machine language is a collection of binary digits or bits ( 0 and 1) that the computer reads
and interprets.
Features
 The computer can understand the programs written in machine language directly. No translation of the program is
needed.
 Program written in machine language can be executed very fast (Since no translation is required).
 Machine language is defined by the hardware of a computer. It depends on the type of the processor or processor
family that the computer uses, and is thus machine-dependent.
 A machine- level program written on one computer may not work on another computer with a different processor.
 It is difficult to write a program in machine language as it has to be written in binary code. Such programs are also
difficult to modify
 Error occurring chance is high
ASSEMBLY LANGUAGE
 A program written in assembly language uses symbolic representation of machine codes needed to program a particular
processor (CPU) or processor family.
 This representation is usually defined by the CPU manufacturer, and is based on abbreviations (called mnemonics) that
help the programmer remember individual instructions, registers, etc. Small, English-like representation is used to write
the program in assembly language.
Features
 Assembly language programs are easier to write than the machine language programs, since assembly language
programs use short, English-like representation of machine code.
For example
ADD 2, 3
LOAD A
SUB A, B
 The program written in assembly language is the source code, which has to be converted into machine code, also
called object code, using translator software, namely, assembler.
 Each line of the assembly language program is converted into one or more lines of machine code. Hence
assembly language programs are also machine-dependent.
HIGH LEVEL LANGUAGE
High level languages are written in a form that is close to human language, enabling the
programmer to just focus on the problem being solved.
Features

 Programs are easier to write, read or understand in high-level languages


 Programs written in high-level languages is the source code which is converted into the object
code (machine code) using translator software like interpreter or compiler.
 A line of code in high-level program may correspond to more than one line of machine code.
 Programs written in high-level languages are easily portable from one computer to another.
Example: C, C++, Java, python
SYSTEM TRANSLATORS

 A translator is a programming language processor that converts a computer program from one
language to another. I

 t takes a program written in source code and converts it into machine code. It discovers and
identifies the error during translation.

There are 3 different types of translators as follows:

1. COMPILER

2. INTERPRETER

3. ASSEMBLER
 COMPILER
A compiler is a translator used to convert high-level language to machine language.
Eg: gcc, javac, g++.
 INTERPRETER
Interpreter is a translator used to convert high-level programming language to low-level programming
language.
Example: Python, jvm(Java Virtual Machine)
 ASSEMBLER
An assembler is a translator used to translate assembly language to machine language.
Example: Fortran Assembly Program (FAP), Macro Assembly Program (MAP).
BASIS FOR
COMPILER INTERPRETER
COMPARISON
Input It takes an entire program at a time. It takes a single line of code or
instruction at a time.

Output It generates intermediate objectcode. It does not produce any


intermediateobject code.

Working mechani The compilation is done Compilationandexecution takeplace


sm beforeexecution. simultaneously.

Speed Comparatively faster Slower

Errors Display all errors after compilation, all at Displays error of each line one by one.
the same time.

Error detection Difficult Easier comparatively

Example Gcc, g++, javac Python, jvm


STRUCTURED APPROACH TO PROGRAMMING
 Structured Programming Approach can be defined as a programming approach in which the
program is made as a single structure.
 It means that the code will be executed instruction by instruction one after the other.
 It doesn’t support the possibility of jumping from one instruction to some other with the help of any
statement like GOTO, etc. Therefore, the instructions in this approach will be executed in a
serial and structured manner.
 The languages that support structured programming approach are:
 C
 C++
 java
Advantages of Structured Programming Approach
1. Easier to read and understand
2. Easier to Maintain
3. Easier to Debug
4. Machine-Independent, mostly.

You might also like