I'm working on a code which ask user to type the number of rows and columns for 2 matrices and later ask user to type its elements.
After taking the user input for elements of matrices. The function matadd
is called which receives the addresses of array variable a[20][20]
, b[20][20]
containing elements of first and second matrix, address of variables m
, n
containing no. of rows and columns, address of array variable s[20][20]
which is going to store the sum of two matrices.
matadd
function code :
int matadd(int *(h)[20], int *(k)[20], int *u, int *l,int *(m)[20])
{
//VARIABLE-DECLARATION
int i = 0, j = 0;
for (i = 0; i < *u; i++)
{
for (j = 0; j < *l; j++)
{
*(*(m + i) + j) = *(*(h + i) + j) + *(*(k + i) + j);
}
}
printf("The Addition of above two given matrices is given below :\n");
printf("-- --\n");
for (i = 0; i < *u; i++)
{
for (j = 0; j < *l; j++)
{
printf("|");
for (j = 0; j < *l; j++)
{
printf("%4d", *(*(m + i) + j));
}
printf(" | ");
printf("\n");
}
}
printf("-- --\n");
return(m);
}
The problem comes at this line of code *(*(m + i) + j) = *(*(h + i) + j) + *(*(k + i) + j);
where it has to assign the added value one by one inside pointer variable m
.
But program stops there and give me error :
Exception thrown at 0x00C124B9 in mATRIX_pROTO.exe: 0xC0000005: Access violation reading location 0x00000001.
If there is a handler for this exception, the program may be safely continued.
Not able to find whats wrong with the code. I am using Visual Studio 2015 for debugging my code.
Any help would be appreciated.
Here's my code :
//CONSTANT-VARIABLE
#define MAX 1000
//USER-DEFINED FUCNTION
int matadd(int *h, int *k, int *u, int *l,int *(m)[20]);
char xgets(char *line, int size, FILE *stdi);
void header(void);
char xgets(char *line, int size, FILE *stdi) {
/* read a line from the user */
fgets(line, size, stdi);
/* strip the \n if present */
line[strcspn(line, "\n")] = '\0';
return line;
}
void header(void)
{
printf("*-*-*-*-*MATRIX_ADD_PTR*-*-*-*-*");
printf("\n\n");
}
//PROGRAM STARTS HERE
int main(void)
{
//FUCNTION CALL-OUT
header();
//VARIABLE-DECLARATION
int a[20][20] = { { 0 } }, b[20][20] = { { 0 } }, c[20][20] = { { 0 } },s[20][20] = { {0} };
int i = 0, j = 0;
int m = 0, n = 0, q = 0, w = 0;
int line[MAX] = { 0 };
printf("Type Same Number of ROWs And COLUMNs for Both Matrx \n\n");
printf("Enter the No. of ROWs needed in First Matrix : ");
xgets(line, sizeof(line), stdin);
sscanf_s(line, "%d", &m);
printf("\n");
printf("Enter the No. of COLUMNs needed in First Matrix : ");
xgets(line, sizeof(line), stdin);
sscanf_s(line, "%d", &n);
printf("\n");
printf("Enter the Elements for 1st ROW then 2nd and So on for First Matrix : \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
xgets(line, sizeof(line), stdin);
sscanf_s(line, "%d", &a[i][j]);
}
}
printf("\n");
printf("Enter the No. of ROWs needed in Second Matrix : ");
xgets(line, sizeof(line), stdin);
sscanf_s(line, "%d", &q);
printf("\n");
printf("Enter the No. of COLUMNs needed in Second Matrix : ");
xgets(line, sizeof(line), stdin);
sscanf_s(line, "%d", &w);
printf("\n");
printf("Enter the Elements for 1st ROW then 2nd and So on for Second Matrix : \n");
for (i = 0; i < q; i++)
{
for (j = 0; j < w; j++)
{
xgets(line, sizeof(line), stdin);
sscanf_s(line, "%d", &b[i][j]);
}
}
//OUTPUT OF ENTERED MATRIX
printf("First Matrix : \n\n");
printf("-- --\n");
for (i = 0; i < m; i++)
{
printf("|");
for (j = 0; j < n; j++)
{
printf("%3d", a[i][j]);
}
printf(" | ");
printf("\n");
}
printf("-- --\n\n\n");
printf("Second Matrix : \n\n");
printf("-- --\n");
for (i = 0;i < q;i++)
{
printf("|");
for (j = 0; j < q; j++)
{
printf("%3d", b[i][j]);
}
printf(" | ");
printf("\n");
}
printf("-- --\n\n\n");
//FUCNTION CALL-OUT
matadd(a, b, &m, &n, s);
//TERMINAL-PAUSE
system("pause");
}
int matadd(int *(h)[20], int *(k)[20], int *u, int *l,int *(m)[20])
{
//VARIABLE-DECLARATION
int i = 0, j = 0;
for (i = 0; i < *u; i++)
{
for (j = 0; j < *l; j++)
{
*(*(m + i) + j) = *(*(h + i) + j) + *(*(k + i) + j);
}
}
printf("The Addition of above two given matrices is given below :\n");
printf("-- --\n");
for (i = 0; i < *u; i++)
{
for (j = 0; j < *l; j++)
{
printf("|");
for (j = 0; j < *l; j++)
{
printf("%4d", *(*(m + i) + j));
}
printf(" | ");
printf("\n");
}
}
printf("-- --\n");
return(m);
}