Chapter 6: Multidimensional Arrays
A sparse matrix is a matrix in which most of the elements are zero
A Multidimensional array is represented using the type name[capacity][capacity]...[capacity] format.
A 2d array is just an collection of arrays.
A 3d array is just an array of array[s] of array[s]. Collection of 2d arrays
Example: arr[4][4][4]
Is a array that consists of 4 - 2d arrays , where each 2d arrays consists of 4 - 1d array and each 1d array contains 4
elements .
Memory Representation of Multidimensional Arrays
Row-Major representation: is where the a complete row is stored fisrt in the memory
Column-Major representation: is where the complete column is stored in the memory
C, C++ , Java , python uses Row-major
Fortran, Matlab, R uses Column-Major
When a function calls another variable the second dimension must be specified in C in the called
function's paramters
Addition and Subtraction of 2d Arrays
download me
#include<stdio.h>
int main(){
int a[10][10],b[10][10],i,j,n,m,x,y;
printf("Enter the rows and columns for matrix A: ");
scanf("%d %d",&n,&m);
printf("Enter the rows and columns for matrix B: ");
scanf("%d %d",&x,&y);
if(n!=x || m!=y){
printf("Addition / Subtraction is not possible");
return 0;
}
printf("\n\n\n\n Matrix 1 \n\n\n");
for(i=0;i<n;i++){
printf("\n\t\t\t At Row %d \n\n",i+1);
for(j=0;j<m;j++){
printf("Enter element %d ",j+1);
scanf("%d",&a[i][j]);
}
}
printf("\n\n\n\nMatrix 2: \n\n\n");
for(i=0;i<x;i++){
printf("\n\t\t\t At Row %d \n\n",i+1);
for(j=0;j<y;j++){
printf("Enter element %d ",j+1);
scanf("%d",&b[i][j]);
}
}
printf("\nA \t\tB \t\t Sum \t\tSub\n");
for(i =0;i<n;i++){
for(j=0;j<m;j++){
printf("%d ",a[i][j]);
}
printf("\t\t");
for(j=0;j<m;j++){
printf("%d ",b[i][j]);
}
printf("\t\t");
for(j=0;j<m;j++){
printf("%d ",a[i][j]+b[i][j]);
}
printf("\t\t");
for(j=0;j<m;j++){
printf("%d ",a[i][j]-b[i][j]);
}
printf("\n");
}
return 0;
}
Multiplication of 2D arrays
download me
#include<stdio.h>
int main(){
int a[10][10],b[10][10],i,j,n,m,x,y;
printf("Enter the rows and columns for matrix A: ");
scanf("%d %d",&n,&m);
printf("Enter the rows and columns for matrix B: ");
scanf("%d %d",&x,&y);
if(n!=x || m!=y){
printf("Multiplication is not possible");
return 0;
}
printf("\n\n\n\n Matrix 1 \n\n\n");
for(i=0;i<n;i++){
printf("\n\t\t\t At Row %d \n\n",i+1);
for(j=0;j<m;j++){
printf("Enter element %d ",j+1);
scanf("%d",&a[i][j]);
}
}
printf("\n\n\n\nMatrix 2: \n\n\n");
for(i=0;i<x;i++){
printf("\n\t\t\t At Row %d \n\n",i+1);
for(j=0;j<y;j++){
printf("Enter element %d ",j+1);
scanf("%d",&b[i][j]);
}
}
int sum=0;
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
sum+=a[k][j]*b[j][i];
}
printf("%d ",sum);
sum=0;
}
printf("\n");
}
return 0;
}
;
Sparse Matrix P.S: its Transpose
download me
#include<;stdio.h>
int main(){
int i,s,j,count=0,n,m;
printf("Enter the order of the matrix: \n");
scanf("%d %d",&n,&m);
int arr[n][m];
int sm[n][m];
int tm[n][m];
for(i=0;i<;n;i++){
printf("\n\n **** IN ROW %d **** \n\n",i+1);
for(j=0;j<;m;j++){
printf("Enter element no %d: \n",j+1);
scanf("%d",&arr[i][j]);
if(arr[i][j]==0){
count++;
}
}
}
if (count>((n*m)/2)){
printf("This is a sparse matrix! ");
for(i=0;i<;n;i++){
for(j=0;j<;m;j++){
if(arr[i][j]!=0){
sm[s][0]=i;
sm[s][1]=j;
sm[s][2]=arr[i][j];
tm[s][0]=j;
tm[s][1]=i;
tm[s][2]=arr[i][j];
s++;
}
}
}
printf("\n\nThe non - zero Entries [a.k.a: sparse matrix] are: \n");
for(i=0;i<;s;i++){
for(j=0;j<;3;j++){
printf("%d ",sm[i][j]);
}
printf("\n");
}
printf("\n\n The Transpose matrix is: \n");
for(i=0;i<;s;i++){
for(j=0;j<;3;j++){
printf("%d ",tm[i][j]);
}
printf("\n");
}
}
else{
printf("This is not a sparse matrix! ");
}
return 0;
}
Linked Lists