Sunday 2 June 2013

// Create TWO 2-D arrays of size 3 by 3 with user defined values.
// Swap the contents of the two arrays. Display both arrays.


#include<iostream.h>
void main()
{
int a[3][3], b[3][3], i, j, temp;

// initializing 2 D array a with user defined values

for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
  {
    cout<<"enter value for a["<<i<<"]["<<j<<"]:";
    cin>>a[i][j];
  }
}

cout<<endl;

// initializing 2 D array b with user defined values

for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
  {
    cout<<"enter value for b["<<i<<"]["<<j<<"]:";
    cin>>b[i][j];
  }
}

cout<<endl;

//displaying array a

cout<<"displaying array a:"<<endl<<endl;

for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
  {
cout<<a[i][j]<<" ";
  }
  cout<<endl;
}

cout<<endl;

//displaying array b

cout<<"displaying array b:"<<endl<<endl;

for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
  {
cout<<b[i][j]<<" ";
  }
  cout<<endl;
}

cout<<endl;

// swapping 2 D array a and b

cout<<"copying array:"<<endl<<endl;

for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
  {
temp = a[i][j];
a[i][j] = b[i][j];
b[i][j] = temp;
  }
}

//displaying array a

cout<<"displaying array a after swapping:"<<endl<<endl;

for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
  {
cout<<a[i][j]<<" ";
  }
  cout<<endl;
}

cout<<endl;

//displaying array b

cout<<"displaying array b after swapping:"<<endl<<endl;

for(i=0;i<3;i++)
{
  for(j=0;j<3;j++)
  {
cout<<b[i][j]<<" ";
  }
  cout<<endl;
}

cout<<endl;

}

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!