So i started learning c quite recently and have a question.
I want to make a program that receives the number of lines and collumns of a matrix, a matrix of char, the number of pairs of coordinates and the coordinates. the program has to return the char (or chars) that are on those coordinates on the matrix.
---------------------------------------------------
example:
receives
2 3
ABC
DEF
2
1 1
1 2
returns
AB
---------------------------------------------------
this is how i tried to solve this problem:
---------------------------------------------------
Code:
#include
#define MAX 1000
int main() {
int nlin, ncol;
char mat[MAX][MAX];
int x[MAX], y[MAX];
int ncoords;
int l, c, n;
/* receiving variables and storing matrix.*/
scanf("%d%d\n", &nlin, &ncol);
for(l = 0; l < nlin; l++) {
for(c = 0; c < ncol; c++)
mat[l][c] = getchar();
getchar(); /* read the \n at the end of each line */
}
scanf("%d", &ncoords);
for(n = 0; n < ncoords; n++) {
scanf("%d%d", &x[n], &y[n]);
}
/*finding string with the received coordinates*/
int f;
int e;
int r;
char result [1000];
for ( r=0; ncoords != 0; ncoords--,r++,f++) {
for(l=1,f=0;x[f]!=l;l++);
for(c=1,e=0;y[e]!=c;c++)
result[r]=mat[f][e];
}
printf("found string '%s'\n",result);
return 0;
}
---------------------------------------------------
For some reason that i can't seem to find, this solution is not right, may someone explain me why and point me into the right direction?