Originally Posted by
ssharish2005
So can you now take that pesudo code and implement it?
That pseudo code does not match the OP's specs, I would say!
Have a look at this:
Code:
#include <stdio.h>
#include <stdlib.h>
void showmatrix (int *matrix[], int rows, int cols) { /* generalized function */
int i, ii;
for (i=0; i<rows; i++) {
for (ii=0; ii<cols; ii++) printf("%5d", matrix[i][ii]);
printf("\n");
}
}
int main() {
int size, **matrix, i, ii;
printf("Size of matrix: ");
scanf("%d",&size);
matrix = malloc(size*sizeof(int*)); /* allocate for rows of int POINTERS */
for (i=0; i<size; i++) {
matrix[i]=malloc(size*sizeof(int)); /* allocate for columns of INTS */
printf("Data for row %d: ",i+1);
for (ii=0; ii<size; ii++) {
scanf("%d",&matrix[i][ii]);
}
}
showmatrix(matrix,size,size);
return 0;
}
Tested, -Wall, works (enter the row data as numbers separated by spaces).
The most important thing for you to understand is the use of malloc; a dynamic matrix is actually an array of pointers; each pointer represents a row.
This does not error check the user data!