Printing matrix from an txt file in C - c

I have to read the number of rows and columns of a matrix from a txt file also its elements. the first row is the number of rows and columns followed by its numbers. For example this is a matrix with 2 rows and 3 columns containing 123 456 and a matrix with 3 rows and 2 columns.
2 3
1 2 3
4 5 6.
3 2
1 2
3 4
5 6
What I have to do is to read two matrix from the same text file and multiply them. I'm encountering some unknown problem.
When I write my code to test whether the matrix is populated correctly. I only get the first matrix printed and then the program stopped. Output: 1 2 3
4 5 6
Here's the part of the code. Thanks in advance.
int main(){
int **mat1,**mat2,**result;
int row1,col1,row2,col2;
int i,j,k;
FILE *file;
char fname[100];
printf("enter file name\n");
scanf("%s", &fname);
file=fopen(fname, "r");
fscanf(file, "%d %d", &row1,&col1);//row of first matrix
mat1=malloc(row1 * col1 * sizeof(int*));//create memory for first matrix
//reading data for first matrix
for(i=0; i<row1; i++)
{
for(j=0; j<col1; j++)
{
fscanf(file, "%d", &mat1[i][j]);
}
}
for(i=0; i<row1; i++)
{
for(j=0; j<col1; j++)
{
printf("%d\t",mat1[i][j]);
}
printf("\n");
}
fscanf(file,"%d %d", &row2, &col2);//row of second matrix
mat2=malloc(row2 * col2 * sizeof(int*));//create memory for second matrix
//reading data for second matrix
for(i=0; i<row2; i++)
{
for(j=0; j<col2; j++)
{
fscanf(file,"%d",&mat2[i][j]);
}
}
for(i=0; i<row2; i++) //check mat2
{
for(j=0; j<col2; j++)
{
printf("%d\t",mat2[i][j]);
}
printf("\n");
}

I think I fixed the problem, but I had to do quite a bit of debugging due to lots of segfaults, so I'll put the answer to the original question at the top and defer all the things I had to debug to the bottom.
fscanf(file, "%*[.]"); //this is to read the period in the file
fscanf(file,"%d %d", &row2, &col2);//row of second matrix
mat2=malloc(row2 * col2 * sizeof(int*));//create memory for second matrix
By adding that one line of code, it started working for me, but it only started working due to the debugging below.
Since the number of calls to malloc did not represent the number of nested ararys that were indexed, I had to change the declaration of the matrices, the way they were populated, and they way they are referenced.
int *mat1,*mat2;
This declaration is to make the next edits work.
//reading data for first matrix
for(i=0; i<row1; i++)
{
for(j=0; j<col1; j++)
{
fscanf(file, "%d", &(mat1[i*col1 + j]));
}
}
for(i=0; i<row1; i++)
{
for(j=0; j<col1; j++)
{
printf("%d\t",mat1[i*col1 + j]);
}
printf("\n");
}
And of course, for the second matrix;
//reading data for second matrix
for(i=0; i<row2; i++)
{
for(j=0; j<col2; j++)
{
fscanf(file,"%d",&(mat2[i*col2 + j]));
}
}
for(i=0; i<row2; i++) //check mat2
{
for(j=0; j<col2; j++)
{
printf("%d\t",(mat2[i*col2 + j]));
}
printf("\n");
}
Please keep in mind that if you are going to be referencing an array inside of an an array, then you need to malloc space for another array in each slot of the original array. I chose the unintuitive approach to matrix arithmetic, but the code can be augmented to easily allow for double array indexing to be used.

You have some problem with allocating a 2D arrays:
For example:
mat1=malloc(row1 * col1 * sizeof(int*));//create memory for first matrix
it should be
mat1=(int**)malloc(row1 * sizeof(int*));//create memory for first matrix
Then should allocate each row in mat1 in for loop with before scanf each cell in a matrix
for (i = 0; i < row1; i++)
{
mat1[i] = malloc(col1 * sizeof(int));
for (j = 0; j < col1; j++)
{
fscanf(file, "%d", &mat1[i][j]);
}
}
Then, do similar stuff for mat2
Here is my solution that based on original problem:
#include <stdio.h>
#include <stdlib.h>
int main() {
int **mat1, **mat2, **result;
int row1, col1, row2, col2;
int i, j, k;
FILE *file;
char fname[100];
printf("enter file name\n");
scanf("%s", &fname);
file = fopen(fname, "r");
fscanf(file, "%d %d", &row1, &col1);//row of first matrix
mat1 = (int **)malloc(row1 * sizeof(int*));//create memory for first matrix
//reading data for first matrix
for (i = 0; i < row1; i++)
{
mat1[i] = malloc(col1 * sizeof(int));
for (j = 0; j < col1; j++)
{
fscanf(file, "%d", &mat1[i][j]);
}
}
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
printf("%d\t", mat1[i][j]);
}
printf("\n");
}
fscanf(file, "%d %d", &row2, &col2);//row of second matrix
mat2 = (int **)malloc(row2 * sizeof(int*));//create memory for second matrix
//reading data for second matrix
for (i = 0; i < row2; i++)
{
mat2[i] = malloc(col2 * sizeof(int));
for (j = 0; j < col2; j++)
{
fscanf(file, "%d", &mat2[i][j]);
}
}
for (i = 0; i < row2; i++) //check mat2
{
for (j = 0; j < col2; j++)
{
printf("%d\t", mat2[i][j]);
}
printf("\n");
}
fclose(file);
return 0;
}

