Hi friends, This is a program i posted before to calculate the sum of two tables.There is a little change in these codes than the before one.
I have used pointers to arrays here rather than arrays of pointers as I had done before.The previous program was corrected by using arrays of pointers and then using malloc for assigning the reqd. bytes for the respective arrays.But here I am using pointers to arrays and not using malloc.I compiled the program in Code::Blocks and run it.It runs but in the middle it crashes.So I thought if there was any mistake in my codes.I tried to find mistakes if any in the program but none.But when i tried in Turbo C++ compiler, it was compiled and run quite fine without any problem and correct answer.So I want to know why the same program runs on one compiler and not on other(i.e.,crashes).I also want to know if my program was deficient or not good in using any codes.Please comment..........
The program is:
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define COL 20
void readinput(int (*x)[COL],int,int);//function prototypes
void computesum(int (*x)[COL],int (*y)[COL],int (*z)[COL],int,int);
void printout(int (*x)[COL],int,int);
int main()
{
int row,nor,noc;//nor=no of rows noc=no of columns
int (*a)[COL],(*b)[COL],(*c)[COL];//THIS STATEMENT DECLARES THAT a,b,c are pointers to arrays of dimensions 20
printf("\nHOW MANY ROWS? ");
scanf("%d",&nor);
printf("\nHOW MANY COLUMNS? ");
scanf("%d",&noc);
printf("\nREAD INPUT OF FIRST TABLE:\n");
readinput(a,nor,noc);//inputting first table(function call)
printf("\nREAD INPUT OF SECOND TABLE:");
readinput(b,nor,noc);//inputting second table(function call)
computesum(a,b,c,nor,noc);//computing the sum of the tables(function call)
printf("\nPRINTING OUTPUT\n");
printout(c,nor,noc);//showing the result(function call)
getch();
return 0;
}
void readinput(int (*x)[COL],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
printf("\nENTER THE ELEMENTS OF THE ROW NO. %d:\n",i+1);
for(j=0;j<c;j++)
{
scanf("%d",(*(x+i)+j));//reading each element of the table
}
}
}
void computesum(int (*x)[COL],int (*y)[COL],int (*z)[COL],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
*(*(z+i)+j) = *(*(x+i)+j) + *(*(y+i)+j);//adding corresponding elements
}
}
}
void printout(int (*x)[COL],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%-5d",*(*(x+i)+j));//printing out the numbers
if(j==c-1)
printf("\n");
}
}
}
Thanks......