Wednesday, 12 October 2011


IMPLEMENTATION OF STATIC MEMBERS

Aim:
To write a C++ program to demonstrate the static members

Algorithm:
Step 1: Start the program.
Step 2: Create a class with static data members and static function.
Step 3: The static value is initialized to zero during object creation. The static value is incremented when a member function set is called for every object.
Step 4: We can also reinitialize the static member value during the static member definition.
Step 5: The result is displayed.
Step 6: Stop the process.

Program:

#include<iostream.h>
#include<conio.h>
class staticmember
{
 int a;
 static int count,cnt;
 public:
       void set()
       {
        a=++count;
        cnt++;
       }
       void show()
       {
            cout<<"\n";
            cout<<"Object Number:"<<a<<"\n";
       }
       static void showcount()
       {
         cout<<"Static count value:"<<count<<"\n";
         cout<<"Static cnt value:"<<cnt<<"\n";
       }
};
int staticmember::count;
int staticmember::cnt = 100;
void main()
{
  staticmember s1,s2,s3;
  clrscr();
  s1.set();
  s1.show();
  cout<<endl;
  s2.set();
  staticmember::showcount();
  s2.show();
  cout<<endl;
  s3.set();
  staticmember::showcount();
  s3.show();
  cout<<endl;
  getch();
}

Output:

Object Number:1

Static count value:2
Static cnt value:102

Object Number:2

Static count value:3
Static cnt value:103

Object Number:3

Result:
Thus the C++ program was executed and the output is verified.


LIST OF EXPERIMENTS

1.                              Implementation of Static Members
2.                              Calculation of Simple Interest using Default Arguments
3.                              Matrix Multiplication using friend functions
4.                              Implementation of Complex Numbers using Operator Overloading
5.                              Implementation of Complex Numbers using Operator Overloading and Type
                  Conversion
6.                              Matrix Class with Constructor, Destructor, Copy constructor and Assignment
                  operator overloading
7.                              Overloading New and Delete Operator
8.                              Creating Linked List using Templates
9.                              Implementation of Bubble sort using Templates
10.                          Implementation of Insertion sort using Templates
11.                          Implementation of Merge sort using Templates
12.                          Implementation of Quick sort using Templates
13.                          Implementation of Stack using Exception Handling
14.                          Implementation of Queue using Exception Handling
15.                          Minimum Spanning Tree
16.                          Dynamic Polymorphism & RTTI 
17.                          Operations on complex numbers using files as Storage

oops lab manual


SYLLABUS
141353            OBJECT ORIENTED PROGRAMMING LAB                                       0  0  3  2
(Common to CSE & IT)

1.    Design C++ classes with static members, methods with default arguments, friend functions.  (For example, design matrix and vector classes with static allocation, and a friend function to do matrix-vector multiplication)
2.    Implement complex number class with necessary operator overloadings and type conversions such as integer to complex, double to complex, complex to double etc.
3.    Implement Matrix class with dynamic memory allocation and necessary methods.  Give proper constructor, destructor, copy constructor, and overloading of assignment operator.
4.    Overload the new and delete operators to provide custom dynamic allocation of memory.
5.    Develop a template of linked-list class and its methods.
6.    Develop templates of standard sorting algorithms such as bubble sort, insertion sort, merge sort, and quick sort.
7.    Design stack and queue classes with necessary exception handling.
8.    Define Point class and an Arc class.  Define a Graph class which represents graph as a collection of Point objects and Arc objects.  Write a method to find a minimum cost spanning tree in a graph.
9.    Develop with suitable hierarchy, classes for Point, Shape, Rectangle, Square, Circle, Ellipse, Triangle, Polygon, etc.  Design a simple test application to demonstrate dynamic polymorphism and RTTI.
10.  Write a C++ program that randomly generates complex numbers (use previously designed Complex class) and writes them two per line in a file along with an operator (+, -, *, or /).  The numbers are written to file in the format (a + ib).  Write another program to read one line at a time from this file, perform the corresponding operation on the two complex numbers read, and write the result to another file (one per line).

   (Common to Information Technology & Computer Science Engineering)

List of Equipments and software for a batch of 30 students


  1. PC – 30 nos.
·      Processor – 2.0 GHz or higher
·      RAM – 256 MB or higher
·      Hard disk – 20 GB or higher
·      OS- Windows 2000/ Windows XP/ NT
  1. Software – Turbo C  (freeware) – to be installed in all PC’s.

Thursday, 29 September 2011


IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME USING BEST FIT

AIM:

To write a program to implement dynamic memory allocation best fit.

ALGORITHM:

1. Start the program.

2. Enter the number of blocks.

3. Enter the base address and size for each block.

4. Enter the size of process.

5. Compute and allocate the smallest block which is large enough to hold the process request.

6. Stop the program.


