Fundamentals of Computer
Fundamentals of Computer
LABORATORY MANUAL
COMPUTER PRACTICE LABORATORY-II
II SEMESTER
2008-2009
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00
APPROVAL
PREPARED BY APPROVED BY
1. Ms.S.Sudha. Mr.M.Babu.
2. Mr.M.Karthikeyan. HOD/CSE.
3. Mr.P.Selvakumar.
4. Mr.E.Senthilkumaran.
5. Mr.M.Manickam.
6. Mr.N.Kalyanasundaram.
7. Mr.Amit Prasad.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00
INDEX-I
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00
I N D E X - II
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00
WHAT IS UNIX
UNIX is an operating system which was developed first in the
1960’s and has under constant development ever since by operating
system we mean the suite of programs which make the computer
work. It is a stable, multi-user, multi-tasking operating system for
servers, desktops and laptops.
UNIX operating system also have graphical user interface (GUI)
similar to Microsoft windows operating system which provides an
easy to environment. However knowledge of unix is required for
operations which are not covered by graphical program or for when
there is no windows interface available.
TYPES OF UNIX
There are many different versions of unix, although they
share the common similarities. The most popular varieties of unix
are Sun Solaris, GNU/Linux and Macos X.
THE UNIX OPERATING SYSTEM
The Unix Operating system is made up of three parts
1. The Kernel
2. The Shell
3. The Programs
THE KERNEL
The Kernel of Unix is the hub of the operating system. It
allocates time and memory to programs and handles the file store
and communication in response to system calls.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 1 OF 51
As an illustration of the way that the shell and the kernel work
together, suppose a user types ‘rm myfile’ (which has the effect of
removing the file myfile). The shell scratches the filestore for the file
containing the program ‘rm’. And then requests the kernel, through
system calls to ‘rm myfile’ finished running, the shell then returns
the unix prompt ‘%’ to the user, indicating execute the program ‘rm’
on ‘myfile’. When the process that it is waiting for future commands.
THE SHELL
The shell acts as an interface between the user and the
kernel. When user login, the login program checks the username and
password, and then starts another program called the shell. The
shell is a command line interpreter (CLI). It interprets the commands
the user types in an arranges for them to be carried out. The
commands are themselves programs. When they terminate, the shell
gives the user another prompt (% on our system). By typing part of
the name of a command, filename and directory and pressing the
[tab] key, the shell will complete the rest of them automatically. If
shell finds more than one name beginning with those letters you
have typed, it will beep, prompting to type a few more letters before
pressing the tab key again.
The shell keeps the list of the commands you have typed in, if you
need to repeat a command, use the cursor key to scroll up and down
the list or type history for a list of previous commands. Different
users may use different shells. Initially, your system administrator
will supply a default shell, which can be overridden or changed.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 2 OF 51
• editing
• file maintenance
• printing
• sorting
• programming support
• online info
The Bourne shell is one of the oldest shells and is the most
efficient for background work. However it provides few facilities for
interactive users.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 3 OF 51
The C shell is the default shell for interactive work on many UNIX
systems. It will be covered in this document, although most of the
basic commands given here are relatively standard across all the
main shells.
The Korn shell was written by David Korn from AT&T and in it he
attempted to merge the preferred features of both the Bourne and C
shells as well as adding some additional features.
Unfortunately the Korn shell was not available for free, as other
UNIX shells were, so many users and companies did not chose to
use of it.
The Bash shell was based on the Bourne shell (Bourne again
shell) and as with Korn it attempted to combine the best features of
the other shells which were available at the time. This shell however
was available for free.
Bash was initially adopted for LINUX although several varieties of
LINUX now exist e.g. RedHat, SuSE and Debian-GNU.
FEATURES OF UNIX
1. Multitasking
Multitasking is the capability of the operating system to
perform various tasks simultaneously. I.e. A single user can run
multiple programs (tasks) concurrently.
2. Multi-user Capability
Multi-user capability of UNIX allows several users to use the
same computer to perform their tasks. Several terminals (keyboards
and monitors) are connected to a single powerful computer (Unix
server) and each user can work with their terminals.
3. Security
Unix allows sharing of data. Every user must have a login
name and a password. So, accessing another user’s data is
impossible without permission.
I.e. Invalid users cannot access data.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 5 OF 51
4. Portability
UNIX is portable because it is written in a high level language.
So, UNIX can be run on different computers.
5. Communication
UNIX supports the fallowing communications
o Between the different terminals connected to the UNIX
server.
o Between the users of one computer to the users of
another computer located elsewhere in the network.
6. Programming facility
UNIX is highly programmable, the UNIX shell programming
language has all necessary ingredients like conditional and control
structure (Loops) and variables, that establish it as a programming
language in its own right.
ADVANTAGES OF UNIX
UNIX is a very stable operating system.
UNIX supports the Multi-User feature.
UNIX is a Multitasking operating system.
UNIX can be loaded to any type of computer hardware.
UNIX is optimized for program development.
UNIX has rich set of small commands and utilities that do
specific tasks.
UNIX has a powerful unified file system, everything in a file;
data, program and all physical devices.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 6 OF 51
RESULT:
Thus study of unix operating system was completed.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 7 OF 51
GENERAL COMMANDS
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 8 OF 51
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 10 OF 51
14 File file filename Displays the
type of the
specified file
15 Wc wc filename Print the
number of lines,
words,
characters and
bytes in the file.
16 Head head n Display the first
filename n lines from the
file
17 Tail tail n filename Display the last
n lines from the
file
18 Sort sort filename -r sort in Sorts the lines
reverse order in the text files
-f sort ignoring and displays
case them in the
-n sort console
numerically
-b ignore
leading blanks
19 Grep grep pattern -F for fixed Grep searches
filename grep the named input
-E for extended FILEs for lines
grep containing a
-c print the match to the
count of given
matching lines PATTERN.
-i ignore case
for matching
-h prefix each
matching line
with line
number
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 11 OF 51
20 Chmod chmod + for adding chmod changes
filename users permission the permissions
+ permissions - for removing of each given
file/directory permission file according
u stands for to the mode
owner specified
g stands for
group
o stands for
others
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 12 OF 51
RESULT: Thus basic UNIX commands in UNIX was studied.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 13 OF 51
EX:NO:3 VI EDITOR
What is VI?
The default editor that comes with the UNIX operating system is called vi (visual
editor). [Alternate editors for UNIX environments include Pico and emacs, a
product of GNU.]
The UNIX vi editor is a full screen editor and has two modes of operation:
In the command mode, every character typed is a command that does something
to the text file being edited; a character typed in the command mode may even
cause the vi editor to enter the insert mode.
In the insert mode, every character typed is added to the text in the file; pressing
the <Esc> (Escape) key turns off the Insert mode.
The most basic and useful commands are marked with an asterisk (* or star) in the
tables below.
NOTE: Both UNIX and vi are case-sensitive. Be sure not to use a capital letter
in place of a lowercase letter; the results will not be what you expect.
To Start vi
To use vi on a file, type in vi filename. If the file named filename exists, then
the first page (or screen) of the file will be displayed; if the file does not exist,
then an empty file and screen are created into which you may enter text.
* vi filename edit filename starting at line 1
vi -r filename Recover filename that was being edited when system crashed
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 14 OF 51
To Exit vi
Usually the new or modified file is saved when you leave vi. However, it is also
possible to quit vi without saving the file.
Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This
type of command is completed by hitting the <Return> (or <Enter>) key.
* :x<Return> quit vi, writing out modified file to file named in original invocation
:wq<Return> quit vi, writing out modified file to file named in original invocation
:q<Return> quit (or exit) vi
* :q!<Return> quit vi even though latest changes have not been saved for this vi call
Unlike many of the PC and MacIntosh editors, the mouse does not move the
cursor within the vi editor screen (or window). You must use the key commands
listed below. On some UNIX platforms, the arrow keys may be used as well;
however, since vi was designed with the Qwerty keyboard (containing no arrow
keys) in mind, the arrow keys sometimes produce strange effects in vi and should
be avoided.
If you go back and forth between a PC environment and a UNIX environment,
you may find that this dissimilarity in methods for cursor movement is the most
frustrating difference between the two.
In the table below, the symbol ^ before a letter means that the <Ctrl> key should
be held down while the letter key is pressed.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 15 OF 51
or <Return>
j
* move cursor down one line
[or down-arrow]
* k [or up-arrow] move cursor up one line
or <Backspace>
h
* move cursor left one character
[or left-arrow]
l or <Space>
* move cursor right one character
[or right-arrow]
* 0 (zero) move cursor to start of current line (the one with the cursor)
* $ move cursor to end of current line
w move cursor to beginning of next word
b move cursor back to beginning of preceding word
:0<Return> or 1G move cursor to first line in file
:n<Return> or nG move cursor to line n
:$<Return> or G move cursor to last line in file
Screen Manipulation
The following commands allow the vi editor screen (or window) to move up or
down several lines and to be refreshed.
^f move forward one screen
^b move backward one screen
^d move down (forward) one half screen
^u move up (back) one half screen
^l redraws the screen
^r
redraws the screen, removing deleted
lines
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 16 OF 51
Adding, Changing, and Deleting Text
Unlike PC editors, you cannot replace or delete text by highlighting it with the
mouse. Instead use the commands in the following tables.
Perhaps the most important command is the one that allows you to back up and
undo your last action. Unfortunately, this command acts like a toggle, undoing
and redoing your most recent action. You cannot go back more than one step.
* u UNDO WHATEVER YOU JUST DID; a simple toggle
The main purpose of an editor is to create, add, or modify text for a file.
The following commands allow you to insert and add text. Each of these
commands puts the vi editor into insert mode; thus, the <Esc> key must be
pressed to terminate the entry of text and to put the vi editor back into command
mode.
* i insert text before cursor, until <Esc> hit
I insert text at beginning of current line, until <Esc> hit
* a append text after cursor, until <Esc> hit
A append text to end of current line, until <Esc> hit
open and put text in a new line below current line, until
* o
<Esc> hit
open and put text in a new line above current line, until
* O
<Esc> hit
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 17 OF 51
Changing Text
Deleting Text
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 18 OF 51
Cutting and Pasting Text
Other Commands
Searching Text
Being able to determine the line number of the current line or the total number of
lines in the file being edited is sometimes useful.
:.= Returns line number of current line at bottom of screen
:= Returns the total number of lines at bottom of screen
provides the current line number, along with the total
^g number of lines,
in the file at the bottom of the screen
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 19 OF 51
Saving and Reading FilesThese commands permit you to input and output files other
than the named file with which you are currently working.
:r filename<Return>
read file named filename and insert after current line
(the line with cursor)
:w<Return> write current contents to file named in original vi call
:w newfile<Return> write current contents to a new file named newfile
:12,35w smallfile<Return>
write the contents of the lines numbered 12 through 35 to
a new file named smallfile
:w! prevfile<Return> write current contents over a pre-existing file named
prevfile
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 20 OF 51
Ex .No: 4A
CALCULATING THE GROSS SALARY OF AN
EMPLOYEE
AIM:
To write a simple script to calculate the gross salary of an employee.
ALGORITHM:
STEP 1 : Start
STEP 2 : Get the basic salary from the user
STEP 3 : Calculate the Da , Hra, and gross with the needed formulas namely
da=`expr $b \* 10 / 100`
hra=`expr $b \* 20 / 100`
gross=`expr $b + $da + $hra`
STEP 4 : Print the gross salary by use of echo command
STEP 5 : Stop
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 21 OF 51
PROGRAM
OUTPUT
Result:
Thus the program to find gross salary of an employee is created and
executed successfully.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 22 OF 51
Ex .No: 4 B FINDING THE AREA OF A CIRCLE
AIM:
To write a simple script to find the area of a circle.
ALGORITHM:
STEP 1 : Start
STEP 2 : Get the radius from the user
STEP 3 : Read the radius.
STEP 4 : Calculate the area by using the formula 3.142857 \* $radius \* $radius | bc
STEP 5 : Stop
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 23 OF 51
PROGRAM
OUTPUT
Result:
Thus the program to find radius of circle was created and executed
successfully.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 24 OF 51
Ex .No:5 A TWO-WAY BRANCHING
AIM:
To implement the concept of two-way branching using “if ” statement.
ALGORITHM:
STEP1 :Start
STEP2 :Declare the variables and its access
STEP3 :If a is greater than b then print A is Big, else
STEP4 :Print B is Big
STEP5 :Stop
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 25 OF 51
Program
OUTPUT
Result:
Thus the program to find biggest of given two numbers by using if statement
was created and executed successfully.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 26 OF 51
Ex .No: 5 B
MULTI-WAY BRANCHING USING “ IF-ELIF ”
STATEMENT
AIM:
To implement the concept of multi-way branching using “ if-elif ” statement.
ALGORITHM:
STEP 1 : Start
STEP 2 : Declare the variables and its access
STEP 3 : If a is greater than b and a is greater than c, then print A is Big, else
STEP 4 : If b is greater than c then print B is Big, else
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 27 OF 51
Program
OUTPUT
Result:
Thus the program to find highest number among given three numbers by using
if-elif statement was developed and executed successfully.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 28 OF 51
Ex .No: 5C
MULTI-WAY BRANCHING USING “CASE” STATEMENT
AIM:
To implement the concept of multi-way branching using “ case ” statement.
ALGORITHM:
STEP 1 : Start
STEP 2 : Initialize the number of choices
STEP 3 : If the choice is present corresponding result will be displayed otherwise
STEP 4 : The statement of the default will be displayed
STEP 5 : Stop
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 29 OF 51
Program
echo “Concept of multi-way branching using case statement”
clear
echo “1. To know Your current Directory”
echo “2.Todays Date”
echo “3.List of Users”
echo “Enter your choice”
read choice
case $choice in
1) pwd;;
2) 2)date;;
3) who;;
*) echo “Only give inputs from 1 to 3”
esac
OUTPUT
Concept of multi-way branching using case statement
1. To know Your current Directory
2.Todays Date
3.List of Users
Enter your choice
2
wed march 18 08:25:20 IST 2009
Result:
Thus the program for multiway branching by using case statement was created
and executed successfully.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 30 OF 51
Ex .No: 6 A PROGRAM TO PRINT N NATURAL NUMBERS
AIM:
To write a program to print n Natural numbers
ALGORITHM:
STEP 1 : Start
STEP 2 : Get a number from the user to print n Natural numbers
STEP 3 : Read the number
STEP 4 : Start the while loop and write the statements of the loop within do and done
STEP 5 : Echo the value of i and increment the value of i
STEP 6 : End the loop
STEP 7 : Stop
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 31 OF 51
PROGRAM
OUTPUT
Result:
Thus the program to print n natural numbers was created by using while loop
and executed successfully.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 32 OF 51
Ex .No: 6B PROGRAM TO PRINT FIRST N FIBONACCI
SERIES.
AIM:
To write a program to print first N Fibonacci Series.
ALGORITHM:
STEP 1 : Start
STEP 2 : Get the number of terms of Fibonacci Series the user needs.
STEP 3 : Read the number
STEP 4 : Initialize f1=-1,f2=1 and i=1
STEP 5 :Start the while loop and write the statements of the loop within do and done
STEP 6 : calculate the value of the Fibonacci Series with the needed formulas
STEP 7 : End the loop
STEP 8 : Stop
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 33 OF 51
PROGRAM
echo Enter the number of terms
read n
f1=-1
f2=1
i=1
echo The Fibonacci series upto $n terms is
while [ $i -le $n ]
do
f3=`expr $f1 + $f2`
echo $f3
f1=$f2
f2=$f3
i=`expr $i + 1`
done
OUTPUT
Enter the number of terms
10
The Fibonacci series upto 10 terms is
0
1
1
2
3
5
8
13
21
34
Result: Thus the program to display Fibonacci series up to given terms was created by
using while loop and executed successfully.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 34 OF 51
Ex .No: 6C PRINT THE MULTIPLICATION TABLE USING
UNTIL LOOP
AIM:
To write a program to print the multiplication table using until loop.
ALGORITHM:
STEP 1 : Start
STEP 2 : Get the number from the user to print its multiplication table.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 35 OF 51
PROGRAM
OUTPUT
Enter a number
5
The multiplication table of 5 is
5 x 1=5
5 x 2=10
5 x 3=15
5 x 4=20
5 x 5=25
5 x 6=30
5 x 7=35
5 x 8=40
5 x 9=45
5 x 10=50
Result: Thus the program for multiplication table was developed and executed
successfully by until loop.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 36 OF 51
Ex .No: 6D PROGRAM TO PRINT THE SUM USING FOR LOOP.
AIM:
To write a program to print the sum using for loop.
ALGORITHM:
STEP 1 : Start
STEP 2 : Initialize the variable i.e sum=0
STEP 3 : Start the for loop
STEP 4 : calculate the sum by using the formula sum=`expr $sum + $i` for all the
elements in the for loop list
STEP 5 : End the loop.
STEP 6 : Print the value of the sum
STEP 7 : Stop
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 37 OF 51
Program
echo “ Program implementing For Loop”
sum=0
for i in 1 2 3 4 10
do
sum=`expr $sum + $i`
done
echo “The sum is $sum”
OUTPUT
The sum is 20
Result:
Thus the program to find the sum of the numbers by using for loop was
developed and executed successfully.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 38 OF 51
Ex .No: 6E PROGRAM TO CALCULATE THE FACTORIAL
AIM:
To write a program to calculate the factorial.
ALGORITHM:
STEP 1 : Start
STEP 2 : Get the number from the user
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 39 OF 51
Program
echo "Factorial Program"
echo "Enter the number"
read a
f=1
for (( i = 1; i <= ${a}; i++ ))
do
f=`expr $f \* $i`
done
echo "The factorial of $a is $f"
OUTPUT
Factorial Program
Enter the number
5
The factorial of 5 is 120
Result:
Thus the program to find factorial of given number was created and executed
successfully by using for loop.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 40 OF 51
EX NO: 7 Program to find inverse of the given number and Fibonacci series
up to needed level using functions
AIM:
To develop the program to find inverse of the given number and
Fibonacci series up to needed level using functions
ALGORITHM:
1. Open the UNIX operating system.
2. Edit the command vi filename in the home directory or in
the directory created by user. VI stands for visual, a full
screen editor, indicates most powerful editor. File name
indicates program name.
3. Now enter required header files and open main( ) function.
4. Inside main( ) function declare required variables, declare
two functions. One function to find inverse of given number
with return type and one argument of integer data type.
5. Another function for Fibonacci series with no return type
and one argument of integer data type.
6. Assign the values to given variables at compile or runtime.
7. Call the two functions inside main ( ) function.
8. Close main ( ) function. Define two functions outside
main ( ) function.
9. To save the program, press Escape button in the keyboard
and enter :wq command.
10. To compile the program edit the command cc file name.c.
11. If no compile time error, to run the program edit the
command ./a. out.
12. If no run time error, output of the program is displayed.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 41 OF 51
Program
#include<stdio.h>
main( )
{
int n1,n2;
printf(" ENTER THE VALUE FOR N1: ");
scanf("%d",&n1);
printf(" INVERSE OF THE GIVEN NUMBER %d = %d",n1,inverse(n1));
n2=10;
printf("\n");
printf(" FIBONACCI SERIES . . . . \n ");
fibo(n2);
}
inverse(int n)
{
int a,d;
d=0;
while(n>1)
{
a=n%10;
d=d*10+a;
n =n/10;
}
return d;
}
fibo(int m)
{
int a,b,c,i;
a=0;b=1;
for(i=1;i<=m;i++)
{
if(i==1)
printf("%d\t",a);
else if(i==2)
printf("%d\t",b);
else
{
c=a+b;
printf("%d\t",c);
a=b;
b=c;
}}}
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 42 OF 51
OUTPUT
RESULT:
Thus the program to find inverse of the given number and
Fibonacci series by using functions is developed and executed successfully.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 43 OF 51
EX NO: 8 Program for the illustration of pointers expressions.
AIM:
To develop the program for the illustration of pointers
expressions.
ALGORITHM:
1. Open the UNIX operating system.
2. Edit the command vi filename in the home directory
or in the directory created by user. VI stands for visual, a full
screen editor, indicates most powerful editor. File name indicates
program name
3. Now enter required header files and open main ( )
function.
4. Inside main ( ) function declare required variables,
pointers of integer data type.
5. Give the value for the variables at compile or
runtime. After given values, assign the address of variables to the
pointers.
6. Show the value of the variables and its stored
address by pointers.
7. Evaluate more than one expression by pointers and
show the result.
8. To save the program, press Escape button in the
keyboard and enter :wq command.
9. To compile the program edit the command
cc filename.c
10. If no compile time error, to run the program edit the
command ./a. out.
11. If no run time error, output of the program is
displayed.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 44 OF 51
Program
#include<stdio.h>
main( )
{
int a,b,*p1,*p2;
printf(" ENTER THE VALUE FOR A,B : ");
scanf("%d%d",&a,&b);
p1=&a;
p2=&b;
printf(" \n %d is stored in the address %u ",*p1,p1);
printf(" \n %d is stored in the address %u ",*p2,p2);
printf(" \n (*p1+*p2)*(a -*p2) = %d", (*p1+*p2)*(a-*p2));
printf(" \n (*p1+*p2)/(*p1-b) = %d",(*p1+*p2)/(*p1-b));
printf(" \n *p1*p2/a+b = %d",*p1*p2/a+b);
printf("\n");
}
OUTPUT
Enter the value for a,b: 12 8
RESULT:
Thus the program for the illustration of pointer expressions is
developed, executed successfully
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 45 OF 51
EX NO: 9 Program for dynamic memory allocation, reallocation and
deleting.
AIM:
To develop the program for dynamic memory allocation to store
information and the reallocating dynamically the same memory to store more
information .After the deleting same memory dynamically by dynamic memory
allocation functions.
ALGORITHM:
1. Open the UNIX operating system.
2. Edit the command vi filename in the home directory or in
the directory created by user. VI stands for visual, a full
screen editor, indicates most powerful editor. File name
indicates program name.
3. Now enter required header files and open main ( )
function.
4. Inside main ( ) function declare required variables,
pointers and allocate a block of memory dynamically by
using malloc ( ) function.
5. Check whether dynamically a block of memory allocated
or not.
6. If allocated, store the information and display the
information.
7. Reallocate the same memory to store more information
than before by using realloc ( ) function.
8. Check whether reallocation of same memory dynamically
done or not.
9. If allocated, store the new information and display it.
10. If no longer needed the stored information, release by
using free ( ) function.
11. To save the program, press Escape button in the
keyboard and enter :wq command.
12. To compile the program edit the command
cc filename.c
13. If no compile time error, to run the program edit the
command ./a. out.
14. If no run time error, output of the program is displayed
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 46 OF 51
Program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main( )
{
char *buffer;
buffer=(char *)malloc(10*sizeof(char));
if(buffer==0)
{
printf(" Dynamically memory not allocated . . . \n");
exit(1);
}
else
printf(" Dynamically memory allocated . . . \n");
strcpy(buffer,"Chennai");
printf(" Buffer contains : %s \n",buffer);
buffer=realloc(buffer,50);
if(buffer==0)
{
printf(" Reallocation failed . . . \n");
exit(1);
}
printf(" Buffer size modified . . .\n");
printf(" Buffer still contains: %s \n",buffer);
strcpy(buffer,"Chennai is the capital of the Tamilnadu state ");
printf(" Buffer now contains: %s \n",buffer);
printf(" Releasing dynamically allocated memory . . . . ");
free(buffer);
}
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 47 OF 51
OUTPUT
Dynamically Memory Allocated
RESULT:
Thus the program for dynamically memory allocation, reallocation and
releasing the allocated memory dynamically is developed and executed
successfully.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 48 OF 51
EX NO: 10 Program to create file, to store information in the file and to show
the information in the file by using file handling functions.
AIM:
To develop the program to create file, to store information in the
file and to show the information in the file by using file handling functions.
ALGORITHM :
1. Open the UNIX operating system.
2. Edit the command vi filename in the home directory or in
the directory created by user. VI stands for visual, a full
screen editor, indicates most powerful editor. File name
indicates program name.
3. Now enter required header files and open main ( )
function.
4. Inside main ( ) function declare required variables,
file pointer. Open the file with write mode and assign to
the file pointer.
5. Enter number of students information to store.
6. Through for( ) loop, read the one by one student
information by using stdin keyword, represents the
keyboard and write to the given file by file pointer.
7. Close the file using fclose ( ) function with file pointer as
argument.
8. Again open the file with read mode and assign to the file
pointer.
9. Again through same for( ) loop, read one by one student
information from the file by file pointer and write to the
screen by using stdout keyword, represents screen.
10. For reading use fscanf ( ) function and for writing fprintf ( )
function.
11. Close the file using fclose ( ) function with file pointer as
argument.
12. To save the program, press Escape button in the
keyboard and enter :wq command.
13. To compile the program edit the command
cc filename.c
14. If no compile time error, to run the program edit the
command ./a. out.
15. If no run time error, output of the program is displayed.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 49 OF 51
Program
#include<stdio.h>
main( )
{
FILE *fp;
char studname[15];
int n, i,em,tm,mm,sm.scm,total;
float avg;
fp=fopen("studentdetails","w");
printf(" ENTER NUMBER OF STUDENTS : ");
scanf("%d",&n);
printf(" enter student name and fallowing subject marks \n \n");
printf(" english tamil maths science socialscience \n \n");
for(i=1;i<=n;i++)
{
printf(" %d: STUDENT DETAILS \n ",i);
fscanf(stdin,"%s%d%d%d%d%d",studname,&em,&tm,&mm,&sm,&scm);
fprintf(fp,"%s%d%d%d%d%d",studname,em,tm,mm,sm,scm);
printf("\n");
}
fclose(fp);
fp=fopen("studentdetails","r");
printf(" STUDENT DETAILS STORED IN THE FILE . . . \n \n");
for(i=1;i<=n;i++)
{
fscanf(fp,"%s%d%d%d%d
%d",studname,&em,&tm,&mm,&sm,&scm);
total=em+tm+mm+sm+scm;
avg=total/5; fprintf(stdout,"%s\n%d\n%d\n%d\n%d\n
%d\n%d\n%f",studname,em,tm,mm,sm,scm,total,avg);
printf("\n");
}
fclose(fp);
}
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 50 OF 51
OUTPUT
1: STUDENT DETAILS
Gnanasekar.P
48 78 90 76 56
2: STUDENT DETAILS
Visu.L
70 89 76 65 54
Gnanasekar.P
48
78
90
76
56
348
69.6
Visu.L
70
89
76
65
54
354
70.8
RESULT:
Thus the program for file handling by using file handling functions is
developed and executed successfully.
.
ISO/CSE/MANUAL/CP LBII – GE 2155
ISSUE: B REV: 00 PAGE NO: 51 OF 51