Related

Error in calculating resultant matrix in C programming language

I am trying to write a program where I have two matrices and I multiply the two matrices and store it in a resultant matrix named "carr." For some weird reason, the matrix multiplication is not getting executed properly. Tried to find the issue for quite a while but couldn't find the error. Can anyone help? TIA for your time!
Here is the ss of the issue: https://snipboard.io/s9ifP4.jpg
#include <stdio.h>
int main()
{
int row1, column1, row2, column2,i,j,k, sum=0;
//START OF THE 1ST ARRAY//
printf("How many rows do you want for the first matrix? Ans: ");
scanf("%d", &row1);
printf("How many columns do you want for the first matrix? Ans: ");
scanf("%d", &column1);
int arr[row1][column1];
printf("Enter the elements of the first array:\n");
for(i = 0; i <row1; i++){
for(j=0; j < column1; j++){
scanf("%d", &arr[i][j]);
}
}
printf("\n----------------------------------------\n");
printf("The elements of the first array are:\n");
for(i = 0; i <row1; i++){
printf("[ ");
for(j=0; j < column1; j++){
printf("%d, ", arr[i][j]);
}
printf("]\n");
}
//END OF THE FIRST ARRAY//
printf("----------------------------------------\n");
//START OF THE 2ND ARRAY//
printf("\n**How many rows do you want for the second matrix?\n\nAlert: For matrix multiplication, the COLUMN of the 1st matrix MUST equal to the ROW of the 2nd matrix.\nAns: ");
scanf("%d", &row2);
printf("How many columns do you want for the second matrix? Ans: ");
scanf("%d", &column2);
int barr[row2][column2];
printf("Enter the elements of the second array:\n");
for(i = 0; i <row2; i++){
for(j=0; j < column2; j++){
scanf("%d", &arr[i][j]);
}
}
printf("\n----------------------------------------\n");
printf("The elements of the second array are:\n");
for(i = 0; i <row2; i++){
printf("[ ");
for(j=0; j < column2; j++){
printf("%d, ", arr[i][j]);
}
printf("]\n");
}
printf("----------------------------------------\n");
//END OF THE 2ND ARRAY//
//Everything above this part is okay. The problem starts from the Matrix multiplication part//
//MATRIX MULTIPLICATION//
//The resultant matrix where the values of the multiplied matrix is being held has row = ROW1 and column = COLUMN2.//
int carr[row1][column2];
if(column1 == row2)
{
for(i = 0; i < row1; i++){
for(j=0; j < column2; j++){
for(k=0; k < row2; k++){
sum = sum + arr[i][k] * barr[k][j];
}
carr[i][j] = sum;
sum=0;
}
}
}
else
{
printf("Matrix multiplication is not possible");
}
printf("\n----------------------------------------\n");
printf("The elements of the resultant array are:\n");
for(i = 0; i <row1; i++){
printf("[ ");
for(j=0; j < column2; j++){
printf("%d, ", carr[i][j]);
}
printf("]\n");
}
printf("----------------------------------------\n");
return 0;
}
Changing arr to barr fixes the issue. Thanks to #M Oehm for pointing out the error.

Varying Results When Displaying Result Matrix

