C++

Tuesday, 9 October 2012

Array Basic Operations(code):

//Code for Array-Baisc Operations:
#include <iostream.h>
const int size = 5;
//Array class:
class Array
{
private:
          int a[size];
          int n; // no of elements.
public:
//constructor
          Array()
          {
                   for (int i=0;i<size;i++)
                             a[i] = 0;
                   n = 0;
          }
// Input function
          void read()
          {
                   int n_val;
                   cout << "Enter number of values you want to enter";
                   cin >> n_val;
                   for(int i=0;i<n_val;i++)
                   {
                             cout<<"enter element at"<<i<<"index";
                             cin>>a[i];
                        n++;
                   }
          }
// Function to show output
          void display()
          {
                   for (int i=0;i<n;i++)
                             cout << a[i]<< endl;
          }
//function to delete an element from any index
          void del()
          {
                   int index;
                   cout << "Enter index from which you want to delete the value";
                   cin >> index;
                   for (int i=index;i<n-1;i++)
                   {
                             a[i] = a[i+1];
                   }
                   n--;
                   display();
          }
// function to insert an element at any index of array
          void insert()
          {
                   int index;
                   cout << "Enter index where you want to insert";
                   cin >> index;
                   for (int i=n;i>index;i--)
                   {
                             a[i] = a[i-1];
                   }
                   cout << "Enter Value";
                   cin >> a[i];
                   n++;
                   display();
          }
// calculate sum of elements of Array
          int sum()
          {
                   int sum=0;
                   for (int i=0;i<n;i++)
                   {
                             sum=sum+a[i];
                   }
                   cout<<"Sum of elements of Array=";
                   return sum;
          }
// function to calculate average of the elements of an array
          void average()
          {
                   float avg;
                   float s= sum();
                   avg= s / n;
                   cout<<"Average of the elements of Array="<<avg<<endl;
          }
//function to calculate sum of even elements of Array
          void sum_even()
          {
                   int s_even=0;
                   for (int i=0;i<n;i++)
                   {
                             if(a[i]%2==0)
                                      s_even=s_even+a[i];
                   }
                   cout<<"Sum of even elements of the Array="<<s_even<<endl;
          }
//function to calculate sum of odd elements of Array
          void sum_odd()
          {
                   int s_odd=0;
                   for (int i=0;i<n;i++)
                   {
                             if(a[i]%2!=0)
                                      s_odd=s_odd + a[i];
                   }
                   cout<<"Sum of the odd elements of Array="<<s_odd<<endl;
          }
//function to find maximum element of an array
          void max()
          {
                   int max=0;
                   for (int i=0;i<n;i++)
                   {
                             if(a[i]>=max)
                                      max=a[i];
                   }
                   cout<<"Maximum element="<<max<<endl;
          }
          //function to find minimum element of an array
          void min()
          {
                   int min=0;
                   for (int i=0;i<n;i++)
                   {
                             if(a[i]<=min)
                                      min=a[i];
                   }
                   cout<<"Minimum element="<<min<<endl;
          }
//function to sort element of an array using bubble sort
          void bubble_sort()
          {
                   for (int i=0;i<n-1;i++)
                             for (int j= 0;j<n-i-1;j++)
                                      if (a[j+1] < a[j])
                                      {
                                                int temp = a[j];
                                                a[j] = a[j+1];
                                                a[j+1] = temp;
                                      }
          }

// Function to search any element using linear search
          void linear_srch()
          {
                   bool found=false;
                   int val;
                   cout<<"enter value you want to find:";
                   cin>>val;
                   for (int i= 0; i< n && found!=true; i++)
                   {
           if (val == a[i])
              found = true;
                   }
                   if(found==true)
                   {
                             cout<<"value \""<<a[i]<<"\" found at"<<i<<"index"<<endl;
                   }
                   else
                   {
                             cout<<"value not found";
                   }
          }
// Function to search any element using binary search
          void binary_srch()
          {
                   bool found=false;
                   int val, mid, low=0, high= n-1;
                   cout<<"enter value you want to find:";
                   cin>>val;
                      while (low<= high && found!=true)
                   {
                           mid = (low + high)/2;
          
                         if (val == a[mid])
                                 found = true;
                           else if (val < a[mid])
                                 high = mid -1;
                           else
                                 low = mid + 1;
                   }
                   if(found==true)
                   {
                             cout<<"value \""<<a[mid]<<"\" found at"<<mid<<"index"<<endl;
                   }
                   else
                             cout<<"value not found";
          }
//function to merge two arrays and store the merged elements in a new array
          void merge(Array a1, Array a2)
          {
                   int i=0,j=0;
                   while (i< a1.n && j< a2.n )
                   {
                             if (a1.a[i] < a2.a[j])
                             {
                                      a[n] = a1.a[i];
                                      i++;
                             }
                             else
                             {
                                      a[n] = a2.a[j];
                                      j++;
                             }
                             n++;

                   }
                   while (i < a1.n)
                   {
                             a[n] = a1.a[i];
                             i++; n++;
                   }
                   while (j < a2.n)
                   {
                             a[n] = a2.a[j];
                             j++; n++;
                   }
          }
};
//Main Function:
void main()
{
          Array a1,a2,a3;
          a1.read();
          a1.display();
          a2.read();
          a2.display();
          a2.insert();
          a1.sum();
          a1.max();
          a1.min();
          a2.average();
          a2.sum_even();
          a2.sum_odd();
          a1.bubble_sort();
          a1.binary_srch();
          a2.linear_srch();
          a1.del();
          a3.merge(a1,a2);
          a3.display();
}

No comments:

Post a Comment