Hi guys,
I'm a noob so please bear with me.
I'm trying to write some values in a multidimensional array with a variable length and then to print the values in row/col format. The problem is that it prints out wrong values. In the case of a 2*2 matrix it prints the last 2 values entered. Why is that?
Don't mind the commented out lines.
Thank you, guys.
Code:
Code:
#include <stdio.h>
//#define MAX 100
int main(void)
{
int rowA, colA, rowB, colB;
rowA = colA = rowB = colB = 0;
int i, j;
// int N = -1;
int A[rowA][colA];//, B[0][0], C[0][0];
// while ((N <= 0) || (N > MAX))
// {
// printf("Enter the No. of elements for arrays >>> ");
// scanf("%i", &N);
// if ((N > MAX) || (N <= 0))
// {
// printf("Up to '100' and bigger than '0'.\n");
// //printf("Enter the No. of elements for arrays >>> ");
// }
// }
printf("\nEnter the No. of rows for matrix 'A' >>> ");
scanf("%i", &rowA);
printf("\nEnter the No. of cols for matrix 'A' >>> ");
scanf("%i", &colA);
// printf("\nEnter the No. of cols for matrix 'B' >>> ");
// scanf("%i", &colB);
// rowB = colA;
for (i = 0; i < rowA; i++)
{
for (j = 0; j < colA; j++)
{
printf("\nEnter the value of A[%i][%i] >>> ", i, j);
scanf("%i", &A[i][j]);
printf("%i", A[i][j]); // checking if values has been entered corectly
}
}
// for (i = 0; i < rowB; i++)
// {
// for (j = 0; j < colB; j++)
// {
// printf("\nThe value of B[%i][%i] is >>> ", i, j);
// scanf("%i", &B[i][j]);
// }
// }
printf("\nThis is the matrix 'A':\n");
for (i = 0; i < rowA; i++)
{
for (j = 0; j < colA; j++)
{
printf("%i ", A[i][j]);
}
printf("\n");
}
return 0;
}