Friday, April 9, 2021

Implement a class CppArray which is identical to a one-dimensional C++ array (i.e., the index set is a set of consecutive integers starting at 0) except for the following : 1. It performs range checking. 2. It allows one to be assigned to another array through the use of the assignment operator (e.g. cp1=cp2) 3. It supports a function that returns the size of the array. 4. It allows the reading or printing of array through the use of cout and cin

 #include<iostream>

using namespace std;

class CppArray

{

   private:

   int a[10],i,j,n,b[10],temp;

   public:

   void get();

   void print();

   void sort();

   void range();

   void exchange();

   int size();

};

void CppArray::get()

{

   cout<<"\n Enter the limit of array ";

   cin>>n;

   cout<<"\n Enter the array elements ";

   for(i=0;i<n;i++)

   {

       cout<<"\n a["<<i<<"]=";

       cin>>a[i];

   }

}

void CppArray::print()

{

   cout<<"\n The entered array is ";

   for(i=0;i<n;i++)

   {

       cout<<"\n a["<<i<<"]="<<a[i];

   }

}

void CppArray::sort()

{

   for(j=0;j<n;j++)

   {

      for(i=0;i<n-1;i++)

      {

         if(a[i]>a[i+1])

         {

            temp=a[i+1];

            a[i+1]=a[i];

            a[i]=temp;

         }

      }

   }

   cout<<"\n The sorted array is ";

   for(i=0;i<n;i++)

   {

       cout<<"\n a["<<i<<"]="<<a[i];

   }

}

void CppArray::range()

{

   cout<<"\n The range of array is from "<<a[0]<<" to "<<a[n-1];

}

void CppArray::exchange()

{

   for(i=0;i<n;i++)

   {

       b[i]=a[i];

   }

   cout<<"\n The exchanged array is ";

   for(i=0;i<n;i++)

   {

       cout<<"\n b["<<i<<"]="<<b[i];

   }

}

int CppArray::size()

{

   return n;

}

int main()

{

   int a;

   CppArray obj;

   obj.get();

   obj.print();

   obj.range();

   obj.exchange();

   obj.sort();

   a=obj.size();

   cout<<"\n The size of array is "<<a;

   return 0;

}

************output*****************


PS F:\harshal\MCA\opps\assignment 3> cd "f:\harshal\MCA\opps\assignment 3\" ; if ($?) { g++ ass3.cpp -o ass3 } ; if ($?) { .\ass3 }


 Enter the limit of array 4


 Enter the array elements 

 a[0]=3


 a[1]=2


 a[2]=4


 a[3]=5


 The entered array is

 a[0]=3

 a[1]=2

 a[2]=4

 a[3]=5

 The range of array is from 3 to 5

 The exchanged array is

 b[0]=3

 b[1]=2

 b[2]=4

 b[3]=5

 The sorted array is

 a[0]=2

 a[1]=3

 a[2]=4

 a[3]=5

 The size of array is 4 

**//

No comments:

Post a Comment