Tuesday, 19 May 2015

Break Array Into Two Arrays Using C++

divide array in c++
Problem Description: Write a code in C++ that will break the array into two arrays.

In this program we created an array of size 10 by giving values and break this array into two arrays of size 5.



#include<iostream>
using namespace std;
int main()
{int n=10;
    int arr[]={2,4,6,7,4,3,9,0,1,5};
    int arr1[n/2],arr2[n/2];
    cout<<"Original Array of size 10 \n\n";
    for(int i=0;i<n;i++)
        cout<<arr[i]<<"\t";    
    cout<<endl<<endl;
    for(int i=0,j=(n/2);i<n/2;i++,j++)
    {
       arr1[i]=arr[i];
       arr2[i]=arr[j];  
    }
    cout<<"\tnew Array1 of size 5 \n\n";
    for(int i=0;i<n/2;i++)
        cout<<arr1[i]<<"\t";
     cout<<endl<<endl;
     cout<<"\tnew Array2 of size 5 \n\n";
     for(int i=0;i<n/2;i++)
        cout<<arr2[i]<<"\t";    
     
     cout<<endl<<endl;
     system("pause");    
}

0 comments:

Post a Comment