PROGRAM:
#include<stdio.h>
void line()
{
int i;
printf("\n");
for(i=0;i<80;i++)
printf("-");
}
void main()
{
int bsize[20],i,n,j,temp,psize,baddr[20];
line();
printf("\n\t\tBEST FIT PLACEMENT ALGORITHM\n");
line();
printf("\nEnter the number of blocks:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter base address of block no %d:",i+1);
scanf("%d",&baddr[i]);
printf("\nEnter free block size no. %d:",i+1);
scanf("%d",&bsize[i]);
}
printf("\nEnter the size of the process to be fit:");
scanf("%d",&psize);
line();
printf("\nBEFORE ALLOCATION:\n");
printf("\nBlock No. \tBase Address \tBlock Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
line();
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(bsize[j]>bsize[j+1])
{
temp=bsize[j];
bsize[j]=bsize[j+1];
bsize[j+1]=temp;
temp=baddr[j];
baddr[j]=baddr[j+1];
baddr[j+1]=temp;
}
for(i=0;i<n;i++)
if(bsize[i]<psize)
continue;
else
{
bsize[i]-=psize;
baddr[i]+=psize;
break;
}
if(i==n)
printf("\nThe process has not been fit");
else
{
printf("\nAFTER ALLOCATION:\n");
printf("\nBlock No. \tBase Address \t Block Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
}
line();
printf("\n");
}


OUTPUT:
[root@localhost ~]# ./best

--------------------------------------------------------------------------------
BEST FIT PLACEMENT ALGORITHM

--------------------------------------------------------------------------------
Enter the number of blocks:4

Enter base address of block no 1:10

Enter free block size no. 1:45

Enter base address of block no 2:60

Enter free block size no. 2:75

Enter base address of block no 3:150

Enter free block size no. 3:100

Enter base address of block no 4:300

Enter free block size no. 4:140

Enter the size of the process to be fit:50

--------------------------------------------------------------------------------
BEFORE ALLOCATION:

Block No.       Base Address    Block Size
1               10              45
2               60              75
3               150             100
4               300             140
--------------------------------------------------------------------------------
AFTER ALLOCATION:

Block No.       Base Address     Block Size
1               10              45
2               110             25
3               150             100
4               300             140
--------------------------------------------------------------------------------
[root@localhost ~]#

IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME USING BEST FIT

AIM:

To write a program to implement dynamic memory allocation best fit.

ALGORITHM:

1. Start the program.

2. Enter the number of blocks.

3. Enter the base address and size for each block.

4. Enter the size of process.

5. Compute and allocate the smallest block which is large enough to hold the process request.

6. Stop the program.


PROGRAM:
#include<stdio.h>
void line()
{
int i;
printf("\n");
for(i=0;i<80;i++)
printf("-");
}
void main()
{
int bsize[20],i,n,j,temp,psize,baddr[20];
line();
printf("\n\t\tBEST FIT PLACEMENT ALGORITHM\n");
line();
printf("\nEnter the number of blocks:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter base address of block no %d:",i+1);
scanf("%d",&baddr[i]);
printf("\nEnter free block size no. %d:",i+1);
scanf("%d",&bsize[i]);
}
printf("\nEnter the size of the process to be fit:");
scanf("%d",&psize);
line();
printf("\nBEFORE ALLOCATION:\n");
printf("\nBlock No. \tBase Address \tBlock Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
line();
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(bsize[j]>bsize[j+1])
{
temp=bsize[j];
bsize[j]=bsize[j+1];
bsize[j+1]=temp;
temp=baddr[j];
baddr[j]=baddr[j+1];
baddr[j+1]=temp;
}
for(i=0;i<n;i++)
if(bsize[i]<psize)
continue;
else
{
bsize[i]-=psize;
baddr[i]+=psize;
break;
}
if(i==n)
printf("\nThe process has not been fit");
else
{
printf("\nAFTER ALLOCATION:\n");
printf("\nBlock No. \tBase Address \t Block Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
}
line();
printf("\n");
}


OUTPUT:
[root@localhost ~]# ./best

--------------------------------------------------------------------------------
BEST FIT PLACEMENT ALGORITHM

--------------------------------------------------------------------------------
Enter the number of blocks:4

Enter base address of block no 1:10

Enter free block size no. 1:45

Enter base address of block no 2:60

Enter free block size no. 2:75

Enter base address of block no 3:150

Enter free block size no. 3:100

Enter base address of block no 4:300

Enter free block size no. 4:140

Enter the size of the process to be fit:50

--------------------------------------------------------------------------------
BEFORE ALLOCATION:

Block No.       Base Address    Block Size
1               10              45
2               60              75
3               150             100
4               300             140
--------------------------------------------------------------------------------
AFTER ALLOCATION:

Block No.       Base Address     Block Size
1               10              45
2               110             25
3               150             100
4               300             140
--------------------------------------------------------------------------------
[root@localhost ~]#

IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME USING FIRST FIT

AIM:

To write a program to implement dynamic memory allocation first fit.

ALGORITHM:

1. Start the program.

2. Enter the number of blocks.

3. Enter the base address and size for each block.

4. Enter the size of process.

5. Compute and allocate the first block which is large enough to hold the process request.

6. Stop the program.

PROGRAM:

#include<stdio.h>
void line()
{
int i;
printf("\n");
for(i=0;i<80;i++)
printf("-");
}
void main()
{
int bsize[20],i,n,psize,baddr[20];
line();
printf("\n\t\tFIRST FIT PLACEMENT ALGORITHM\n");
line();
printf("\nEnter the number of blocks:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter base address of block no %d:",i+1);
scanf("%d",&baddr[i]);
printf("\nEnter free block size no. %d:",i+1);
scanf("%d",&bsize[i]);
}
printf("\nEnter the size of the process to be fit:");
scanf("%d",&psize);
line();
printf("\nBEFORE ALLOCATION:\n");
printf("\nBlock No. \tBase Address \tBlock Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
line();
for(i=0;i<n;i++)
if(bsize[i]<psize)
continue;
else
{
bsize[i]-=psize;
baddr[i]+=psize;
break;
}
if(i==n)
printf("\nThe process has not been fit");
else
{
printf("\nAFTER ALLOCATION:\n");
printf("\nBlock No. \tBase Address \t Block Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
}
line();
printf("\n");
}


OUTPUT:

[root@localhost ~]# ./first

--------------------------------------------------------------------------------
FIRST FIT PLACEMENT ALGORITHM

--------------------------------------------------------------------------------
Enter the number of blocks:4

Enter base address of block no 1:10

Enter free block size no. 1:45

Enter base address of block no 2:60

Enter free block size no. 2:75

Enter base address of block no 3:150

Enter free block size no. 3:100

Enter base address of block no 4:300

Enter free block size no. 4:140

Enter the size of the process to be fit:50

--------------------------------------------------------------------------------
BEFORE ALLOCATION:

Block No.       Base Address    Block Size
1               10              45
2               60              75
3               150             100
4               300             140
--------------------------------------------------------------------------------
AFTER ALLOCATION:

Block No.       Base Address     Block Size
1               10              45
2               110             25
3               150             100
4               300             140
--------------------------------------------------------------------------------

RESULT:
Thus the program was executed and the output was verified.

IMPLEMENTATION OF PRODUCER CONSUMER PROBLEM
AIM:

            To write a c program to implement Producer- consumer problem.

ALGORITHM:

            1. Start the program.

            2. Declare the variables required.

            3. If the buffer is empty, Allow the producer to produce the item and add it in the queue.

            4. Note the position of the buffer.

            5. Display the item added to the queue.
             
 6.  If the buffer is full,Producer can't produce the item and the consumer is allowed to
   consume the item.

            7. Decrement the queue and display the values.

            8. Stop the program
  
PROGRAM:
#include<stdio.h>
#define BUFFER_SIZE 5
int in,out=0;
struct semaphore
{
int b[10],c[10],d[10];
};
struct semaphore b[BUFFER_SIZE],c[10],d[10];
int main()
{
int c,q=0;
do
{
printf("\n******** PRODUCER-CONSUMER PROBLEM **********");
printf("\n 1.Producer\n 2.Consumer\n 3.Exit\n");
printf("\nEnter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
producer();
break;
case 2:
consumer();
break;
case 3:
q=1;
}
}
while(!q);
return 0;
}
producer()
{
int i;
if(((in+1)%BUFFER_SIZE)==5)
printf("\nBUFFER IS FULL\n");
else
{
for(i=1;i<BUFFER_SIZE;i++)
{
printf("\nProducer creates the product:%d\n",i);
b[in]=c[i];
printf("The position of Buffer before production:%d\n",i);
in=(in+1)%BUFFER_SIZE;
printf("The position of the Buffer after production:%d\n",in);
}
}
}
consumer()
{
int i;
if(in==out)
printf("BUFFER IS EMPTY\n");
else
{
for(i=1;i<BUFFER_SIZE;i++)
{
printf("Consumer used the product:%d\n",i);
d[i]=b[out];
printf("The position of buffer before consumption:%d\n",out);
out=(out+1)%BUFFER_SIZE;
printf("The position of the buffer after consumption:%d\n",out);
}
}
}

 OUTPUT:
 [root@localhost ~]# ./pc
  
******** PRODUCER-CONSUMER PROBLEM **********
 1.Producer
 2.Consumer
 3.Exit
Enter your choice:1
Producer creates the product:1
The position of Buffer before production:1
The position of the Buffer after production:1
Producer creates the product:2
The position of Buffer before production:2
The position of the Buffer after production:2
Producer creates the product:3
The position of Buffer before production:3
The position of the Buffer after production:3
Producer creates the product:4
The position of Buffer before production:4
The position of the Buffer after production:4
******** PRODUCER-CONSUMER PROBLEM **********
 1.Producer
 2.Consumer
 3.Exit
Enter your choice:2
Consumer used the product:1
The position of buffer before consumption:0
The position of the buffer after consumption:1
Consumer used the product:2
The position of buffer before consumption:1
The position of the buffer after consumption:2
Consumer used the product:3
The position of buffer before consumption:2
The position of the buffer after consumption:3
Consumer used the product:4
The position of buffer before consumption:3
The position of the buffer after consumption:4
******** PRODUCER-CONSUMER PROBLEM **********
 1.Producer
 2.Consumer
 3.Exit
Enter your choice:3
--------------------------------------------------------------------------------

RESULT:
Thus the program was executed and the output was verified.