The trouble is you think you have time. Your success depends on how you utilize your time.

C++ Program which copy all even numbers from first Array A to third Array C from left to right and copy all odd numbers from right to left and do this same thing for the second Array B and copy it into third Array C.


Given two arrays of integers A and B of sizes M and N respectively. Write a C++ program which will produce a third array named C. Such that the following sequence is followed.
a. All even numbers of A from left to right are copied into C from left to right.
b. All odd numbers of A from left to right are copied into C from right to left.
c. All even numbers of B from left to right are copied into C from left to right.
d. All odd numbers of B from left to right are copied into C from right to left.



#include<iostream>
using namespace std;
int main ()
{ cout<<"[www.moznum.blogspot.com]"<<endl<<endl;



int soa,sob,soc,a,b;   // Here "soa", "sob" & "soc" are the size of array.
cout<< "Please Enter the Length Of array A: ";
cin>>soa;
cout<<endl;
cout<< "Please Enter the Length Of array B: ";
cin>>sob;
int A[soa];     //Creating Array of the size of A.
int B[sob]; //Creating Array of the size of B.
soc=soa+sob; //Creating Array By Adding both size of A & B.
int C[soc];
cout<<endl<< "Please Enter the values in array A: ";
cout<<endl;
for(int i=1;i<=soa;i++)
{
cin>>A[i];   // Taking input in array A.

   }

cout<<endl;
cout<< "Please Enter the values in array B: ";
cout<<endl;
for(int i=1;i<=sob;i++)
{
cin>>B[i]; // Taking input in array B.

   }
cout<<endl;


a=1;       //here "a" is use to store even values in array C from left to right ,starting from first index of array C.
b=soc; //here "b" is use to store odd values in array C from right to left ,starting from last index of array C.
cout<<"Now the array C is= "<<endl;
for(int i=1;i<=soa;i++)
{
     if(A[i]%2==0)
      {
      C[a]=A[i];
      a++;
      }
             else
             {
              C[b]=A[i];
              b--;
             }
   }

for(int i=1;i<=sob;i++)  // This loop is started right after where the last loop done its work.
{
     if(B[i]%2==0)
      {
      C[a]=B[i];
      a++;
      }
             else
             {
              C[b]=B[i];
              b--;
             }
   }

for(int i=1;i<=soc;i++)  // This loop is for printing.
{
cout<<C[i]<<endl;

   }
}

4 comments:

  1. how to do this using a function

    ReplyDelete
    Replies
    1. They just you will not declare any of the array if you are not making the main function array will be passed under as arguments in the function

      Delete
  2. but you havent declared the value of 'soc' before using it in the loop . hence it will show an error.. please correct it

    ReplyDelete
  3. kiya baat hai janab kamal kar diya

    ReplyDelete