Escolar Documentos
Profissional Documentos
Cultura Documentos
Semester
: II
LAB MANUAL
F .E.
COMPUTER PROGRAMMING
Prepared By
Prof. S. S. INGOLE
(Assist. Professor in Computer Department)
CERTIFICATE
This is to certify that
Mr./ Miss _______________________________________,
Of Class FIRST YEAR Roll No. _____ Has completed all the practical
work in the subject COMPUTER PROGRAMMING satisfactorily in
the Department of FIRST YEAR ENGINEERING as prescribed by
University of Solapur, in the academic year 2011 -2012.
Staff In-charge
Principal
2.
3.
4.
5.
6.
7.
8.
Page. No
Date
Sign
Experiment No.
Title
:1
Date
Objective
:
1. To understand history of c language
2. To understand the concept of algorithm and flowchart.
3. To understand the concept of data types
Theory
INTRODUCTION TO C:
C is a programming language written by Dennis Ritche and developed at AT & T Bell Lab in USA
in 1972. C is a high level language. Most of the part of Operating System like Windows, UNIX, and
Linux are written in C. Many 3D gaming frame works are written in C language where speed is
important factor. Drivers are also written in C language.
ALGORITHM :
Algorithm is step by step procedure for solving a problem. Each step is called instruction. Algorithm
consists of English like sentences.
Characteristics of algorithm:
1. Input: It may accept zero or more inputs.
2. Output: It should produce at least one output [result].
3. Definiteness: Every instruction must be clear and well defined. There should not
be any ambiguity
4. Finiteness: It should end after finite number of steps. It should not enter into an
Infinite loop.
5. Effectiveness: Every operation must be simple and should complete in a finite
time.
ALGORITHM NOTATIONS:
Following notations are considered while writing an algorithm
1. Name of algorithm: It specifies problem definition to be solved.
2. Step number: Identification tag of an instruction.
3. Explanatory comment: It describes the operation and it is written within pair of
square brackets.
4. Termination: It specifies end of algorithm. It is generally stop statement.
START
Read a, b, c
sum = a+
b+c
Avg=sum/3
Print Average
STOP
Table A shows the different shapes used to draw flowchart with their function.
TABLE A
Shapes
Name
Function
START and STOP.
Terminal
(Oval)
Parallelogram
Input, Output
Rectangle
Processing
Rhombus
Decision making
Arrows
Connection.
Circle
Continuation
Hexagon
Repetition/ Looping
Double sided
rectangle
(predefined
process)
DATA TYPES
Data type indicates the type of data that variable can hold. Data or information consists of figures,
numbers and facts. If we want to save integer value with variable name x then declaration will be int
x where int is integer data type and x is variable name which stores integer value only. Data types are
categorized into.
a) Primary or Fundamental data type.
b) Derived data type.
c) User defined data type.
Data Type
Integer
Float
Double
Character
Void
Keyword
int
float
double
char
void
Size in bytes
2
4
8
1
--
Table 1.1
Integer data type allows to store integer value only. The memory space allocated for integer data type
is 2 bytes for 16 bit machine. The range of integer data type is -32768 to +32767.
Floating data type store floating point values only. The range of float data type is -3.4e38 to
+3.4e38. The memory space allocated for float data type is 4 bytes for 16 bit machine.
Double data type store double value than the float. The range of double data type is -1.7e308 to
+1.7e308. The memory space allocated for double data type is 8 bytes for 16 bit machine.
Character data type is used to store single character or number. The range of character data type is
-128 to +127. The memory space allocated for character data type is 1 bytes for 16 bit machine.
Conclusion
2. What is mean by data types? And what are the different types of it explain with the
examples?
Experiment No.
Title
:2
Date
Problem Statement : Write a program to implement the control statements and loops
Objective
Theory
:
1. To understand concept of control statements.
2. To understand the concept of loop.
The statement used to control the program is called control statement. The flow of program execution
is controlled by these control statement. The control statement has three types:
1.
2.
3.
Selection
Repetition
Sequence
Selection: Selection control type is used to select condition from multiple conditions. After selecting,
that particular condition, it get execute. If else and switch case loops are used to select a condition
from multiple condition.
Repetition: Sometime user wants to repeat certain statements or instructions. By using initial and
final condition user can repeat the statement or instruction. The repetition starts from initial condition
and ends with when final condition occurs. While and for loops are used for repetition and Sequence.
Sequencing is the execution of statement or instruction depending upon conditions.
DECISION CONTROL STRUCTURE :
Decision making is used to select a particular condition from multiple conditions. If statement and Ifelse statement are the major decision making statement.
If statement:
The general form of if statement is as follows:
Entry
If condition
{
(Execute this statement);
Execute statement
}
True
False
If-else statement.
If there are two group of statements and it is desired that one of them will be executed, if condition is
true and other will be executed if condition is false. In such situation if-else statement is used.
if(condition)
Entry
{
(Execute this statement1);
True
if condition
False
}
Execute statement 1
else
Execute statement 2
{
(Execute this statement 2);
}
LOOP CONTROL STRUCTURE :
Loop means repetition of instruction. The loop will iterate till given condition become true. When
condition becomes false the loop gets breaks. If user want to print his name 10 times, then there is no
need to write printf ( ) statement 10 times.
There are 3 loops control structure available in C:
1.
2.
3.
While loop
Do-while loop
For loop
While loop
The while loop starts with while keyword followed by condition. The general form of while loop is as
follows: Initialize loop counter
Start
while (condition)
{ Statement 1;
Statement 2;
Initialize
Test
Stop
Increment/decrement counter}
Body of loop
Increment
When execution of program reaches at while loop, first condition is checked. If condition is true, it
will enter into body of loop. The body of loop iterate until the condition of while loop becomes false.
Do-while loop:
The main difference between while and do-while is checking of test condition. In while loop,
condition checks in the beginning of loop but in do-while loop condition, checks at the end loop.
The general form of do-while loop is:
Start
Initialize
do{
Body of loop
Statement 1;
Statement 2;
Increment
True
Test
False
Stop
---------------;
---------------;
Increment/decrement counter.
} while (condition);
For loop:
The for loop is most powerful and popular loop for repetition of instruction.The general form of
for loop is:
for (initialize counter; test counter; increment counter)
{
Do this;
Do this;
And this;
Do this;
Break;
--------------------Do this;
}
In switch statement, different conditions are called as cases. These cases are break by the keyword
break. Let discuss about how switch statement executed. Firstly expression is evaluated. Then value
of it matching with constant value of case statement. When match found then program execute
statement following that case. If match not found, then default statement will executed.
Conclusion:
b) If else
c) while
d) do while
e) for
Experiment No
Title
Date
Objective
:1)To understand the basic concept of call by value and call by reference
Theory:
Up to now we used functions like printf( ), scanf( ), main( ), strcpy( ), strlen( ) etc. A function is
self contained block of statements that performs a specific task. Every C program is collection of
functions. If a program written by programmer is very large & complex and it is also difficult to test
and debug. Then such programs are divided into sub programs. These sub programs are easily
compiled and test. These sub programs are called functions.
Figure A
TYPES OF FUNCTIONS :
There are two types of Functions:
1. Library Functions: Library functions are readymade functions available in C so there is no need to
develop these functions. Library functions are not written by user.
2. User Defined Functions: User defined functions are to be developed by user. In this chapter, we will
focus on user defined functions.
Function declaration
Function Call
Function Definition
1.
Function Declaration:
Function declaration is just like a variable declaration. Function can be declared above main ( )
function. The general form of function declaration is:
Data type function_name (parameter list)
The program which call the function for execution is called calling function. The calling function can
communicate with called function by passing parameters, whereas the called function can
communicate with calling function by returning the value.
Example:
Declare the function with float data type for return value one integer and one float as a parameter.
Void add( );
The parameters are separated by ,.
If function is not having any parameter, then we can write void in bracket.
2.
Function Call:
A function call is called by writing the function name and passing parameter if required. The general
form of function call is as follows:
Function_name (Actual Parameter list)
Example:
add();
Here, cube is function name and we pass number the parameter. Here number is called as
actual parameter. When compiler encounters a function call, the control is transferred to that
function. When execution of function completes, then automatic control is transferred to calling
function.
The function call has two types:
1.
2.
Call by value
Call by reference
Experiment No
Title
Date
: Swapping of two nos. by using third variable and without using third
Variable.
:
Objective
Theory
a=b;
//a=20.
b=temp; //b=10.
After swapping a=20 & b= 10.
Swapping of two variables also possible by without using third variable, syntax is as
follows:
Int a=5, b=10;
b=a+b;
//b=5+10=15.
a=b-a;
// a=15-5=10.
b=b-a;
// b=15-10=5.
Passing a variable by value makes a copy of the variable before passing it onto a
function. This means that if you try to modify the value inside a function, it will only
have the modified value inside that function. One the function returns, the variable you
passed it will have the same value it had before you passed it into the function. When we
call the function by passing the values or variables, the function is called as call by
values.
Algorithm
Step 1: Start
Step 2: Take the two numbers from keyboard.
Step 3: Use temp variable.
Step 4: temp=a;
a=b;
b=temp;
Execute this logic, two numbers will be swap.
Step 6: Stop
Conclusion:
Experiment No.
Title
.
Date
Objective
Theory
We frequently use files for storing information which can be processed by our programs. In
order to store information permanently and retrieve it we need to use files. Files are not only
used for data. Our programs are also stored in files. The editor which you use to enter your
program and save it simply manipulates files for you. As you know, you can have many files on
your disk. If you wish to use a file in your programs, then you must specify which file or files
you wish to use.
Specific operation of files:
1. Creating a new file.
2. Opening an existing file.
3. Reading data from file.
4. Writing data to the file.
5. Moving to specific location in file.
6. Closing a file.
File Functions:
Fopen()
To open a file.
Fclose()
To close a file.
Fgetc()
Fputc()
Fgets()
Fputs()
Specifying the file you wish to use is referred to as opening the file. When you open a file you
must also specify what you wish to do with it i.e. Read from the file, Write to the file, or both.
Because you may use a number of different files in your program, you must specify when
reading or writing which file you wishes to use. This is accomplished by using a variable called
a file pointer. Every file you open has its own file pointer variable. When you wish to write to a
file you specify the file by using its file pointer variable. You declare these file pointer variables
as follows:
FILE *fopen (), *fp1, *fp2, *fp3;
The variables fp1, fp2, fp3 are file pointers. You may use any name you wish. The file
<stdio.h> contains declarations for the Standard I/O library and should always be included at
the very beginning of C programs using files. Constants such as FILE, EOF and NULL are
defined in <stdio.h>.You should note that a file pointer is simply a variable like an integer or
character. It does not point to a file or the data in a file. It is simply used to indicate which file
your I/O operation refers to. A file number is used in the Basic language and a unit number is
used in FORTRAN for the same purpose. The function fopen is one of the Standard Library
functions and returns a file pointer which you use to refer to the file you have opened e.g.
fp = fopen ( prog.c, r) ;
The above statement opens a file called prog.c for reading and associates the file pointer fp with
the file. When we wish to access this file for I/O, we use the file pointer variable fp to refer to it.
You can have up to about 20 files open in your program - you need one file pointer for each file
you intend to use.
Algorithm:
Step 1: Start
Step 2: Create the text files in TC/ BIN folder.
Step 3: Save these text files with some name.
Step 4: In one of the text file write the data, that file is nothing but source file. Another text file
keeps as a blank.
Step 5: Open these two text files in turbo c Window by using fopen ( ) function.
Step 6: Perform read operation on source file and read the information from this file by using
fgetc ( ) function.
Step 7: Perform the write operation on the target file, by using fputc( ) function.
Step 8: Stop
Conclusion:
Experiment No.
Title
:6
Date
Problem Statement : Write a program to perform whether our string is palindrome or not,
Implement string function
Objective:
Theory
String is the collection of characters. Example Welcome. The general form of the string is char
array-name [size];
Char name [13]; here name is a character array having size 13. The empty memory location is
placed with /0 (NULL) it indicates end of string.
0
2
l
If the reverse of a string is equal to original string then it is called palindrome. We are
going to perform different operations on string such as calculate the length of the string,
reverse the string , copy one string to the another, and check whether given string is
palindromes or not.
There are some string functions are available in C:
1. strlen ()
- string length
- string concatenate
5. strlwr()
- string lower
6. strupr()
-string upper
Algorithm:
Step 1: Start
Step 2: Read the string
Step 3: Make reverse of the given string.
Step4: Store this reverse string into the another temporary string
Step 4: Compare these two strings
Step 5: If both are equal then print palindrome
Step 6: Otherwise print not palindrome
Step 7: Stop
Conclusion:
Experiment No.
:5
Title
Date
Objective
:
1) To understand the basic concept Pointer
2) To understand the basic concept of array of pointer
3) To understand the concept of declaration, accessing of pointer.
Theory
The Pointer is one of the powerful tools of C programming. Pointers are the variables which stores
the address of another variable.
Advantages of Pointers
1.
2.
3.
4.
5.
6.
We know that memory is consist of millions of cells. Each cell has its own address (Address is
nothing but numbers). These memory locations we can assign by using variable or memory address.
Let int i=3;
Here, i is name of memory location and memory space reserved to store integer value. 3 is stored at
memory location.
i
location name
3
3 Value at location
65524 Location number (Address)
j
3
65524
65524
65522
The variable j having its own address i.e. 65522. The variable j stores the address of i, so j is called as
pointer.
Declaration of Pointer:
The pointer is declared as follows:
Data type *pointer_name;
int *i;
char *ch;
float *f;
Accessing Pointer
After declaring pointer it is necessary to store address of required variable into it. That is we create a
link between pointer and variable. The & is used to assign address of variable.
The general form to assign address to pointer is:
pointer_name = & (variable name)
Example: The statement ptr = &i shows that ptr points to i variable.
void main ( )
{ int i = 3;
int *ptr;
ptr = &i; }
a[0]
a[1]
a[2]
a[3]
a[4]
location of array
Array Element
1000
1002
1004
1006
1008
Memory Address
Array elements are stored in contiguous memory location. If we store base element address to 0th
element then rest of elements are accessed by pointer. Now, we assign the base address to pointer as
follows:
int *ptr;
ptr=&a[0];
Here, base address of 0th element will be stored. By using statement ptr++ we will easily go to the
next location.
ARRAY OF POINTER
Array of pointer is nothing but it is a collection of pointers of same data type. The array of pointer is
declared as:
Data type *array_name [size];
Example:
int *a[20];
char *state[3]={
Goa,
Maharashtra,
Karnataka,
}
POINTER TO STRUCTURE
Structure is collection of element. These elements may be same type or may be different data type.
We can declare pointer to structure. We use () operator to point to the member of structure. The
general form of pointer to structure is:
struct <structure name>
{
Data type
variable name;
------------------------------};
struct *ptr;
Conclusion:
Experiment No.
:4
Title
Date
Problem Statement :
Write a program to perform operations on matrices (Addition Subtraction,
Multiplication of two matrices.).
Objective
:
1. To understand concept of array.
2. Passing of array to function.
3. To understand different Operation on Matrices.
4. To understand concept of 2 Dimensional array.
5. Passing of 2 Dimensional arrays to function.
6. To understand different Operation on Matrices.
Theory
Array is a collection of elements of similar data type. Discussing array without using pointer
makes the discussion incomplete and wanting. Array makes the use of pointers internally. In
array for loop is used to access input values from first location till the end of array.
TYPES OF ARRAY
1. ONE DIMENSIONAL ARRAY.
2. TWO DIMENSIONAL ARRAY.
3. MULTIDIMENSIONAL ARRAY.
1.ONE DIMENSIONAL ARRAY
SYNTAX:
Data type array_ name [size];
Examples:
1. int a[5];
2. float b[10];
3. ch ar name [20];
a[0]
a[1]
a[2]
10
15
20
a[3]
30
a[4]
40
elements of matrix A are added to their corresponding elements in matrix B and stored as
elements of matrix C. Obviously, all three matrices must be of the same order or size. Notice that
the plus sign in the above equation is enclosed in parentheses to indicate addition of matrix
elements, as contrasted with addition of matrices, to be discussed in section to follow. An
example of addition of matrix elements is schematized below
and illustrated as
and illustrated as
In sum, the above matrix elements can be added and subtracted if and only if the matrices are of
the same order (identical in the number of rows and columns). Matrices upon which an operation
is permissible are said to conform to the operation.
MULTIPLICATION OF MATRIX
If A = [a ij] is a matrix of order m x n and B = [b ij] a matrix of order n x p, then the product C =
AB of the two matrices is a matrix of order m x p matrix defined.
Conclusion
2. What is the difference between one dimensional array and two dimensional array?
3. How to assign pointer to the one dimensional and two dimensional array?