-1

I made a program that adds two matrices and displays their sum with a max dimension of 100.

/* 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;
}

Now I need to change it so there is no max size for each matrix. So I need to use malloc to create my arrays. So I cant use int A[rows][cols]. This is what I did to covert arrays to malloc. It compiles but it crashes after I entered all the integers. Need help.

/* 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;      // Variables
   int *first = NULL;
   int *second = NULL;
   int *sum = NULL;    // Matrices variables

   printf("Please enter the number of rows: ");
   scanf("%d", &m);
   printf("Please enter the number of columns: ");
   scanf("%d", &n);

   first = (int*)malloc(m * sizeof(int));
   second = (int*)malloc(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);

   // 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);

   // Adds the sum of Matrix A and Matrix B
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         sum = *first + *second;

   // 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);

      printf("\n");
   }

   return ;
}

// Main Function
int main()
{
    construct(); // Calls construct function
    return 0;
}
Alexis Marcelo
  • 7
  • 1
  • 2
  • 6
  • See http://stackoverflow.com/questions/25984700/setup-a-2d-array-change-size-later-c/25984834#25984834 – Richard Chambers Mar 01 '15 at 00:56
  • 1) the returned value from scanf() should always be checked to assure the input/conversion was successful. 2) in C, the returned value from malloc should not be cast. 3) the returned value from malloc should always be checked (!= NULL) to assure the operation was successful – user3629249 Mar 01 '15 at 02:00
  • this kind of line: 'canf("%d", &first);' will always put the input value in the same location in the malloc';d area, specifically in the very first '4' bytes.\ – user3629249 Mar 01 '15 at 02:09

1 Answers1

2

First of all, you don't need to use malloc. Just move the array definitions to be after you have inputted m and n:

int first[m][n];

printf("Enter Matrix A\n");
for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
        scanf("%d", &first[i][j]);

and similarly for second and sum.

If the matrices might be larger than 100x100 then it might be good to use malloc to avoid the risk of a stack overflow; the way to do that is:

int (*first)[n] = malloc(m * sizeof *first);

printf("Enter Matrix A\n");
// etc.

and at the end, free(first);. No other changes are required.


In your attempt to use malloc, you didn't allocate enough space, and you didn't scanf into the space you allocated either (instead you overwrote the pointer to the allocated space).

M.M
  • 138,810
  • 21
  • 208
  • 365