Programming in C

In this section i add some of the programs of the c language. i think this is useful for the beginner programmer of the c

1.Adding matrix using malloc.c

#include<stdio.h>
#include<stdlib.h>
#define MAXROWS 20
void readinput(int *a[MAXROWS],int nrows, int ncols);
void compute(int *a[MAXROWS],int *b[MAXROWS],int *c[MAXROWS],int nrows,int ncols);
void readoutput(int *a[MAXROWS],int nrows,int ncols);
main()
{
int row,nrows,ncols;
int *a[MAXROWS],*b[MAXROWS],*c[MAXROWS];
printf(“How many rows?”);
scanf(“%d”,&nrows);
printf(“How many cols?”);
scanf(“%d”,&ncols);
for(row=0;row<nrows;++row)
{
a[row]=(int *)malloc(ncols * sizeof(int));
b[row]=(int *)malloc(ncols * sizeof(int));
c[row]=(int *)malloc(ncols * sizeof(int));
}
printf(“First table”);
readinput(a,nrows,ncols);
printf(“Second table”);
readinput(b,nrows,ncols);
compute(a,b,c,nrows,ncols);
readoutput(c,nrows,ncols);
}
void readinput(int *a[MAXROWS],int nrows, int ncols)
{
int row,col;
for(row=0;row<nrows;++row)
{
printf(“Enter data for row no %d\n”,row+1);
for(col=0;col<ncols;++col)
{
scanf(“%d”,(*(a+row)+col));
}
}
return;
}
void compute(int *a[MAXROWS],int *b[MAXROWS],int *c[MAXROWS],int nrows,int ncols)
{
int row,col;
for(row=0;row<nrows;++row)
{
for(col=0;col<ncols;++col)
{
*(*(c+row)+col)=*(*(a+row)+col)+*(*(b+row)+col);
}
}
return;
}
void readoutput(int *c[MAXROWS],int nrows,int ncols)
{
int row,col;
for(row=0;row<nrows;++row)
{
for(col=0;col<ncols;++col)
{
printf(“%d”,*(*(c+row)+col));
}
}
return;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s