diff --git a/mapping b/mapping new file mode 100644 index 00000000..d36c547a --- /dev/null +++ b/mapping @@ -0,0 +1,431 @@ +// PROGRAM:MAPPING OF 2 DIMENSIONAL MATRIX + +#include +using namespace std; +//m is number of rows. +//n is number of columns. +//a is a 2d array. +//bis a 1d array. +int i,j,k,m,n,a[10][10],b[100],found=0;; +int enter() //function to enter a matrix. +{ + cout<<"enter no. of rows:"; + cin>>m; + cout<<"\n enter no. of columns:"; + cin>>n; + cout<<"\n enter elements of matrix:"<>a[i][j]; + } + } + cout<<"\n entered matrix:"<1) && a[i][j]!=0) //if non tridiagonal elements are not 0 then found is equal to 0. + { + found=1; + } + if(k>=-1 || k<=1) //if i-j is greater than -1 and less than 1. then (i,j) is tridiagonal element. + { + switch(k) // switch case for lower diagonal,main diagonal and upper diagonal. + { + case 1: //for lower diagonal. + b[i-1]=a[i][j]; + break; + case 0: //for main diagonal. + b[n+i-1]=a[i][j]; + break; + case -1: //for upper diagonal. + b[2*n+i-1]=a[i][j]; + break; + } + } + } + } + //if found is 0 then display the new 1d array. + // if found is not 0 then display matrix is not tridiagonal. + if(found==0) + { + cout<<"\n entered matrix is tridiagonal."; + cout<<"\n new matrix:"< i*(i+1)/2 +j. + 2.column major + mapping formula-> (m*j) + i - (j*(j+1)/2) +*/ +int lower_triangle() //function to check whether matrix is lower triangular or not. +{ + int rc; + found=0; + //matrix should be a square matrix. + if(m==n) + { + //switch case to choose how to display 1d array(row major or column major). + cout<<"1.row major"<>rc; + switch(rc) + { + case 1: //for row major lower triangular matrix + for(i=0;i=j) //for lower triangle elements if condition is correct convert 2d array into 1d array. + { + k=(i*(i+1)/2 + j); //formula for row major mapping of upper triangular matrix. + b[k]=a[i][j]; + } + } + } + //if found is 0 display the new 1d array in row major. + //if found is not 0 display matrix is not lower triangular. + if(found==0) + { + cout<<"\n matrix is lower triangular"; + cout<<"\n new matrix row major:"<=j) //for lower trianle elements if condition is correct convert 2d array into 1d array. + { + k=((m*j) + i - j*(j+1)/2); //formula for column major mapping of upper triangular matrix + b[k]=a[i][j]; + } + } + } + // if found is 0 display the new 1d array in column major. + //if found is not 0 display matrix is not lower triangular + if(found==0) + { + cout<<"\n matrix is lower triangular"; + cout<<"\n new matrix column major:"<(n*i) + j - i*(i+1)/2. + 2.column major + mapping formula->j*(j+1)/2 + i. +*/ +int upper_triangle() //function to check whether matrix is upper triangular or not. +{ + int rc; + found=0; + //matrix should be a square matrix. + if(m==n) + { + //switch case to choose how to display 1d array(row major or column major). + cout<<"1.row major"<>rc; + switch(rc) + { + case 1: //for row major upper triangular matrix + for(i=0;ij && a[i][j]!=0) //if non upper triangle elements are not 0 then found is 1. + found=1; + if(i<=j) //for upper triangular elements if condition is correct convert 2d array into 1d array. + { + k=((n*i) + j - i*(i+1)/2); //formula for row major mapping of upper triangular matrix. + b[k]=a[i][j]; + } + } + } + //if found is 0 display new 1d array in row major. + //if found is 1 display matrix is not upper triangular. + if(found==0) + { + cout<<"\n matrix is upper triangular"; + cout<<"\n new matrix row wise:"; + for(k=0;kj && a[i][j]!=0) //if non upper triangle elements are not 0 then found is 1. + found=1; + if(i<=j) //for upper triangular elements if condition is correct convert 2d array into 1d array. + { + k=(j*(j+1)/2 + i); //formula for column major mapping of upper triangular matrix. + b[k]=a[i][j]; + } + } + } + //if found is 0 display new 1d array in row major. + //if found is 1 display matrix is not upper triangular. + if(found==0) + { + cout<<"\n matrix is upper triangular"; + cout<<"\n new matrix column wise:"; + for(k=0;k>ch; + switch(ch) //switch case to choose between operation. + { + menu(); + case 1:enter(); //to enter a mXn matrix + break; + case 2:diagonal(); //to find whether matrix is diagonal or not + break; + case 3:lower_triangle(); //to find whether matrix is lower triangular or not + break; + case 4:upper_triangle(); //to find whether matrix is upper triangular or not + break; + case 5:symmetric(); //to find whether matrix is symmetric or not + break; + case 6:tridiagonal(); //to find whether matrix is tridiagonal or not + break; + default: //in case user enters wrong number. + cout<<"\n enter from menu."; + break; + } + cout<<"\n want to enter more(y/n)?"; //if user want to choose again. + cin>>ch1; + if(ch1=='y' || ch1=='Y') + cout<<"\n enter again:"<