I am currently working on a code that calculates the following equation:
W = (XT * X)^-1 * XT * Y
with XT being the transpose of X and ^-1 the inverse of the product XT*X.
To do this, I am passing a text file with a matrix to the program, and then performing operations on it to obtain the proper matrix formats to use in the equation. Although the code works some times, there are instances when I will run the code and receive the proper answer, but others when a random number will be produced.
For example, when using the matrix [first integer = columns-1, second integer = rows]:
4
4
4.000000,3.000000,2.000000,3.000000,200.000000
5.000000,2.000000,1.000000,7.000000,300.000000
1.000000,4.000000,2.000000,1.000000,500.000000
8.000000,8.000000,9.000000,3.000000,200.000000
it will produce an answer for XT * (XT * X)^-1 as:
0.497617 -0.166646 0.061712 -0.137570
-61.086998 -0.258283 -0.340935 -0.064228
0.186411 -0.083895 0.285920 -0.082587
-0.722773 0.207515 -0.009408 0.238550
-0.579552 0.345476 0.154362 0.055048
and the numbers will not remain the same through each test run. It does this with this result matrix multiplied by Y [the last column in the original matrix] as well. Below is a sample of the code I have written thus far:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]){
if(argc < 2){
printf("error.");
return 0;
}
FILE *fptrain = fopen(argv[1], "r");
int row, col, i, j;
fscanf(fptrain, "%d", &col);
col = col+1;
fscanf(fptrain, "%d", &row);
char ch;
//creates the original X and Y matrix
float trainX[row][col];
float trainY[row][1];
for(i=0; i<row; i++)
{
trainX[i][0] = 1.000000;
for(j=1; j<col; j++)
{
fscanf(fptrain, "%f%c", &trainX[i][j], &ch);
}
fscanf(fptrain, "%f%c", &trainY[i][0], &ch);
}
//creates the X transposed matrix
float trainXtrans[col][row];
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
trainXtrans[j][i] = trainX[i][j];
}
}
//multiplies X and X transposed
float trainXtemp[row][row];
int s;
int num=0;
for(i=0; i<row; i++)
{
for(j=0; j<row; j++)
{
for(s=0; s<col; s++)
{
num = num + trainX[i][s]*trainXtrans[s][j];
}
trainXtemp[i][j] = num;
num =0;
}
}
//finds the identity matrix of X times X transposed
float trainXinden[row][row*2];
for(i=0; i<row; i++)
{
for(j=0; j<row; j++)
{
trainXinden[i][j] = trainXtemp[i][j];
}
for(j=row; j<row*2; j++)
{
if(j==i+row)
{
trainXinden[i][j] = 1;
}
else{
trainXinden[i][j] = 0;
}
}
}
//finds the inverse of X times X transposed through Gauss Jordan Elimination
int k;
float divscalar;
for(i=0; i<row; i++)
{
divscalar = trainXinden[i][i];
for(j=0; j<row*2; j++)
{
trainXinden[i][j] = trainXinden[i][j]/divscalar;
}
for(k=0; k<row; k++)
{
if(i!=k)
{
float subscalar = trainXinden[k][i];
for(j=0; j<row*2; j++)
{
trainXinden[k][j] = trainXinden[k][j] - subscalar*trainXinden[i][j];
}
}
}
}
//copies over the result of gauss jordan elimination
float trainXinverse[row][row];
for(i=0; i<row; i++)
{
for(j=0; j<row; j++)
{
trainXinverse[i][j] = trainXinden[i][j+row];
}
}
//multiplies (X times X transpose) inverse by (X transposed)
float trainXinvXt[col][row];
for(i=0; i<col; i++)
{
for(j=0; j<row; j++)
{
for(s=0; s<row; s++)
{
trainXinvXt[i][j] += trainXtrans[i][s]*trainXinverse[s][j];
}
}
}
//multiples (trainXinvXt) by Y
float weight[row][1];
for(i=0; i<col; i++)
{
for(s=0; s<col-1; s++)
{
weight[i][0] += trainXinvXt[i][s]*trainY[s][0];
}
}
return 0;
}
is this perhaps a memory issue or is my Gauss-Jordan Elimination method throwing something off?

Dynamic Memory Allocation for Matrix Multiplication

