Tuesday, January 30, 2024

Arrays - 2D Array/Matrix - DSTC using C Language (Source Code Implemented)

Arrays - 2D Array/Matrix
Implemented using C Language


Two-dimensional arrays are a little more complicated to use than one-dimensional ones. Here data is stored in the form of ROWS and COLUMNS which are accessed with the help of SUBSCRIPT number.
For e.g int arr[5][4] ;

  • Conventionally, the 1st subscript is used for rows and the 2nd for columns.
  • The 2d array can hold up to (rows * columns) values.
  • As is usual in C, subscripts start with zero, so the subscripts range from 0 to 4 for the rows and 0 to 3 for the columns.
  • If the system uses 2 bytes for an int, this array would use 40 bytes of RAM. All of the bytes are in consecutive memory locations, the first row occupying the first 8 bytes, the second the next 8, and so on.

The following table illustrates how the subscripts are specified for this array, which has five (5) rows and four (4) columns.

   
0123
0[0][0][0][1][0][2][0][3]
1[1][0][1][1][1][2][1][3]
2[2][0][2][1][2][2][2][3]
3[3][0][3][1][3][2][3][3]
4[4][0][4][1][4][2][4][3]

 

C Language Code:

        MULTIARR.H      

// DSTC ------- MULTIARR.H
// This program is creating MATRIX Dynamically
// MATRIX.. add, mulitpication, subtration, creation,

#include "assert.h"

typedef struct MultiDimentionArray
{
int maxr;
int maxc;
int **bfr;
}matrix;

matrix *makemat(int nr,int nc)
{
matrix *m;
int i,j;
m = (matrix *) malloc(sizeof(matrix));
m->maxr= nr;
m->maxc= nc;
m->bfr = (int **) malloc(sizeof(int*)*nr);
for(i=0;i<nr;i++)
{
m->bfr[i]=(int *)malloc(sizeof(int)*nc);
}
// m->bfr = (int *) malloc(sizeof(int **)*nc);
return (m);
}

void inputmat(matrix *m,int val, int r, int c)
{
m->bfr[r][c] = val ;
}

int inputmat(matrix *m, int r, int c)
{
return(m->bfr[r][c]);
}

void printmat(matrix *m)
{
int i,j ;
printf("\n");
for(i=0;i < m->maxr;i++)
{
printf("\n |");


Get Paid by Reading Ads on your Mobiles

for(j=0;j < m->maxc ; j++)
{
printf("\t%d",m->bfr[i][j]);
}
printf(" |");
}
} void add2mat(matrix *m1,matrix *m2,matrix *res)
{
int i,j;
assert((m1->maxr == m2->maxr) && (m1->maxc == m2->maxc)) ;
for(i=0;i<m1->maxr;i++)
{
for(j=0;j < m1->maxc ; j++)
{
res->bfr[i][j] = m1->bfr[i][j] + m2->bfr[i][j] ;
}
}
}

void subtract2mat(matrix *m1,matrix *m2,matrix *res)
{
int i,j;
assert((m1->maxr == m2->maxr) && (m1->maxc == m2->maxc)) ;
for(i=0;i<m1->maxr;i++)
{
for(j=0;j < m1->maxc ; j++)
{
res->bfr[i][j] = m1->bfr[i][j] - m2->bfr[i][j] ;
}
}
}

// precondion for matrix Multiplication is
// cols of first matrix = rows of second matrix
// and new matrix will be if
// m1(m*n) * m2(n*p) == res(m*p)

matrix *multiplymat(matrix *m1, matrix *m2)
{
assert(m1->maxc == m2->maxr) ;
matrix *r;
int i,j,m,n,s ;
i = m1->maxr ;
j = m2->maxc ;
r = makemat(i,j);

for(i=0;i < r->maxr;i++)
{
for(j=0;j < r->maxc;j++)
{
printf("\n");
s=0;
for(m=0,n=0;m< m1->maxc;m++,n++)
{
s = s + (m1->bfr[i][m] * m2->bfr[m][j]);
r->bfr[i][j] = s;
printf("m1[%d][%d] * m2[%d][%d]\t",i,m,m,j);
}
}
}
return (r) ;
}

          MULTIARR.CPP        

// DSTC ------- MULTIARR.CPP (including MULTIARR.H header file)
// this program is creating Dynamically
//

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "multiarr.h"

main()
{
clrscr();

int i,j,v ,r,c;
matrix *m,*m1,*res;

printf("\n Enter the 1 Matrix's Dimentions r,c");
scanf("%d,%d", &r,&c);

m = makemat(r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\nEnter m[%d][%d]:",i,j);
scanf("%d",&v);
inputmat(m,v,i,j);
}
}

printf("\n Enter the 2 Matrix's Dimentions r,c");
scanf("%d,%d", &r,&c);
m1 = makemat(r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\nEnter m1[%d][%d]:",i,j);
scanf("%d",&v);
inputmat(m1,v,i,j);
}
}

Get Paid by Reading Ads on your Mobiles

res = makemat(r,c) ;
printmat(m);
printf("\n + ");
printmat(m1);
//add2mat(m,m1,res);

printf(" = \n" );
//printmat(res);
//subtract2mat(m,m1,res);
//printf(" - \n" );
//printmat(res);
res = multiplymat(m,m1) ;
printf("\nMatrix Multiplication:");
printmat(res);
}

No comments: