Tuesday 30 August 2011

You are required to write a program which takes two 3x3 matrix A and B containing 09 elements each and sort all these 18 elements in descending order and put it in one dimensional array and then display it.

Follow the following steps to code this program

1. Take two two-dimensional arrays as A[3][3]and B[3][3] and  one-dimensional array C[] to keep the result.

2. Prompt the user to enter the elements of first matrix

3. Prompt the user to enter the elements of second matrix

4. Now take first element of first matrix and check in both matrix weather it is greatest or any other number is the greatest and put the greatest number at the first index of the one dimensional array. Follow this procedure for other elements and at the end one dimensional array would be in descending order

5. Display this one-Dimensional sorted array on the screen


Following is the sample program structure



   2 3 5                2 8 7

A= 6 5 9             B= 0 1 1

   1 0 2                4 7 9



Output should be like this


Please enter the Elements of First Matrix:

Please enter the Elements of second Matrix:
 Sorted Values from Both the Matrices are:             

    ------------------------------------------------
     9 9 8 7 7 6 5 5 4 3 2 2 2 1 1 1 0 0
    ------------------------------------------------
Answer

#include<iostream.h>
// ---------- declaration of user defined function for merging and sorting array -----------
//user defined function for merging the arrays into one array
void merge(int [][3], int [][3], int[]);
//  user-defined function for sorting the merged array
void sort(int[], int);

//--------------    Start of the main function   -------------------
main()
{
   
 int A[3][3],B[3][3];   // declaration of 2D arrays
 int C[18];  // declaration of 1D array to store the values after merging


// prompting the user to enter the elements of first array 
 cout<<"Please enter the Elements of 1st Matrix\n\n";

 for(int row=0;row<3;row++)
 {
         for(int col=0;col<3;col++)
         {
                 cout <<"Enter the element for row "<<row+1 << ", column " <<col+1 <<":\t";
                 cin>>A[row][col];
         }
 }


 // prompting the user to enter the elements of second 2d array 
  cout<<"\n\nPlease enter the Elements of 2nd Matrix\n\n";

 for(int row=0;row<3;row++)
 {
         for(int col=0;col<3;col++)
         {
                 cout <<"Enter the element for row "<<row+1 << ", column " <<col+1 <<":\t";
                 cin>>B[row][col];
         }
 }

   cout<<"\n\nElements of 1st Matrix are:\n\n";

 for(int row=0;row<3;row++)
 {
         for(int col=0;col<3;col++)
         {
                 cout<<A[row][col];
                 cout<<"\t";
                
         }
        
         cout<<endl;
 }

    cout<<"\n\nElements of 1st Matrix are:\n\n";

 for(int row=0;row<3;row++)
 {
         for(int col=0;col<3;col++)
         {
                 cout<<B[row][col];
                 cout<<"\t";
                
         }
        
         cout<<endl;
 }
 //  calling user defined function for merging arrays
  merge(A, B, C);
 
 
 //  calling user defined function for sorting the resultant array
  sort(C, 18);

 
 // displaying the elements of sorted array on screen
  cout<<"\n\nSorted Values from Both the Matrices are:\n\n";
 
  for(int i=0; i<18; i++)
  {
     
       cout<<C[i];
       cout<<"\t";
       
  }

  cout<<endl<<endl;

  //  function to halt the output screen
 system("pause");

}

// function definition for merging the arrays
void merge(int Array1[][3], int Array2[][3], int mergeArray[])
{
     int insert = 0;
    
    
     for(int row=0; row<3; row++)
     {
          for(int col=0; col<3;col++)
          {
                 mergeArray[insert]=Array1[row][col];
                 insert++;
          }
     }
    
    

     for(int row=0; row<3; row++)
     {
         for(int col=0; col<3; col++)
         {
                 mergeArray[insert]=Array2[row][col];
                 insert++;
         }
     }
    

}
void sort(int Array[], int size)
{
      for(int row=0; row<size; row++)
  {
         for(int col=row; col<size; col++)
         {
                 if(Array[row]<Array[col])
                 {
                          int t;            
                          t = Array[row];
                          Array[row] = Array[col];
                          Array[col] = t;
                 }
         }
  }
    
}
   
 
 


No comments:

Post a Comment