Friday, March 1, 2013

C++ Array Codes


Write a C++ program to swap first and last element of an integer 1-d array.


#include<iostream.h>
#include<conio.h>

int main()
{
 int Arr[100],n,temp;
 cout<<"Enter number of elements you want to insert ";
 cin>>n;

 for(int i=0;i<n;i++)
 {
  cout<<"Enter element "<<i+1<<":";
  cin>>Arr[i];
 }

 temp=Arr[0];
 Arr[0]=Arr[n-1];
 Arr[n-1]=temp;

 cout<<"\nArray after swapping"<<endl;

 for(i=0;i<n;i++)
  cout<<Arr[i]<<" ";

 getch();
 return 0;
}

******************************************************

Write a C++ program to find the sum and average of one dimensional integer array.

#include<iostream.h>
#include<conio.h>

int main()
{
 int Arr[100],n,sum=0;
 cout<<"Enter number of elements you want to insert ";
 cin>>n;

 for(int i=0;i<n;i++)
 {
  cout<<"Enter element "<<i+1<<":";
  cin>>Arr[i];
 }

 for(i=0;i<n;i++)
  sum+=Arr[i];

 cout<<"\nThe sum of Array is :"<<sum;
 cout<<"\nThe average of Array is :"<<sum/i;

 getch();
 return 0;
}


******************************************************

Write a C++ program to reverse the element of an integer 1-D array.


#include<iostream.h>
#include<conio.h>

int main()
{
 int Arr[100],n,temp,i,j;
 cout<<"Enter number of elements you want to insert ";
 cin>>n;

 for(i=0;i<n;i++)
 {
  cout<<"Enter element "<<i+1<<":";
  cin>>Arr[i];
 }

 for(i=0,j=n-1;i<n/2;i++,j--)
 {
  temp=Arr[i];
  Arr[i]=Arr[j];
  Arr[j]=temp;
 }

 cout<<"\nReverse array"<<endl;

 for(i=0;i<n;i++)
  cout<<Arr[i]<<" ";

 getch();
 return 0;
}
******************************************************

P is one-dimensional array of integers. Write a C++ function to efficiently search for a data VAL from P. If VAL is present in the array then the function should return value 1 and 0 otherwise.


#include<iostream.h>
#include<conio.h>

int lsearch(int Arr[], int s, int VAL);

int main()
{
 int Arr[100],n,val,found;
 cout<<"Enter number of elements you want to insert ";
 cin>>n;
 for(int i=0;i<n;i++)
 {
  cout<<"Enter element "<<i+1<<":";
  cin>>Arr[i];
 }

 cout<<"Enter the number you want to search ";
 cin>>val;

 found=lsearch(Arr,n,val);

 if(found==1)
  cout<<"\nItem found";
 else
  cout<<"\nItem not found";

 getch();
 return 0;
}

int lsearch(int Arr[], int s, int VAL)
{
 for(int I=0; I<s; I++)
 {
  if(Arr[I]==VAL)
   return 1;
 }
 return 0;
}

******************************************************

Suppose X. Y, Z are arrays of integers of size M, N, and M + N respectively. The numbers in array X and Y appear in descending order. Write a user-defined function in C++ to produce third array Z by merging arrays X and Y in descending order.

#include<iostream.h>
#include<conio.h>

void Merge(int A[], int B[], int C[], int N, int M, int &K);

int main()
{
 int A[100], B[100], C[200],n,m,k;
 cout<<"\nEnter number of elements you want to insert in first array ";
 cin>>n;
 cout<<"Enter element in descending order\n";
 for(int i=0;i<n;i++)
 {
  cout<<"Enter element "<<i+1<<":";
  cin>>A[i];
 }

 cout<<"\nEnter number of elements you want to insert in second array ";
 cin>>m;

 cout<<"Enter element in descending order\n";
 for(i=0;i<m;i++)
 {
  cout<<"Enter element "<<i+1<<":";
  cin>>B[i];
 }

 Merge(A,B,C,n,m,k);

 cout<<"The Merged Array in Descending Order"<<endl;
 for(i=0;i<k;i++)
 {
  cout<<C[i]<<" ";
 }

 getch();
 return 0;
}

void Merge(int A[], int B[], int C[], int N, int M, int &K)
{
      int I=0, J=0;
      K=0;
      while (I<N && J<M)
      {
     if (A[I]>B[J])
    C[K++]=A[I++];
     else if (A[I]<B[J])
    C[K++]=B[J++];
     else
     {
    C[K++]=A[I++];
    J++;
     }
      }
      for (int T=I;T<N;T++)
     C[K++]=A[T];
      for (T=J;T<M;T++)
     C[K++]=B[T];

}


No comments:

Post a Comment