Pages

Showing posts with label ARRAY. Show all posts
Showing posts with label ARRAY. Show all posts

Wednesday, September 12, 2012

C++ PROGRAM TO IMPLEMENT QUEUE USING ARRAY.


#include<iostream.h>
#include<conio.h>
class queue
{
  public:
  int q[5],front,rear,x,result;
  void enq();
  void dque();
  void disp();
  queue()
  {
    front=0;
    rear=0;
  }
};
void queue::enq()
{
  if(rear>=5)
  cout<<"\nQueue overflow!!\n";
  else
  {
    cout<<"\nEnter the number to be inserted: ";
    cin>>x;
    rear++;
    q[rear]=x;
    cout<<"\nNumber pushed in the queue:"<<q[rear];
  }
}
void queue::dque()
{
  if(rear==0)
  cout<<"\nQueue underflow!!\n";
  else
  {
    if(front==rear)
    {
      front=0;
      rear=0;
    }
    else
      front++;
  }
  cout<<"\nDeleted element is:";
  result=q[front];
  cout<<result;
}
void queue::disp()
{
  if(rear==0)
    cout<<"\nQueue underflow!!\n";
  else
    cout<<"\nContents of queue is:";
  for(int i=front+1;i<=rear;i++)
    cout<<q[i]<<"\t";
}
void main()
{
  int c;
  queue qu;
  clrscr();
//  cout<<"\n*****";
//  cout<<"\nQUEUE";
//  cout<<"\n*****";
  do
  {
    cout<<"\n1.Insertion\n2.Deletion\n3.Display\n";
    cout<<"\nEnter your choice:";
    cin>>c;
    switch(c)
    {
      case 1:
    qu.enq();
    break;
      case 2:
    qu.dque();
    break;
      case 3:
    qu.disp();
    break;
      default:
    cout<<"\nInvalid choice!!\n";
    }
  }
  while(c<4);
  getch();
}


OUTPUT


Saturday, September 1, 2012

ROW SUM AND COLUMN SUM OF A 2-D MATRIX USING POINTER


C++ program to find the row and column sums of a two-dimensional array of integers using pointers. The underlying representation of the 2-d array must be a single-subscripted array of integers with rows*columns number of elements. Calculate row sum and column sum and display the result along with the array. 

 


#include<iostream.h>

#include<conio.h>

void main()

{

  int r,c,i,j;
  clrscr();
  cout<<"Enter the no. of rows and columns"<<endl;
  cin>>r>>c;
  int *a=new int[r*c];
  int *colsum=new int[c];
  int *rowsum=new int[r];
  cout<<"Enter the array elements\n";
  for(i=0;i<r;i++)
  {
    rowsum[i]=0;
    for(j=0;j<c;j++)
    {
      cin>>a[i*c+j];
      rowsum[i]=rowsum[i]+a[i*c+j];
    }
  }
  for(j=0;j<c;j++)
  {
    colsum[j]=0;
    for(i=0;i<r;i++)
    {
      colsum[j]=colsum[j]+a[i*c+j];
    }
  }
  cout<<"\nThe no.s are\n";
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
      cout<<a[i*c+j]<<"\t";
    }
    cout<<endl;
  }
  cout<<"\nRow sum:\n";
  for(i=0;i<r;i++)
  {
    cout<<"row "<<(i+1)<<": "<<rowsum[i]<<endl;
  }
  for(i=0;i<c;i++)
  {
    cout<<"column "<<(i+1)<<":"<<colsum[i]<<endl;
  }
  getch();
}

OUTPUT


Thursday, August 23, 2012

STACK IMPLEMENTATION USING OPERATOR OVERLOADING IN C++


Write a C++ program to create a class called STACK using an array of integers. Implement the following operations by overloading the operators + and – 
i) s1=s1+element; where s1 is an object of the class STACK and element is aninteger to be pushed on the top of the stack.
ii) s1=s1-; where s1 is an object of the class STACK - operator pops the element.
Handle the STACK empty and STACK full conditions. Also display the contents of the stack after each operation, by overloading the operator <<


#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
const int SIZE=5; //Stack size
//class declaration
class stack
{
   int items[SIZE];
   int top;
   int full();
   int empty();
   public:
   stack()
   {
      top=-1;
   }
   stack operator--(int);
   friend stack operator+(stack s1,int elem);
   friend ostream &operator<<(ostream &os,stack &s1);
};
// checking for Stack overflow
int stack::full()
{
   if(top==SIZE-1)
      return 1;
   else
      return 0;
}
//Checking for stack under flow.
int stack::empty()
{
   if(top==-1)
      return 1;
   else
      return 0;
}
//function for element deletion from the stack

