Hey so I need to print out a matrix using malloc function. I have done that part and this is my code
Code:
#include<stdio.h>
#include <stdlib.h>
int main (void)
{
int row_size1,col_size1,row_size2,col_size2;
int **A;
int i = 0, j = 0;
//allocate space for 1 matrix
A = malloc(1 * sizeof (int *));
printf("Enter the number of rows and columns:");
scanf("%d %d",&row_size1,&col_size1);
A= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[i]=(int*)malloc(col_size1*sizeof(int));
}
printf("Enter the elements of the first matrix write 99 when done:");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[i][j]);
if (A[i][j] == 99) // 'x' is character variable I declared to \
break;
}
}
printf("The required matrix is\n");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
printf("%d ",A[i][j]);
printf("\n");
}
free(A);
}
So the catch is I don't want to ask the users seperately the number of rows and columns of a matrix. I want them to tell me at the same time they are telling me the matrix.
for example:
Code:
printf("What is your matrix")
(user) 2 2 1 2 3 4
so I want the code to record the first 2 values (2,2) as the number of rows and columns and then print out the rest of the matrix.
The only thing confusing me about this is how I am supposed to allocate space for a matrix I don't know the dimensions of
Thanks