Currently I am working on a code that computes the following equation with two matrices, X and Y, to return the value of matrix W.
W = (XT * X)^-1 * XT * Y
Input Matrix train:
4
10
3.000000,1.000000,1180.000000,1955.000000,221900.000000
3.000000,2.250000,2570.000000,1951.000000,538000.000000
2.000000,1.000000,770.000000,1933.000000,180000.000000
4.000000,3.000000,1960.000000,1965.000000,604000.000000
3.000000,2.000000,1680.000000,1987.000000,510000.000000
4.000000,4.500000,5420.000000,2001.000000,1230000.000000
3.000000,2.250000,1715.000000,1995.000000,257500.000000
3.000000,1.500000,1060.000000,1963.000000,291850.000000
3.000000,1.000000,1780.000000,1960.000000,229500.000000
3.000000,2.500000,1890.000000,2003.000000,323000.000000
Input Matrix test:
3
3.000000,2.500000,3560.000000,1965.000000
2.000000,1.000000,1160.000000,1942.000000
3.000000,1.000000,1430.000000,1927.000000
Result Matrix:
716559
194430
323391
My code returns the proper values for the testcases with the exception of matrices over the size of 1000. I know this is because the size is not dynamically allocated, but I am not sure what the best approach to doing this in my code would be:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]){
if(argc < 3){
printf("error.");
return 0;
}
FILE *fptrain = fopen(argv[1], "r");
if(fptrain == NULL)
{
printf("error.");
return 0;
}
int row, col, i, j;
fscanf(fptrain, "%d", &col);
col = col+1;
fscanf(fptrain, "%d", &row);
char ch;
//creates the original X and Y matrix
double trainX[row][col];
double trainY[row][1];
for(i=0; i<row; i++)
{
trainX[i][0] = 1.000000;
for(j=1; j<col; j++)
{
fscanf(fptrain, "%lf%c", &trainX[i][j], &ch);
}
fscanf(fptrain, "%lf%c", &trainY[i][0], &ch);
}
//creates the X transposed matrix
double trainXtrans[col][row];
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
trainXtrans[j][i] = trainX[i][j];
}
}
//multiplies X and X transposed
double trainXtemp[row][row];
int s;
double num=0;
for(i=0; i<col; i++)
{
for(j=0; j<col; j++)
{
for(s=0; s<row; s++)
{
num = num + trainX[s][j]*trainXtrans[i][s];
}
trainXtemp[i][j] = num;
num = 0;
}
}
//finds the identity matrix of X times X transposed
double trainXinden[col][col*2];
for(i=0; i<col; i++)
{
for(j=0; j<col; j++)
{
trainXinden[i][j] = trainXtemp[i][j];
}
for(j=col; j<col*2; j++)
{
if(j==i+col)
{
trainXinden[i][j] = 1.000000;
}
else{
trainXinden[i][j] = 0.000000;
}
}
}
//finds the inverse of X times X transposed through Gauss Jordan Elimination
int k;
double divscalar;
for(i=0; i<col; i++)
{
divscalar = trainXinden[i][i];
for(j=0; j<col*2; j++)
{
if(trainXinden[i][j] != 0)
{
trainXinden[i][j] = trainXinden[i][j]/divscalar;
}
}
for(k=0; k<col; k++)
{
if(i!=k)
{
double subscalar = trainXinden[k][i];
for(j=0; j<col*2; j++)
{
trainXinden[k][j] = trainXinden[k][j] - subscalar*trainXinden[i][j];
}
}
}
}
double trainXinverse[row][row];
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
trainXinverse[i][j] = trainXinden[i][j+col];
}
}
double trainXinvXt[col][row];
for(i=0; i<col; i++)
{
for(j=0; j<row; j++)
{
for(s=0; s<col; s++)
{
num = num + trainXinverse[i][s]*trainXtrans[s][j];
}
trainXinvXt[i][j] = num;
num = 0;
}
}
//multiples (trainXinvXt) by Y
double weight[row][1];
for(i=0; i<col; i++)
{
for(s=0; s<row; s++)
{
weight[i][0] += trainXinvXt[i][s]*trainY[s][0];
}
}
FILE *fptest = fopen(argv[2], "r");
if(fptest == NULL)
{
printf("error.");
return 0;
}
int testrows;
fscanf(fptest, "%d", &testrows);
//creates the test file matrix
double testM[testrows][col];
for(i=0; i<testrows; i++)
{
testM[i][0] = 1.000000;
for(j=1; j<col; j++)
{
fscanf(fptest, "%lf%c", &testM[i][j], &ch);
}
}
double prices[testrows][1];
for(i=0; i<testrows; i++)
{
for(s=0; s<col; s++)
{
num = num + testM[i][s]*weight[s][0];
}
prices[i][0] = num;
num = 0;
}
for(i=0; i<testrows; i++)
{
printf("%0.0lf", prices[i][0]);
printf("\n");
}
return 0;
}
When I use malloc on each matrix, for some reason it appears that it is not allowing me to create the augmented matrix or perform my gauss-jordan reduction, which is ruining my final answer.
The best approach to allocate memory to a multi-dimensional array (taking e.g. of 2D array, since you are using matrix in your program):
int(*matrix)[col] = malloc (sizeof(int[row][col]));
If you want to write a function for it then:
void* allocMatrix (int row, int col)
{
return malloc (sizeof(int[row][col]));
}
If you are not familiar with this approach of allocating memory dynamically to a multi-dimensional array then read this (very nicely explained by Lundin).
In your program, you are having the matrix of type double, so the allocMatrix() will be -
void* allocMatrix (int row, int col)
{
return malloc (sizeof(double[row][col]));
}
and in main(), you need to make following changes to create matrix dynamically -
double (*trainX)[col] = allocMatrix (row, col);
Do same changes for other matrices also and make sure to free() the dynamically allocated memory to matrices at appropriate places in your program.
Just for the knowledge purpose, this practice of allocating memory to the multi-dimensional array has been followed commonly, though it's not the best.