stack stack::operator--(int )
{
   if(empty())
   {
     cout<<"Stack underflow\n";
   }
   else
   {
       cout<<"\nThe element deleted is :"<<items[top];
       stack t;
       t.top=--top;
       for(int i=0;i<=top;i++)
           t.items[i]=items[i];
   }
   return *this;
}
ostream &operator<<(ostream &os,stack &s1)
{
   for(int i=s1.top;i>=0;i--)
     os<<s1.items[i]<<"\n";
   return os;
}
//function for element insertion on to the stack
stack operator+(stack s1,int elem)
{
   if(s1.full())
     cout<<"\nStack overflow\n";
   else
     s1.items[++(s1.top)]=elem;
   return s1;
}
/*Main function*/
void main()
{
   stack s1;
   int choice,elem;
   clrscr();
   for(;;)
   {
     cout<<"\n1:PUSH 2:POP 3:DISPLAY 4:EXIT\n"
     <<"enter your choice:";
     cin>>choice;
     switch(choice)
     {
       case 1:
           cout<<"Enter the element to be inserted:";
           cin>>elem;
           s1=s1+elem;
           break;
       case 2:
           s1=s1--;
           break;
       case 3:
           cout <<"The contents of the stack are :\n"<<s1;
           break;
       case 4: exit(0);
       default: cout <<"Invalid choice\n";
       getch();
       exit(0);
     }
   }
}

output 


Tuesday, August 21, 2012

OPERATOR OVERLOADING - ADDITION, SUBTRACTION OF MATRICES USING OPERATOR OVERLOADING


Write a C++ program to create a class called MATRIX using a two dimensional array of integers.Implement the following operations by overloading the operator == which checks the compatibility of the two matrices to be added and subtracted. Perform the addition and subtraction by overloading the operators + and - respectively. Display the results by overloading operator <<.



#include<iostream.h>
#include<conio.h>
class matrix
{
  private:long m[5][5];
  int row;int col;
  public:void getdata();
  int operator ==(matrix);
  matrix operator+(matrix);
  matrix operator-(matrix);
  friend ostream & operator << (ostream &,matrix &);
};
/* function to check whether the order of matrix are same or not */
int matrix::operator==(matrix cm)
{
  if(row==cm.row && col==cm.col)
  {
    return 1;
  }
  return 0;
}
/* function to read data for matrix*/
void matrix::getdata()
{
  cout<<"enter the number of rows\n";
  cin>>row;
  cout<<"enter the number of columns\n";
  cin>>col;
  cout<<"enter the elements of the matrix\n";
  for(int i=0;i<row;i++)
  {
    for(int j=0;j<col;j++)
    {
       cin>>m[i][j];
    }
  }
}
/* function to add two matrix */
matrix matrix::operator+(matrix am)
{
  matrix temp;
  for(int i=0;i<row;i++)
  {
    for(int j=0;j<col;j++)
    {
      temp.m[i][j]=m[i][j]+am.m[i][j];
    }
    temp.row=row;
    temp.col=col;
  }
  return temp;
}
/* function to subtract two matrix */
matrix matrix::operator-(matrix sm)
{
  matrix temp;
  for(int i=0;i<row;i++)
  {
    for(int j=0;j<col;j++)
    {
      temp.m[i][j]=m[i][j]-sm.m[i][j];
    }
    temp.row=row;
    temp.col=col;
  }
  return temp;
}
/* function to display the contents of the matrix */
ostream & operator <<(ostream &fout,matrix &d)
{
  for(int i=0;i<d.col;i++)
  {
    for(int j=0;j<d.col;j++)
    {
      fout<<d.m[i][j];
      cout<<" ";
    }
    cout<<endl;
  }
return fout;
}
/* main function */
void main()
{
  matrix m1,m2,m3,m4;
  clrscr();
  m1.getdata();
  m2.getdata();
  if(m1==m2)
  {
    m3=m1+m2;
    m4=m1-m2;
    cout<<"Addition of matrices\n";
    cout<<"the result is\n";
    cout<<m3;
    cout<<"subtraction of matrices\n";
    cout<<"The result is \n";
    cout<<m4;
  }
  else
  {
    cout<<"order of the input matrices is not identical\n";
  }
  getch();
}

You might also like:

output


Wednesday, August 8, 2012

LARGEST IN AN ARRAY

Program to find the largest number in an array using pass by address and array passing.


#include<iostream.h>
#include<conio.h>
void large(int a[],int *n);
void main()
{
  int x[10],i,l;
  clrscr();
  cout<<"Enter ten numbers\n";
  for(i=0;i<10;i++)
  {
    cin>>x[i];
  }
  large(x,&l);
  cout<<"Largest number is "<<l;
  getch();
}
void large(int a[],int *n)
{
  int i;
  *n=a[0];
  for(i=0;i<10;i++)
  {
    if(a[i]>*n)
    {
      *n=a[i];
    }
  }
}

Output