Hey man, giving a first look to your code it looked as if you are well versed i programming, but after thoroughly going i thought either you are not clear with mem managament concepts of have just copied code.
i have made certain change that ececute code correctly.
Many Confusing comments were given...
I am still not able to figure the need of them...
Also many unnecessary inputs were asked....
and theres no ned to take symmetric matrix, any matrix with resultant matrix equal to size of largest matrix would solve the problem...
Go through changes and understand..
Also try to use code that others should feel easy to understand....
Code:
/* Best Method to allocate memory for 2D Array because it's
much more flexible
*/
/* int ***matrixptr -> **rowptr -> *rowptr[nrow] -> element of row
1st Matrix -> *rowptr[nrow] -> element of row
-> *rowptr[nrow] -> element of row
-> **rowptr -> *rowptr[nrow] -> element of row
2nd Matrix -> *rowptr[nrow] -> element of row
-> *rowptr[nrow] -> element of row
-> **rowptr -> *rowptr[nrow] -> element of row
3nd Matrix -> *rowptr[nrow] -> element of row
-> *rowptr[nrow] -> element of row
*/
// How do code this in C
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"conio.h"
/* int **rowptr -- A pointer to pointer to integer
This can be explain through graphically.
int **rowptr -> *rowptr[nrow] -> element of row
-> *rowptr[nrow] -> element of row
-> *rowptr[nrow] -> element of row
*/
int Symmetric(int, int, int, int);
void Add(int **, int **, int, int, int, int);
void Display(int **, int , int );
// int *nrow_ptr = NULL, *ncol_ptr = NULL;
/* int *nrow_ptr, *ncol_ptr;
// Declare the nrow_ptr and ncol pointer
nrow_ptr = &nrow;
ncol_ptr = &ncol;
// Initialized the nrow_ptr and ncol_ptr
*/
int main(int argc, char *argv[])
{
clrscr();
int nMatrix;
int nrow, ncol, nrow_1, ncol_1;
// Number of row and column
int row_loop, col_loop;
// Loop for row and column
int **rowptr = NULL; // -- First Matrix
// A pointer to pointer to integer
int **rowptr_1 = NULL; // -- Second Matrix
int **result = NULL; // Result of two Matrix
int symmetric;
/*
Used while loop instead of dowhile because it's recommended
by Bjarne Stroustrup
*/
// ------------------------------------------------------------
// while(1)
// { // Must allocate at least two matrix
printf("\nPlease Enter same dimension for two Matrix\n");
printf("\nHow many Matrix : ");
scanf("%d", &nMatrix);
switch(nMatrix)
{
case 1:
{
printf("How many Row : ");
scanf("%d", &nrow);
rowptr = (int **)malloc(sizeof(int) * nrow);
if (rowptr == NULL)
{
perror("Dynamic Memory Allocation for row Fails");
}
printf("How many Column : "); scanf("%d", &ncol);
for (row_loop=0;row_loop<nrow;row_loop++)
{
rowptr[row_loop] = (int *)malloc(sizeof(int) * ncol);
if (rowptr[row_loop] == NULL)
{
perror("Dynamic Memory Allocation for column Fails");
}
}
for (row_loop=0;row_loop<nrow;row_loop++)
{
for (col_loop=0;col_loop<ncol;col_loop++)
{
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
scanf("%d", &rowptr[row_loop][col_loop]);
}
}
break;
}
// ------------------------------------------------------------
case 2:
{ // Enter the first matrix
do
{
printf("Enter the First Matrix : \n");
printf("How many Row : ");
scanf("%d", &nrow);
rowptr = (int **)malloc(sizeof(int) * nrow);
if (rowptr == NULL)
{
perror("Dynamic Memory Allocation for row Fails");
}
printf("How many Column : ");
scanf("%d", &ncol);
for (row_loop=0;row_loop<nrow;row_loop++)
{
rowptr[row_loop] =(int *) malloc(sizeof(int) * ncol);
if (rowptr[row_loop] == NULL)
{
perror("Dynamic Memory Allocation for column Fails");
}
}
for (row_loop=0;row_loop<nrow;row_loop++)
{
for (col_loop=0;col_loop<ncol;col_loop++)
{
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
scanf("%d", &rowptr[row_loop][col_loop]);
}
}
// -----------------------------------------------------------
// Enter the second Matrix
printf("Enter the Second Matrix : \n");
printf("How many row : ");
scanf("%d", &nrow_1);
rowptr_1 = (int **)malloc(sizeof(int) * nrow_1);
if (rowptr_1 == NULL)
{
perror("Dynamic Memory Allocation Matrix 2 for row Fails");
}
printf("How many Column : ");
scanf("%d", &ncol_1);
for (row_loop=0;row_loop<nrow_1;row_loop++)
{
rowptr_1[row_loop] = (int*)malloc(sizeof(int) * nrow_1);
if(rowptr_1[row_loop] == NULL)
{
perror("Dynamic Memory Allocation Matrix 2 for column Fails");
}
}
for (row_loop=0;row_loop<nrow_1;row_loop++)
{
for (col_loop=0;col_loop<ncol_1;col_loop++)
{
printf("Enter the [%d][%d] number : ", row_loop, col_loop);
scanf("%d", &rowptr_1[row_loop][col_loop]);
}
}
break;
}while(symmetric == 0);
}
}
// ------------------------------------------------------------
// Check the Dimension of Two Matrix
symmetric = Symmetric(nrow, ncol, nrow_1, ncol_1);
if (symmetric == 1)
{
printf("The two Matrix is same dimension");
}
else
{
printf("The two Matrix is different dimension");
}
// Runtime Error
Add(rowptr, rowptr_1, nrow, ncol, nrow_1, ncol_1);
// Adding the element of matrix
// Display(result, nrow, ncol);
// Display the result after successful allocated the memory
free(rowptr);
free(rowptr_1);
free(result);
// }
return 0;
}
// -----------------------------------------------------------
int Symmetric(int nrow, int ncol, int nrow_1, int ncol_1)
{
int symmetric;
if (nrow == nrow_1 && ncol == ncol_1)
{
symmetric = 1;
return symmetric;
}
else
{
symmetric = 0;
return symmetric;
}
}
// ------------------------------------------------------------
void Add(int **rowptr, int **rowptr_1, int nrow,
int ncol, int nrow_1, int ncol_1)
{
int **result = NULL;
result= (int **)malloc(sizeof(int) * nrow * ncol);
int row_loop, col_loop;
nrow = nrow_1;
ncol = ncol_1;
for (row_loop = 0;row_loop < nrow; row_loop++)
{
for (col_loop = 0; col_loop < ncol; col_loop++)
{
// *(*(result + row_loop) + col_loop) = (*(*(rowptr + row_loop) + col_loop)) + (*(*(rowptr_1 + row_loop) + col_loop));
result[row_loop][col_loop]=rowptr[row_loop][col_loop]+rowptr_1[row_loop][col_loop];
}
}
Display(result,nrow,ncol);
}
// ------------------------------------------------------------
void Display(int **result, int nrow, int ncol)
{
int row_loop, col_loop;
// Loop for row and column
for (row_loop = 0; row_loop < nrow; row_loop++)
{
for (col_loop = 0; col_loop < ncol; col_loop++)
{
printf("The value of array at [%d][%d] is %d\n", row_loop, col_loop, *(*(result + row_loop) + col_loop) );
}
}
}
// ------------------------------------------------------------