Tuesday, February 13, 2024

18. WAP to implement Diagonal Matrix using one-dimensional array.

 18. WAP to implement Diagonal Matrix using one-dimensional array. 


PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class dia{

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

                public:

                                dia(){

                                                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 DATA OF THE MATRIX ->"<<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 DIAGONAL ......."<<endl;

                                                else

                                                                cout<<"THE MATRIX IS NOT DIAGONAL ....."<<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 1;

                                                else

                                                                return 0;

                                }

                                void diaarray(){

                                                if(check()){

                                                                int j=0,i=0;

                                                                while(i<row){

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

                                                                                i++;

                                                                                j++;

                                                                }

                                                }

                                                else

                                                                cout<<"THE MATRIX IS NOT A DIAGONAL MATRIX ......"<<endl;

                                }

                                void display(){

                                                cout<<"THE ORIGINAL MATRIX IS ->"<<endl;

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

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

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

                                                                cout<<endl;

                                                }

                                }

                                void display1(){

                                                cout<<"THE DIAGONAI ELEMENTS ->"<<endl;

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

                                                                cout<<arr1[i]<<"  ";

                                                cout<<endl;

                                }

};

int main(){

                dia ob;

                int opt=0;

                do{

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

                                cout<<"PRESS 1 TO CREAT A MATRIX......."<<endl;

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

                                cout<<"PRESS 3 TO DISPLAY THE DIAGONAL ELEMENTS ......"<<endl;

                                cin>>opt;

                                switch(opt){

                                                case 0:

                                                                break;

                                                case 1:

                                                                ob.getdata();

                                                                break;

                                                case 2:

                                                                ob.display();

                                                                break;

                                                case 3:

                                                                ob.diaarray();

                                                                ob.display1();

                                                                break;

                                                default:

                                                                cout<<"IT IS A ERROR CHOOICE ....."<<endl;

                                }

                }while(opt!=0);

                return 0;

}

 


No comments:

Post a Comment