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

Lab Manual (20EST 102-Programming in C)

The document provides information about the hardware components of a computer system based on the Von Neumann architecture. It describes the central processing unit including the control unit, arithmetic logic unit, and registers. It also discusses the memory unit and input/output interfaces. Example input devices include the keyboard, mouse, joystick, scanner, and microphone. Common output devices are the monitor, printers, speakers, headphones, and projector. The primary memory or main memory temporarily stores data and instructions being processed. Secondary storage such as hard disks provide permanent storage.

Uploaded by

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

Lab Manual (20EST 102-Programming in C)

The document provides information about the hardware components of a computer system based on the Von Neumann architecture. It describes the central processing unit including the control unit, arithmetic logic unit, and registers. It also discusses the memory unit and input/output interfaces. Example input devices include the keyboard, mouse, joystick, scanner, and microphone. Common output devices are the monitor, printers, speakers, headphones, and projector. The primary memory or main memory temporarily stores data and instructions being processed. Secondary storage such as hard disks provide permanent storage.

Uploaded by

ncjnoahzoz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Saintgits College of Engineering

(Autonomous)

LAB MANUAL

for

Programming in C
(20EST 102)
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

INDEX

1. FAMILIARIZATION OF HARDWARE COMPONENTS OF A COMPUTER ........... 2


2. FAMILIARIZATION OF LINUX ENVIRONMENT ................................................ 8
3. FAMILIARIZATION OF CONSOLE I/O AND OPERATORS IN C ......................14
4. LARGEST OF 3 NUMBERS…………………………………………………..................18
5. PRIME OR NOT ...............................................................................................20
6. ARMSTRONG OR NOT.....................................................................................22
7. SUM AND AVERAGE OF THE NUMBERS STORED IN AN ARRAY……………..24
8. LINEAR SEARCH .............................................................................................26
9. BUBBLE SORT .................................................................................................29
10. READ A STRING AND CHECK WHETHER IT'S A PALINDROME OR NOT ..31
11.READ TWO STRINGS AND CONCATENATE THEM WITHOUT USING
LIBRARY FUNCTIONS ...................................................................................33
12.READ A STRING AND COUNT THE NUMBER OF VOWELS, CONSONANTS
AND SPACES IN IT ........................................................................................35
13. STRUCTURES……………………………………………………………………………….38
14. ARRAY OF STRUCTURES .............................................................................40
15. UNION ............................................................................................................43
16. RECURSIVE AND NON-RECURSIVE FACTORIAL…………………………………45
17. REVERSING THE STRING.............................................................................48
18. MATRIX OPERATIONS (MENU DRIVEN).......................................................50
19. ADD AND SWAP OPERATIONS USING POINTERS ........................................60
20. INPUT AND PRINT THE ELEMENTS OF AN ARRAY .....................................63
21. SUM OF THE ELEMENTS STORED IN AN ARRAY USING POINTERS AND
USER DEFINED FUNCTION...........................................................................65
22. FILES .............................................................................................................67
23. COUNT NUMBER OF CHARACTERS, WORDS AND LINES IN A FILE AND
STORE THE RESULTS IN AN OUTPUT FILE ................................................70

Page 1 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

Expt No : 1

FAMILIARIZATION OF HARDWARE COMPONENTS OF A


COMPUTER
Aim:

To familiarize with various hardware components of a computer


system.

Von Neumann Architecture

The modern computers are based on a stored-program concept introduced


by John bon Neumann. In this concept, programs and data are stored in a
separate storage unit called memories and are treated the same. This novel
idea meant that a computer built with this architecture would be much
easier to reprogram.

Central Processing unit


Control Unit (CU)

Input Devices Arithmetic Logic Output Devices


Unit(ALU)

Memory Unit

Central Processing Unit (CPU)

CPU can be considered as the heart of the computer system, includes


three main components:
i) Control Unit (CU) determines the order in which instructions
should be executed and controls the retrieval of the proper
operands. It interprets the instructions and the execution of
each instruction is determined by a sequence of control signals
produced by the control unit.
ii) Arithmetic logic unit (ALU) is a digital circuit used to perform
arithmetic and logic operations. It represents the fundamental
building block of the central processing unit (CPU) of a
computer. Most of the operations of a CPU are performed by one
Page 2 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

or more ALUs, which load data from input registers. A register is


