Tuesday, February 13, 2024

20. WAP to implement Upper Triangular Matrix using one-dimensional array.

 20. WAP to implement Upper Triangular Matrix using one-dimensional array.


PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class utm{

            int arr[50][50],row,col,arr1[100];

            public:

                        utm(){

                                    row=0;

                                    col=0;

                        }

                        void getdata(){

                                    cout<<"ENTER THE VALUE OF THE ROW ......"<<endl;

                                    cin>>row;

                                    cout<<"ENTER THE VALUE OF THE COLOM ....."<<endl;

                                    cin>>col;

                                    cout<<"ENTER THE MATRIX ELEMENTS ........"<<endl;

                                    for(int i=0;i<row;i++){

                                                for(int j=0;j<col;j++)

                                                            cin>>arr[i][j];

                                    }

                                    if(check())

                                                cout<<"THE MATRIX IS A UPPER TRIANGULAR MATRIX ....."<<endl;

                                    else

                                                cout<<"THE MATRIX IS NOT A UPPER TRIANGULAR MATRIX ......"<<endl;

                        }

                        int check(){

                                    int flag1=0,flag2=0;

                                    for(int i=0;i<row;i++){

                                                for(int j=0;j<col;j++){

                                                            if(i<=j&&arr[i][j]==0)

                                                                        flag1++;

                                                            if(i>j&&arr[i][j]!=0)

                                                                        flag2++;

                                                }

                                    }

                                    if(flag1!=0||flag2!=0)

                                                return 0;

                                    else

                                                return 1;

                        }

                        void utm1(){

                                    int k=0,k2=0;

                                    int k1=row;

                                    while(row>0){

                                                k=k+row;

                                                row--;

                                    }

                                    row=k1;

                                    for(int i=0;i<row;i++){

                                                for(int j=0;j<col;j++){

                                                            if(arr[i][j]!=0){

                                                                        arr1[k2]=arr[i][j];

                                                                        k2++;

                                                            }

                                                }

                                    }

                                    cout<<"THE UPPER TRI-DIAGONAL ELEMENTS ARE -> "<<endl;

                                    for(int p=0;p<k;p++)

                                                cout<<arr1[p]<<" ";

                                    cout<<endl;

                        }

                        void display(){

                                    for(int i=0;i<row;i++){

                                                for(int j=0;j<col;j++)

                                                            cout<<arr[i][j]<<"  ";

                                                cout<<endl;

                                    }

                        }

};

int main(){

            int opt=0;

            utm ob;

            do{

                        cout<<"\nPRESS 0 TO EXIT ........."<<endl;

                        cout<<"PRESS 1 TO ENTER MATRIX ........"<<endl;

                        cout<<"PRESS 2 TO DISPLAY MATRIX ......"<<endl;

                        cout<<"PRESS 3 TO DISPLAY UPPER TRIANGULAR ELEMENTS ......."<<endl;

                        cin>>opt;

                        switch(opt){

                                    case 0:

                                                break;

                                    case 1:

                                                ob.getdata();

                                                break;

                                    case 2:

                                                ob.display();

                                                break;

                                    case 3:

                                                ob.utm1();

                                                break;

                                    default:

                                                cout<<"ERROR CHOICE............."<<endl;

                        }

            }while(opt!=0);

            return 0;

}

 

 

 

 

 

 

 


No comments:

Post a Comment