Thanks a lot for that! It works perfectly now!
Here is my complete code....
Just to clarify, this method of creating a variable-size array is a new feature in the C99 standard, right? Therefore, there is no way to compile this on Microsoft Visual C (I receive numerous errors when I try to)?
Thanks again!
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, row, column, rowsize1, rowsize2, columnsize1, columnsize2, i, j, counter1, counter2;
/* Prompt user for matrix dimensions */
printf("Enter row (m) size of Matrix 1: \n");
scanf("%d", &rowsize1);
printf("Enter column (n) size of Matrix 1: \n");
scanf("%d", &columnsize1);
printf("\n");
printf("Enter row (m) size of Matrix 2: \n");
scanf("%d", &rowsize2);
printf("Enter column (n) size of Matrix 2: \n");
scanf("%d", &columnsize2);
/* Test matrix dimensions */
while((rowsize2!=columnsize1))
{
printf("\nMatrix dimensions must agree! Please re-enter values!\n\n");
printf("Enter row (m) size of Matrix 1: \n");
scanf("%d", &rowsize1);
printf("Enter column (n) size of Matrix 1: \n");
scanf("%d", &columnsize1);
printf("\n");
printf("Enter row (m) size of Matrix 2: \n");
scanf("%d", &rowsize2);
printf("Enter column (n) size of Matrix 2: \n");
scanf("%d", &columnsize2);
}
/* Initialize dimensions of input matrices */
int **matrix1;
matrix1 = malloc(rowsize1*sizeof(*matrix1));
if(matrix1)
{
int a;
for(a = 0; a < rowsize1; ++a)
{
matrix1[a] = malloc(columnsize1*sizeof (*matrix1[a] ));
if(matrix1[a] == NULL)
{
/* malloc failed - free what was allocated and return error */
free(matrix1);
printf("Memory allocation failed!\n");
}
}
}
int **matrix2;
matrix2 = malloc(rowsize2*sizeof(*matrix2));
if(matrix2)
{
int a;
for(a = 0; a < rowsize2; ++a)
{
matrix2[a] = malloc(columnsize2*sizeof (*matrix2[a] ));
if(matrix2[a] == NULL)
{
/* malloc failed - free what was allocated and return error */
free(matrix2);
printf("Memory allocation failed!\n");
}
}
}
/* Prompt user for matrix values */
printf("\n");
for (row=0;row<rowsize1;++row)
for (column=0;column<columnsize1;++column)
{
printf("Enter value for position (%d, %d) in Matrix 1: ", row+1, column+1);
scanf("%d", &matrix1[row][column]);
}
printf("\n");
for (row=0;row<rowsize2;++row)
for (column=0;column<columnsize2;++column)
{
printf("Enter value for position (%d, %d) in Matrix 2: ", row+1, column+1);
scanf("%d", &matrix2[row][column]);
}
/* Initialize dimensions of output matrix */
int **output;
output = malloc(rowsize1*sizeof(*output));
if(matrix1)
{
int a;
for(a = 0; a < rowsize1; ++a)
{
output[a] = malloc(columnsize2*sizeof (*output[a] ));
if(output[a] == NULL)
{
/* malloc failed - free what was allocated and return error */
free(output);
printf("Memory allocation failed!\n");
}
}
}
/* Fill output matrix with zeroes */
for(counter1=0;counter1<rowsize1;++counter1)
for(counter2=0; counter2<columnsize2; ++counter2)
output[counter1][counter2]=0;
/* Multiply matrices together */
for (row=0; row<rowsize1; ++row)
for (column=0; column<columnsize2; ++column)
for(i=1;i<=rowsize2;++i)
output[row][column] = output[row][column] + (matrix1[row][i-1])*(matrix2[i-1][column]);
/* Output matrix */
printf("\nAnswer: \n");
for (counter1=0; counter1<rowsize1; ++counter1)
{
printf("\n");
for (counter2=0; counter2<columnsize2; ++counter2)
printf("%i ", output[counter1][counter2]);
}
printf("\n");
return EXIT_SUCCESS;
}