a small amount of storage available as part of a CPU. The
control unit tells the ALU what operation to perform on that
data and the ALU stores the result in an output register. The
control unit moves the data between these registers, the ALU,
and memory.
iii) Registers are temporary storage locations to quickly store and
transfer the data and instructions being used. Because the
registers are on the same chip and directly connected to the
control unit, the registers have faster access time than other
memories. Therefore, using registers both as the source operand
and destination operand for storing results will improve the
performance.
iv) Memory is used to store program instructions and data. A
memory unit is a collection of storage cells together with
associated circuits needed to transfer information in and out of
the storage.

The I/O Interfaces allow the computer’s memory to receive information and
send data to output devices. They allow the computer to communicate to the
user and to secondary storage devices like disk and tape drives.

Input Devices

i) Keyboard is the most common and very popular input device


which helps to input data to the computer. The design of the
keyboard is like that of a traditional typewriter, although there are
some additional keys provided for performing some additional
functions
ii) Mouse is the most popular pointing device. It is a cursor control
device having a small palm size box with a round ball at its base,
which senses the movement of the mouse and sends corresponding
signals to the CPU, when the mouse buttons are pressed.
iii) Joystick is also a pointing device which is used to move the cursor
position on a monitor screen. It is a stick having a spherical ball at
its both lower and upper ends. The lower spherical ball moves in a
socket. The joystick can be moved in all four directions.
iv) Scanner is an input device, which works like a photocopy
machine. It is used when some information is available on paper
and it is to be transferred to the hard disk of the computer for
further manipulation. Scanner captures images from the source
which are then converted into a digital form that can be stored on
the disk. These images can be edited, if required.

Page 3 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

v) Microphone is an input device to input sound that is then stored


in a digital form. The microphone is used for various applications
such as adding sound to a multimedia presentation or for mixing
music.

Output Devices

i) Monitor is the most common output device used with computers,


which displays video images and text. A monitor is made up
of circuitry, a screen, a power supply, buttons to adjust screen
settings, and casing that holds all of these components.
ii) Printers are another common output device found in homes and
offices. In computing terms, they take electronic data stored on a
computer and generates a hardcopy of it. There are different types
of printer, with Ink jet and Laser printers being two of the most
common types.
iii) Computer Speakers are hardware devices that transform the
signal from the computers sound card in to audio. Speakers create
sound using internal amplifiers that vibrate at different frequencies
according to data from the computer. This produces sound.
Speakers are essential if you want a louder sound, surround
sound, fuller bass or just higher quality audio.
iv) Headphones also known as earphones, allow you to listen to audio
without disrupting other people in the vanity. They connect via
computer line out or to the speakers.
v) Projector, as its name suggests, this output device projects
computer images onto a wall or a screen. Projectors are typically
used for presentations, watching movies or as a teaching aid, as
they enable an entire roomful of people to see images generated by
a single computer.

Memory

Memory is the brain of a computer which stores data and information and
also for retrieving it.

i) Primary memory, also known as main memory is the area in a


computer which stores data and information for fast access. It is a
volatile memory, meaning the data is stored temporarily and is
liable to change or lose in case of power failure. The two main types
of primary storage are Random Access Memory (RAM) and Read
Only Memory (ROM).

RAM (Random Access Memory) ROM (Read Only Memory)

Page 4 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

Data is volatile. Data is present Data is permanent. Data remains


till power supply is present. even after power supply is not
present.

Data can be read, erased or Data is read only.


modified.
Used to store data that CPU needs Used to store data that in needed
for current instruction processing. to bootstrap the computer.

Speed is quite high. Speed is slower than RAM.

CPU can access data stored on Data to be copied from ROM to


RAM. RAM so that CPU can access its
data.

RAM memory is large and high ROM is generally small and of low
capacity. capacity.

Used as CPU Cache, Primary Used as firmware by


Memory. microcontrollers.

Costly Cheap

ii) Secondary memory is computer memory that is non-volatile and


persistent in nature and is not directly accessed by a
computer/processor. Primary memory has limited storage capacity
and is volatile. Secondary memory overcome this limitation by
providing permanent storage of data in bulk quantity. Secondary
memory is also termed as external memory and refers to the various
storage media on which a computer can store data and programs.
Some o the secondary memory devices are:
a) Hard disk drive, the data is stored by magnetizing or
demagnetizing the magnetic coating. A magnetic reader arm is
used to read data from and write the data to the disks.
b) CD Drive, CD stands for compact disk. CDs are circular disks that
use optical rays; usually lasers, to read and write data. CDs are
inserted in CD drives built into CPU cabinet.
c) DVD Drive, DVD stands for Digital video display. DVDs are optical
devices tat can store 15 times the data held by CDs. They are
usually used to store rich multimedia files that need high storage
capacity. They also come in 3 varieties - read only, recordable,
rewritable.

