Wednesday 10 October 2018

Operation with dynamically 2-D arrays


Write a menu driven program to do following operation on two dimensional arrays dynamically created at run time depending upon user’s requirement. Take the size of arrays from user. The size of first array is nxl, the size of second array is lxm. The arrays need not to be only square in size. 
 

1. Add the contents of the two array A and B and store it in third array C.  
2. Multiply array A and B and store the results in another array D 
3. Upper-half which takes a two dimensional array A, with size N rows and N columns as argument and prints the upper half of the array including diagonal. Here is an example,
   12 13 11 15 10                           12 13 11 15 10
   27 21 25 23 21                                21 25 23 21
   32 35 37 38 31                                     37 38 31
   40 41 45 40 41                                          40 41
   53 54 59 51 55                                               55
4. Lower-half which takes a two dimensional array A, with size N rows and N columns as argument and prints the lower half of the array excluding diagonal in the array.


Program:


#include <iostream>

using namespace std;

void sum(int **A,int **B,int row,int col);
void multi(int **A,int **B,int row,int col);
void Upper_half(int **A,int row,int col);
void lower_half(int **A,int row,int col);

void sum(int **A,int **B,int row,int col)
{
    int **C;

     C=new int*[row];
    for(int i=0;i<5;i++)
     C[i]=new int[col];
     // summing
      for(int r=0;r<row;r++)
            {
                for(int c=0;c<col;c++)
                {
                    C[r][c]=(A[r][c])+(B[r][c]);
                }
            }

            // Printing array 'C' elements
    cout<<" \n Sum of two array 'A' & 'B' in to array 'C' :"<<endl<<endl;
            for(int r=0;r<row;r++)
            {
                for(int c=0;c<col;c++)
                {
                cout<<C[r][c]<<"    ";
                }
            cout<<endl;
            }
}

void multi(int **A,int **B,int row,int col)
{
    int **D;

     D=new int*[row];
    for(int i=0;i<5;i++)
     D[i]=new int[col];

     // Multiplying Two arrays
            for(int r=0;r<row;r++)
            {
                for(int c=0;c<col;c++)
                {
                    D[r][c]=(A[r][c])*(B[r][c]);
                }
            }

            // Printing array 'D' elements

            cout<<" \n Multiplaying Array 'A' & 'B' and saving into array 'D':"<<endl<<endl;
            for(int r=0;r<row;r++)
            {
                for(int c=0;c<col;c++)
                {
                cout<<D[r][c]<<"    ";
                }
            cout<<endl;
            }

}

void Upper_half(int **A,int row,int col)
{
         cout<<" \n Upper-half including triangle of 2d array is:"<<endl<<endl;
          int i, j;

    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (i > j)
            {
                cout << "    ";
            }
            else
            cout <<A[i][j] << "   ";
        }
        cout << endl;
    }
}
void lower_half(int **A,int row,int col)
{
    cout<<" \n Upper-half including triangle of 2d array is:"<<endl<<endl;
          int i, j;

           for (i = 0; i < row; i++)
            {
                for (j = 0; j < col; j++)
                    {
                        if (i<j)
                        {
                            cout << "    ";
                        }
                        else
                            cout <<A[i][j] << "   ";
                    }
            cout << endl;
            }
}

int main()
{
    int **A;
    int **B;

    int row,col;
    cout<<"Enter the number of rows:"<<endl;
    cin>>row;
    cout<<"Enter the number of columns:"<<endl;
    cin>>col;

    A=new int*[row];
    for(int i=0;i<5;i++)
        A[i]=new int[col];

        B=new int*[row];
    for(int i=0;i<5;i++)
        B[i]=new int[col];

cout<<"Enter value for rows and colums of array 'A' :"<<endl;
         for(int r=0;r<row;r++)
            {
                for(int c=0;c<col;c++)
                {
                    cin>>A[r][c];
                }
            }

cout<<"Enter value for rows and colums of array 'B' :"<<endl;
         for(int r=0;r<row;r++)
            {
                for(int c=0;c<col;c++)
                {
                    cin>>B[r][c];
                }
            }

sum(A,B,row,col);
multi(A,B,row,col);
Upper_half(A,row,col);
lower_half(A,row,col);

    return 0;
}

0 comments:

Post a Comment