← Back to topics
Topic

matrix in C language

g
goran_f2
that's how i input:

int m,n;
int i,j;
scanf("%d%d",&n,&m);
int matrix[n][m];
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d",matrix[i]+j);
}
}

d
demjan0001
you need to put reference on matrix[i][j],
scanf("%d",&matrix[i][j])
...
A
Al3kSaNdaR
This is how you input matrix of characters ->



char Matrix[1001][1001];
scanf("%d %d",&n, &m);
for ( int i = 0; i < n; i++)
{
scanf("%s",Matrix[i]);
}
D
Daniel93
it goes with it. For strings I'm allways doing without it, but its not a mistake cause its the same.
m
mbalunovic
You don't need to use '{' & '}' when you have only one statement in for, while, if...

Matrix of characters:


char mat[10][10];
for ( int i=0;i<n;++i )
scanf("%s",mat[i]);


Matrix of integers:


int mat[10][10];
for ( int i=0;i<n;++i )
for ( int j=0;j<n;++j )
scanf("%d",&mat[i][j]);


It is the same, but code is better without it if you don't need it. :D
d
demjan0001
it's reading ...

you can read

for (int i = 0; i < n; i++)
scanf("%s",matrix[i]);

and then go throw matrix and search for elements
or
when you are reading you need to put space before %c ...

scanf("[space]!!!%c",&c);
//it need to look like
scanf(" %c",&c);
d
demjan0001
hmmm ... why in for j <= m ??? it should be j < m ...

and i thing u will get memory reference because u are looking in matrix[i-1][j] and what if it's i = 0 u will look in matrix[-1!!!][j] ...
d
demjan0001
what if it's i = n and you are looking for i+1 ...
n can be 1000 and you are looking for matrix[1001][j] and for j same ...
in init just do bool matrix[1005][1005] ...
and in begining set hole matrix to false ...
memset(matrix, 0, sizeof(matrix));
or with 2 for loops ... whatever you want ... and try again
m
marcelo
got it thanks
d
demjan0001
np ... :D
you could use bool, no need for long ... but nevermind ...