Page 5 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

d) Pen drive is a portable memory device that uses solid state


memory rather than magnetic fields or lasers to record data. It is
also called USB drive.
e) Blu ray disk, BD is an optical storage media used to store high
definition (HD) video and other multimedia field. It uses shorter
wavelength laser as compared to CD/DVD.
iii) CPU Cache is a hardware used by the CPU of a computer to reduce
the average cost (time and energy) to access data from the main
memory. A cache is a smaller, faster memory, located closer to a
processor core. CPU Cache is a hardware which is divided into
three main levels: L1, L2 & L3
a) L1 (Level1) Cache is the fastest memory that is present in a
computer system. In terms of priority of access, L1 cache has
the data the CPU is most likely to need while completing a
certain task. The L1 cache is usually split into two sections:
the instruction cache and the data cache. The instruction
cache deals with the information about the operation that
the CPU must perform, while the data cache holds the data
on which the operation is to be performed.

b) L2 (Level 2) cache is slower than the L1 cache but bigger in


size. Where an L1 cache may measure in kilobytes, modern
L2 memory caches measure in megabytes. The L2 cache size
varies depending on the CPU, but its size is typically between
256KB to 8MB. When it comes to speed, the L2 cache lags
behind the L1 cache but is still much faster than your
system RAM.

c) L3 (Level 3) cache is the largest but also the slowest cache


memory unit. Modern CPUs include the L3 cache on the CPU
itself. With multicore processors, each core can have
dedicated L1 and L2 cache, but they can share an L3 cache.
If an L3 cache references an instruction, it is usually
elevated to a higher level of cache.

Motherboard and its components

Motherboard is the heart of a computer system. It is called motherboard


because all features and peripherals plug to operate the whole system are
into it. The main components od motherboard are:

i) Back Panel Connectors & Ports: Connectors and ports for


connecting the computer to external devices such as display ports,
audio ports, USB ports, Ethernet ports etc.
Page 6 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

ii) PCI (Peripheral Component Interconnect) Slots: Slot for older


expansion cards such as sound cards, network cards, connector
cards.
iii) PCI Express x1 Slots: Slot for modern expansion cards such as
sound cards, network cards (Wi-Fi, Ethernet, Bluetooth etc.), and
connector cards (USB) etc.
iv) PCI Express x16 Slot: Slot for discrete graphic cards and high
bandwidth devices.
v) North Bridge: It also known as Memory Controller Hub (MCH). One of
the most important component of a motherboard. Northbridge allows
the CPU to communicate with the RAM and graphics card. It is
responsible for co-coordinating the data flow amongst the memory,
video card and the processor.
vi) South bridge: Also known as the Input/output Controller Hub (ICH).
It is responsible for coordinating the data flow between the processor
and peripherals such as sound cards and network cards
vii) CPU Socket: Socket for inserting CPU. The processor socket is the
central piece of a motherboard, usually being located near the centre
of the motherboard.
viii) Power Connector (processor power supply connector): Connects
to the 4-pin power cable of a power supply unit which supplies power
to the CPU
ix) Front Panel USB 2.0/3.0 Connector: Connects to USB 2.0/3.0 ports
at the front or top of the computer case.
x) Front Panel Connectors: Connects to the power switch, reset switch,
power LED, hard drive LED and front audio ports of a computer case.
xi) CMOS Battery (Complementary Metal Oxide Semiconductor): It
supplies power to store BIOS settings and keep the real time clock
running. The CMOS battery found on most motherboards is the
lithium coin cell.
xii) RAM/Memory Slots: Slots to inserts RAM. The memory slots are
used to house the computer’s memory modules.
xiii) SMPS (Switched Mode Power Supply : It is an electronic power
supply that incorporates a switching regulator to convert electrical
power efficiently. Like other power supplies, an SMPS transfers power
from a DC or AC source to DC loads, such as a personal computer,
while converting voltage and current characteristics.
Result:
Familiarization of hardware components of a computer has been carried out.

Page 7 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

Expt. No: 2

FAMILIARIZATION OF LINUX ENVIRONMENT


PROGRAMMING IN C WITH LINUX

Aim:

To familiarize Linux environment and do programming in c with Linux


operating system.

Familiarization of Linux Environment


Linux Distributions:

Linux File System Hierarchy

Page 8 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

Basic Linux Commands

pwd
pwd (print working directory) command displays the full pathname of the
current working directory

ls
This command lists the files and folders of a directory.

cd
cd (change directory) command changes the current working directory to
another. You can also use full paths to folders or simply the name of a folder
within the directory you are currently working.
cd <folder name>

cd <full path>

cd ..
change directory one level up.

cd /
Change directory to the root directory

Cd ~
Change to home directory

cp
Copy files or folders

Page 9 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

mkdir
Make a directory

rm
Remove a directory or files

rm –r
remove a directory with its contents recursively

rmdir
remove an empty directory

man
man command displays a manual page of commands

gedit
Gedit is a commonly used text editor. If gedit is installed, gedit command
will open the text editor application.

Page 10 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

mv
Move or rename command

Rename example

Move example

sudo
sudo (SuperUser DO) Linux command allows you to run programs or other
commands with administrative privileges. This is useful when, for example,
you need to modify files in a directory that your user wouldn’t normally have
access to.

apt-get
apt-get is used to install, update, upgrade and remove any package. apt-get
basically works on a database of available packages. The list of different apt-
get commands are

sudo apt-get install


This command is used for installing a package

Page 11 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

sudo apt-get update


apt-get update with super user privileges is the first command you need to
run in any Linux system after a fresh install. This command updates the
database and let your system know if there are newer packages available or
not.

sudo apt-get upgrade


After updating the package database, next step is to upgrade the installed
packages. For upgrading all the packages with available updates you can
use this command.

sudo apt-get remove


This command is used for removing an installed program

Programming in C with Linux Operating System

Step 1: Open a text editor like gedit, make a file with .c extension, type the
program and save in a folder.

Page 12 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

Step 2: Compile the program using gcc compiler for C and make an
executable file with .out extension with the help of command
gcc <source code filename> -o <output filename>

Step 3: Run the executable code using the following command and verify the
output.
./<output filename>

Result:
Linux environment was familiarized and practiced programming in c with
Linux operating system.

Page 13 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

Expt No: 3

FAMILIARIZATION OF CONSOLE I/O AND OPERATORS IN C


Aim:

To implement C programs to i) Display “Hello World” ii) Read two


