Showing posts with label source code of Arrays - One Dimensional. Show all posts
Showing posts with label source code of Arrays - One Dimensional. Show all posts

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);
}

Arrays - One Dimensional - DSTC using C Language (Source Code Implemented)

Arrays - One Dimensional 
Implemented using C Language

 

Arrays have 2 basic operations i.e. extraction and storing values.

  • Extraction operation is a function that accepts an array (arr) and an index or subscript number ( i ) , and returns a values stored at index nuber. It is denoted as arr[i].

  • Storing operation accepts an array (arr), and index or subscript ( i ), and a value to be stored ( X ). It is denoted as arr[i] = X. Before a value has been assigned to an element of the array, its value is undefined and a reference to it in an expression is illegal.

C Language Code:

       ARRAY1D.H      

#include "assert.h"
typedef struct integer_array
{
int *bfr ;
int size;
}intarr ;

intarr *makearray(int n)
{
intarr *a1;
a1 = (intarr *) malloc(sizeof(intarr)) ;
a1->size = n ;
a1->bfr = (int *) malloc(sizeof(int)*n);
return (a1);
}

void input(intarr *a1,int val, int pos)
{
assert(pos>=0 && pos<a1->size);
assert(val != 0) ;
a1->bfr[pos] = val;
// or *(a1->bfr+pos) = v ;
}

int get(intarr *a1,int pos)
{
assert(pos>=0 && pos<a1->size);
return(a1->bfr[pos]);
// or return(*(a->bfr+pos));
}

void printarray(intarr *a1,int posupto)
{
// if posupto is zero(0) then it will all array elements otherwise
// only the speicified no. of elements.
int i=0,k ;
if (posupto==0)
i=a1->size ;
else
i = posupto ;

//DISPLAYS THE ARRAY BEFORE SORTING
printf("\n");
for(k=0;k<i;k++)
printf("a[%d]\t",k);

printf("\n");
for(k=0;k<i;k++)
printf("%d\t",a1->bfr[k]);
}

void sortarray(intarr *a1)
{
int i=0,j=0,t ;

for(i=0;i<a1->size-1;+i++)
{
for(j=i+1;j<a1->size;j++)
{
if ( a1->bfr[i] > a1->bfr[j] )
{
t = a1->bfr[i];
a1->bfr[i] = a1->bfr[j] ;
a1->bfr[j] = t ;
}
}
}

}

             ARRAY1D.CPP         

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <assert.h>
#include "array1d.h"

void main()
{
intarr *a1;
int i,val,n ;
printf("\n Enter the no.of elements in the array: ");
scanf("%d",&n);
printf("\nvalue entered wwas %d",n);
a1 = makearray(n);
printf("\nsize of array %d",a1->size);
for(i=0;i<n;i++)
{
printf("\n Enter values of a[%d]=",i);
scanf("%d",&val);
// a1->bfr[i] = val ;
input(a1,val,i);
}
printarray(a1,0) ;

sortarray(a1) ;
printarray(a1,0);
}