Okay code that compiles with good indentation:
Code:
#include<stdio.h>
#include <stdlib.h>
void matrix_display(int **A, int row_size1, int col_size1);
int **matrix_alloc(int row_size1, int col_size1);
void matrix_fill(int **A, int row_size1, int col_size1);
int main(void)
{
int row_size1[5],col_size1[5],flag=1;
int **A = NULL;
int **p1, **p2;
char c,k1,k2;
do {
printf("\ncmd > ");
c = getchar();
if (c == 'A') {
scanf("%d %d", &row_size1[0], &col_size1[0]);
A = matrix_alloc(row_size1[0],col_size1[0]);
matrix_fill(A,row_size1[0],col_size1[0]);
matrix_display(A,row_size1[0],col_size1[0]);
}
}
while (flag == 1);
return 0;
}
void matrix_display(int **A, int row_size1, int col_size1)
{
int i, j;
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");
}
}
int **matrix_alloc(int row_size1, int col_size1)
{
int i;
int **result = malloc(row_size1 * sizeof(int *));
for (i = 0; i < row_size1; i++) {
result[i] = malloc(col_size1 * sizeof(int));
}
return result;
}
void matrix_fill(int **A, int row_size1, int col_size1)
{
int i, j;
// printf("Enter matrix of %d rows and %d columns\n", row_size1, col_size1);
for (i = 0; i < row_size1; i++) {
for (j = 0; j < col_size1; j++) {
scanf("%d", &A[i][j]);
}
}
if(j>0){
fprintf(stderr, "the number of entries is not valid\n");
exit(1);}
}
how the code works when I don't have the last j>0 line in it
Code:
(user) A 2 2 1 2 3 4
(displays a 2 by 2 matrix 1 2 3 4)
now how the code works with the j>0 statement
A 2 2 1 2 3 4
(displays 2 by 2 matrix)
AND
"the number of entries is not valid"
This statement "the number of entries is not valid should only print if the user inputs lets say 5 values for a 2 by 2 matrix.
p.s. I did read the wiki page for indentation if I still didn't do it right please let me know provided you also respond to this thread