2D array pointers (almost got it)
I am trying to get this to work. I think I am close.
I am asking users to input for a matrix.
num rows and num columns;
I then want them to input each value that they want in each row.
It complies, and I am pretty sure the pointers are allocated correctly.
I think the loop is not inputing each user input for the values.
Code:
#include "stdafx.h"
#include "math.h"
int main(int argc, char* argv)
{
int i,j;
int **matrix = NULL;
int numRows;
int numCol;
printf( "Enter the number of rows: ");
scanf( " %d", &numRows);
printf( "Enter the number of columns: ");
scanf( " %d", &numCol);
matrix = malloc(numRows * sizeof(*matrix));
for (i = 0; i < numRows; i++)
{
matrix[i] = malloc(numCol * sizeof(*matrix[i]));
printf( "Enter the values in row %d: ", i + 1);
for (j = 0; j < numCol; j++)
{
matrix[i][j] = getchar();
}
}
for (i = 0; i < numRows; i++)
{
for (j = 0; j < numCol; j++)
{
printf("%d",matrix[i][j]);
}
printf("\n");
}
for (i = 0; i < numRows; i++)
{
free(matrix[i]);
}
free(matrix);
return 0;
}
I think this is not efficient
Code:
matrix[i][j] = getchar();
Am I blind??