Matrix multiplication in C - error values in result matrix

So I coded traditional matrix multiplication in C (I'm a beginner to C), but for some reason my result isn't correct, although I don't see any glaring errors. My input file looks like:
3 2
2 2
2 2
2 2
2 3
1 1 1
1 1 1
The 3 2 and 2 3 in the first and fifth lines represent the number of rows and columns in the subsequent matrices. The result should be a 3x3 matrix, all 4s. However, this code returns
4197299 4 4
4 4 4
-1912599044 32621 572
I'm inclined to believe this might be due to the way I declared the matrices. Instead of using malloc, I scanned the row and column values from the input file and directly instantiated the required matrices. I'm very new to C, so the concept of dynamic memory allocation isn't 100% clear yet. I could be totally off the mark, but I'm not sure. What confuses me especially is that about half of the matrix returned correct values. Why is this the case? Below is my code.
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char** argv){
int i, j, k, row1, col1, row2, col2, temp;
if(argc != 2){
printf("error\n");
exit(1);
}
FILE *file = fopen(argv[1], "r");
if(file == NULL){
printf("error\n");
exit(1);
}
//MATRIX 1
fscanf(file, " %d\t%d", &row1, &col1);
int matrix1[row1][col1];
for(i = 0; i<row1; i++){
for(j=0; j<col1; j++){
fscanf(file, "%d", &temp);
matrix1[i][j] = temp;
}
}
//MATRIX TWO
fscanf(file, " %d\t%d", &row2, &col2);
int matrix2[row2][col2];
int product[row1][col2]; //DECLARING PRODUCT MATRIX
for(i = 0; i<row2; i++){
for(j=0; j<col2; j++){
fscanf(file, "%d", &temp);
matrix2[i][j] = temp;
}
}
for(i = 0; i<row1; i++){
for(j = 0; j<col2; j++){
for(k = 0; k<col1; k++){
product[i][j] += matrix1[i][k]*matrix2[k][j]; //MULTIPLICATION STEP
}
}
}
for(i = 0; i<row1; i++){
for(j = 0; j<col2; j++){
printf("%d\t", product[i][j]); //PRINTING THE PRODUCT
}
printf("\n");
}
return 0;
}
for(i = 0; i<row1; i++){
for(j = 0; j<col2; j++){
product[i][j] = 0; // should be zero before summing
for(k = 0; k<col1; k++){
product[i][j] += matrix1[i][k]*matrix2[k][j]; //MULTIPLICATION STEP
}
}
}
You don't initialize product, which means that its contents is indeterminate, then using it in calculations (with e.g. +=) results in undefined behavior.
You need to initialize the products matrix to all zeroes first.
Yeah, that way of declaring dynamic arrays doesn't necessarily work, in any version before or after C99, although what’s causing the problem is that you don’t initialize product. Try calloc( row1*col2, sizeof(int) ); This allocates all the elements and initializes them to 0.

Segfault 11 when trying to multiply 2 matrices

