This is my code without malloc. I need to change the array size so there is no max size for each matrix. I must dynamically allocate space for all arrays used. So I need to use malloc to create my arrays. So I cant use int A[rows][cols].
Code:
/* This program asks the user for 2 matrices called A and B, as integers,
and displays their sum, C. The max dimension of each matrix is 100. */
#include<stdio.h>
// Construct function
void construct()
{
int m, n, i, j;// Variables
int first[100][100], second[100][100], sum[100][100];// Matrices variables
printf("Please enter the number of rows: ");
scanf("%d",&m);
printf("Please enter the number of columns: ");
scanf("%d",&n);
// User enters m x n amount whole numbers for the Matrix A
printf("Enter Matrix A\n");
for(i =0; i < m; i++)
for(j =0; j < n; j++)
scanf("%d",&first[i][j]);
// User enters m x n amount whole numbers for the Matrix B
printf("Enter Matrix B\n");
for(i =0; i < m; i++)
for(j =0; j < n; j++)
scanf("%d",&second[i][j]);
// Adds the sum of Matrix A and Matrix B
for(i =0; i < m; i++)
for(j =0; j < n; j++)
sum[i][j]= first[i][j]+ second[i][j];
// Display the sum of Matrix A and Matrix B
printf("A + B =\n");
for(i =0; i < m; i++)
{
for(j =0; j < n; j++)
printf("%d ", sum[i][j]);
printf("\n");// Prints new line
}
return;
}
// Main Function
int main()
{
construct();// Calls construct function
return 0;
}
Can someone help guide me through this? Im having a hard time understanding/using malloc. Should first, second, and sum be single pointers or double? How do I scan the matrix correctly? And how do I add them properly? Do I have to change my for loops?
Any suggestions for the next step to be?
Code:
/* This program asks the user for 2 matrices called A and B, as integers,
and displays their sum, C. The max dimension of each matrix is 100. */
#include <stdio.h>
#include <stdlib.h>
// Construct function
void construct()
{
int m, n, i, j; // m = rows; n = columns
int *first, *second, *sum; // first = matrix1; second = matrix2
printf("Please enter the number of rows: ");
scanf("%d", &m);
printf("Please enter the number of columns: ");
scanf("%d", &n);
first = (int*)malloc(m * n * sizeof(int));
second = (int*)malloc(m * n * sizeof(int));
sum = (int*)malloc(m * n * sizeof(int));
// User enters m x n amount whole numbers for the Matrix A
printf("Enter Matrix A\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", *(*(first+i)+j));
// User enters m x n amount whole numbers for the Matrix B
printf("Enter Matrix B\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", second[i][j];
// Adds the sum of Matrix A and Matrix B
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
*(sum[i][j])= *(first[i][j]) + *(second[i][j]);
// Display the sum of Matrix A and Matrix B
printf("A + B =\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
printf("%d ", *(sum + i + j));
printf("\n");
}
return ;
}
// Main Function
int main()
{
construct(); // Calls construct function
return 0;
}