# Thread: Defining a Matrix ..

1. ## Defining a Matrix ..

Hi, first i want user to enter the matrix dimension. User have to define a number cuz of the Matrix is square. After that i want user to enter rows values row by row. I want it to look like that;

Enter the 1st row: bla bla
Enter the 2nd row: bla bla
.
.
Enter the n row: bla bla

and so on.

I guess i have to use 'for' to make this but i cant carry out. So how could i do ?

Thanx in advance..

2. Here what i ve done,its not working. it still gets all rows in once.

Code:
```for(r=0;r<n;r++)
{
printf("Enter the row \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&m1[i][j]);
}
}```

3. Code:
```print "Enter the no. of ROW"
read ROW

print "Enter no. of COLUMN"
read COLUMN

for i < ROW
for j < COLUMN
read a[i][j]

print "This is what you entered"

for i < ROW
for j < COLUMN
print a[i][j]```
That is how you should go ahead on doing this. So can you now take that pesudo code and implement it?

-ssharish

4. 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!

5. And the most important thing YOU can learn is to stop doing the whole goddamn thing for people who don't do anything!

Quzah.

6. Originally Posted by MK27
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!
And the code which you've is after all a memory leak!

-ssharish

7. I didnt want the whole code. At least i didnt want to mean it, i just stucked at some point and asked for a advice that pseudo code however its not i was asking was highly enough for me. I havent have enough time to continue my project since my post, thanks for answers(Especially MK27). Later im gonna ask a few things more.

Popular pages Recent additions