Tuesday, February 13, 2024

19. WAP to implement Lower Triangular Matrix using one-dimensional array.

 19. WAP to implement Lower Triangular Matrix using one-dimensional array. 


PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class ltm{

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

                public:

                                ltm(){

                                                row=0;

                                                col=0;

                                }

                                void getdata(){

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

                                                cin>>row;

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

                                                cin>>col;

                                                cout<<"ENTER THE VALUES 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 A LOWER TRIDIAGONAL MATRIX....."<<endl;

                                                else

                                                                cout<<"THE MATRIX IS NOT A LOWER TRIDIAGONAL MATRIX....."<<endl;

                                }

                                int check(){

                                                int flag1=0,flag2=0;

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

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

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

                                                                                                flag1++;

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

                                                                                                flag2++;

                                                                }

                                                }

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

                                                                return 0;

                                                else

                                                                return 1;

                                }

                                void ltm1(){

                                                int k=0,k2=0;

                                                int k1=row;

                                                while(row>=1){

                                                                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++;

                                                                                }

                                                                }

                                                }

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

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

                                                cout<<endl;

                                }

                                void display(){

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

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

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

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

                                                                cout<<endl;

                                                }

                                }

};

int main(){

                ltm ob;

                int opt=0;

                do{

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

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

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

                                cout<<"PRESS 3 TO DISPLAY TRIDIAGONAL ELEMENTS......"<<endl;

                                cin>>opt;

                                switch(opt){

                                                case 0:

                                                                break;

                                                case 1:

                                                                ob.getdata();

                                                                break;

                                                case 2:

                                                                ob.display();

                                                                break;

                                                case 3:

                                                                ob.ltm1();

                                                                break;

                                                default:

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

                                }

                }while(opt!=0);

                return 0;  }


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;

}

 


17. WAP to reverse the order of the elements in the stack using additional Queue.

 17. WAP to reverse the order of the elements in the stack using additional Queue. 

PROGRAME CODE

#include<stdio.h>

#include<iostream>

using namespace std;

class stack_rev{

            int *arr,top,size,*arr1,front,rear;

            public:

                        stack_rev(){

                                    arr=NULL;

                                    arr1=NULL;

                                    top=-1;

                                    size=0;

                                    front=0;

                                    rear=-1;

                        }

                        void getdata(){

                                    cout<<"ENTER THE LENGTH OF THE STACK ->"<<endl;

                                    cin>>size;

                                    arr=new int[size];

                                    arr1=new int[size];

                        }

                        void push(){

                                    if(top==(size-1))

                                                cout<<"THE STACK OVERFLOW...."<<endl;

                                    else{

                                                top++;

                                                cout<<"ENTER THE DATA ->"<<endl;

                                                cin>>arr[top];

                                    }

                        }

                        void stack_rev1(){

                                    if(rear==(top-1)&&front==0)

                                                cout<<"THE QUEUE IS OVER FLOW...."<<endl;

                                    else{

                                                while(top>=0){

                                                            rear++;

                                                            arr1[rear]=arr[top];

                                                            top--;

                                                }

                                    }

                        }

                        void display(){

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

                                    int x=top+1;

                                    while(--x>=0)

                                                cout<<arr[x]<<endl;

                        }

                        void display1(){

                                    cout<<"THE REVERSE STACK ->"<<endl;

                                    int x=rear+1;

                                    while(--x>=front)

                                                cout<<arr1[x]<<endl;

                        }

};

int main(){

            stack_rev ob;

            int opt=0;

            do{

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

                        cout<<"PRESS 1 TO SET STACK LENGTH ......"<<endl;

                        cout<<"PRESS 2 TO ENTER DATA IN STACK .........."<<endl;

                        cout<<"PRESS 3 TO DISPLAY THE STACK......."<<endl;

                        cout<<"PRESS 4 TO REVERSE THE STACK ......"<<endl;

                        cout<<"PRESS 5 TO PRINT THE REVERSE OF THE STACK..........."<<endl;

                        cin>>opt;

                        switch(opt){

                                    case 0:

                                                break;

                                    case 1:

                                                ob.getdata();

                                                break;

                                    case 2:

                                                ob.push();

                                                break;

                                    case 3:

                                                ob.display();

                                                break;

                                    case 4:

                                                ob.stack_rev1();

                                                cout<<"REVERSE SUCESSFULL ....."<<endl;

                                                break;

                                    case 5:

                                                ob.display1();

                                                break;

                                    default:

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

                        }

            }while(opt!=0);

            return 0;

}