numbers, add them and display their sum iii) Read the radius of a circle,
calculate its area and display it iv) Evaluate the arithmetic expression ((a -b
/ c * d + e) * (f +g)) and display its solution. Read the values of the variables
from the user through console.

Algorithm:

i) Step 1: Start
Step 2: Display “Hello World”
Step 3: Stop
ii) Step 1: Start
Step 2: Read two numbers, num1, num2
Step 3: sum = num1 + num2
Step 4: Display sum
Step 5: Stop
iii) Step 1: Start
Step 2: Read a number, radius
Step 3: area = 3.14 x radius x radius
Step 4: Display area
Step 5: Stop
iv) Step 1: Start
Step 2: Read the values of variables a, b, c, d, e, f and g
Step 3: result = ((a -b / c * d + e) * (f +g))
Step 4: Display result
Step 5: Stop

Program - I:

//Program to print "Hello World"


#include <stdio.h>
void main()
{
printf("Hello World");
}

Page 14 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

Sample Output:

Program - II:

//Program to find the sum of two numbers

#include<stdio.h>

void main()

float num1, num2, sum;

printf("Enter two numbers:");

scanf("%f%f", &num1, &num2);

sum = num1 + num2;

printf("\n%.2f + %.2f = %.2f", num1, num2, sum);

Sample Output:

Page 15 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

Program - III:

//Program to find the area of a circle

#include<stdio.h>

#include<math.h>

void main()

float radius, area;

printf("Enter the radius of circle:");

scanf("%f", &radius);

area = 3.1428 * radius * radius;

printf("Area = %.2f", area);

Sample Output:

Program - IV:

//Program to evaluate the expression ((a - b / c * d + e) * (f + g))

#include <stdio.h>

void main()

Page 16 of 72
SAINTGITS COLLEGE OF ENGINEERING
(AUTONOMOUS)

float a, b, c, d, e, f, g, result;

printf("Enter the values a b c d e f in order: ");

scanf("%f%f%f%f%f%f%f", &a, &b, &c, &d, &e, &f, &g);

result = ((a-b/c*d+e)*(f+g));

printf("\nResult = %.2f",result);

Result:

The programs to i) Display “Hello World” ii) Read two numbers, add them
and display their sum iii) Read the radius of a circle, calculate its area and
display it iv) Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g)) and
display its solution are executed and outputs are verified.

Page 17 of 72

You might also like