I'm trying to multiply 2 dynamically allocated arrays. I'm having 2 problems:
When I try unequal sized arrays like [2],[3] and [3],[2] I got a segmentation fault 11, and after staring at my allocations I still can't figure out why.
My final array is formatted with the correct rows and columns, but it displays all 0's.
I'm assuming that this is because I didn't allocate the memory correctly.
-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **a, **b, **c; //pointers to arrays
int m1_r,m1_c,m2_r,m2_c; //declaring arrays
int i,j,k;
printf("\n");
again://repeat if first matrixes are bad
printf("Enter rows and columns for the first matrix.\n");//first matrix
scanf("%d%d" ,&m1_r,&m1_c);
printf("Enter rows and Columns for the second matrix.\n");//second matrix
scanf("%d%d",&m2_r,&m2_c);
if(m1_c!=m2_r) {
printf("You tried to break my code. Nice try.");
goto again;
}
//memory for first matrix
a = malloc(m1_r * sizeof(int *));
for(i=0; i < m1_r; i++) {
a[i] = malloc(m1_c * sizeof(int));
}
//memory for second matrix
b = malloc(m2_r * sizeof(int *));
for(i=0; i < m2_r; i++) {
b[i] = malloc(m2_c * sizeof(int));
}
//memory for 3rd matrix
c = malloc(m1_r * sizeof(int *));
for(i=0; i < m2_r; i++) {
c[i] = malloc(m2_c * sizeof(int));
}
//input 1st matrix
printf("Enter the numbers of the first matrix.\n");
for (i=0; i<m1_r; i++) {
for (j = 0; j<m1_c; j++) {
scanf("%d", &a[i][j]);
}
}
//input 2nd matrix
printf("Enter the second of the first matrix.\n");
for (i=0; i<m1_r; i++) {
for (j = 0; j<m1_c; j++) {
scanf("%d", &b[i][j]);
}
}
printf("\n");
printf("1st matrix looks like this:\n");
//print 1st matrix
for (i=0; i<m1_r; i++) {
for (j = 0; j<m1_c; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
//print 2nd matrix
printf("\n");
printf("2nd matrix looks like this:\n");
//print 2st matrix
for (i=0; i<m2_r; i++) {
for (j = 0; j<m2_c; j++) {
printf("%d\t", b[i][j]);
}
printf("\n");
}
//initialize result matrix to 0
for(i=0; i<m2_r; i++)
for(j=0; j<m2_c; j++) {
c[i][j]=0;
}
//multipication
for(i=0; i<m1_r; i++)
for(j=0; j<m2_c; j++)
for(k=0; k<m1_c; k++) {
c[i][j]+= a[i][k]*b[k][j];
}
//print result
printf("\nThe result of the matrix multiplication is:");
for(i=0; i<m1_r; i++) {
printf("\n");
for(k=0; k<m2_c; k++) {
printf("%d\t", c[i][j]);
}
}
printf("\n");
return 0;
}
You allocate the wrong amount of memory for the third matrix:
c = malloc(m1_r * sizeof(int *));
for(i=0; i < m2_r; i++)
The loop count should be the same as the number of pointers you malloc.
To avoid this sort of error, consider making a function which you pass in the dimensions and it returns the pointer.
Later on, you overwrite its bounds using different indices again:
for(i=0;i<m2_r;i++)
for(j=0;j<m2_c;j++)
{
c[i][j]=0;
}
Then you overwrite the bounds of b (it was m2_r and m2_c):
for (i=0; i<m1_r; i++) {
for (j = 0; j<m1_c; j++) {
scanf("%d", &b[i][j]);
}
}
To avoid this sort of error, you could use a better naming convention for your variables; and also consider using a struct which holds each pointer plus its dimension variables. Then you can have a function that zeroes any matrix and you only need to pass it a pointer to one of your matrix structs.
BTW if you use calloc instead of malloc then you don't need this loop at all (although you might want to have this function anyway so that you can zero a matrix).
Also you should check for success of scanf and malloc.
There are hell lot of bugs in your code:
First
//memory for 3rd matrix
c = malloc(m1_r * sizeof(int *));
for(i=0; i < m2_r; i++) <----- error: used m2_r instead of m1_r
You assigned m1_r and loop till m2_r.
Second
//input 2nd matrix
printf("Enter the second of the first matrix.\n");
for (i=0; i<m1_r; i++) { <----- error: used m1_r instead of m2_r
for (j = 0; j<m1_c; j++) { <----- error: used m1_c instead of m2_c
scanf("%d", &b[i][j]);
}
}
You are using rows and columns of 1st matrix.
Third
//initialize result matrix to 0
for(i=0; i<m2_r; i++) <----- error: used m2_r instead of m1_r
for(j=0; j<m2_c; j++) {
c[i][j]=0;
}
You used row value of second matrix rather than the first matrix

Resources