Several issues right away:
- The array is 7x7, you want it to be
int my_data[8][8]; - Instead of using the number for the size of the array over and over, define a constant
#define FOO 8 - The loops are not properly bounded, should be
for (x=0;x<8;x++). (again, see my note on using a defined constant for the size) - You are not saving the value to the array, you are saving it to the iterator variable.
- With two separate for loops you will not be able to fill the entire table, reconsider this structure because you will likely have to use nested loops.
Several issues right away:
- The array is 7x7, you want it to be
int my_data[8][8]; - Instead of using the number for the size of the array over and over, define a constant
#define FOO 8 - The loops are not properly bounded, should be
for (x=0;x<8;x++). (again, see my note on using a defined constant for the size) - You are not saving the value to the array, you are saving it to the iterator variable.
- With two separate for loops you will not be able to fill the entire table, reconsider this structure because you will likely have to use nested loops.
You can use one loop nested in the other:
#define SIZE 8
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("(%d,%d): ", i+1, j+1);
scanf("%d", &value);
array[i][j] = value;
}
}
Videos
scanf takes the address of the variable that is being read and returns the number of items read. It does not return the value read.
Replace
int i = scanf("%d",&i);
int y = scanf("%d",&y);
by
scanf("%d",&i);
scanf("%d",&y);
and
int r[a][b] = scanf("%d",&a,&b);
by
scanf("%d",&r[a][b]);
EDIT:
You are using variable length array (VLA) in your program:
int r[i][y];
as i and y are not constants and are variables. VLA are a C99 standard feature.
you have to allocate the 2D array dynamically cause you don't know it size in compilation.
replace
int r[i][y];
with
int *r = malloc(i*y*sizeof(int));
and when finish, add:
free(r);
*and also the SCANF errors, people already answered here.
First, you will need to allocate memory to this 2d array. For example, you getting the user input with variable "a" and "b" so you could declare the the array after the user input like this
int a, b, c, d;
printf("Number of rows and cumluns?\n");
scanf("%d%d", &a, &b);
int mat[a][b];
After you done allocate the memory you needed for the array, you will need to allocate the value you want in each position ( depends on c & d variables ), for example I used d variable values and assigned them to the 2nd array:
for (c = 0; c <= a; c++)
for (d = 0; d <= b; d++)
mat[c][d]=d;
Then you will probably want to print the array so, same For loops, just with print
for (c = 0; c < a; c++){
for (d = 0; d < b; d++)
printf("%d ",mat[c][d]);
printf("\n");
Output if a = 2 and b = 4

Also in your original code, you forgot a ";" after the mat[c][d] aswell.
Hopefully this will answer your question.
int a,b,i,j,k=1;
printf("Enter the no of row and col\n");
scanf("%d%d",&a,&b);
int arr[a][b];
for(i=0;i<a;i++){
for(j=0;j<b;j++){
arr[i][j]=k;
k++;
}
}
for(i=0;i<a;i++){
for(j=0;j<b;j++){
printf("%d ",arr[i][j]);
}printf("\n");
}
Just declare your function as:
void fill_matrix(char matrix[MATRIX_SIZE][MATRIX_SIZE], char c);
and then call it like this
fill_matrix(matrix, ' ');
void fill_matrix(char pointer[][MATRIX_SIZE], char c) {
will work, but if you want a pointer style:
#define MATRIX_SIZE 50
void fill_matrix(char (*pointer)[MATRIX_SIZE], char c) {
int i, j;
for (i = 0; i < MATRIX_SIZE; i++) {
for (j = 0; j < MATRIX_SIZE; j++) {
pointer[i][j]= c;
}
}
}
int main(int argc, char *argv[]) {
char matrix[MATRIX_SIZE][MATRIX_SIZE];
fill_matrix(matrix, ' ');
}
Note that there is no need to use & to pass the array
Also note that in this case you don't need a helper function,
memset(matrix, ' ', sizeof matrix);
will do the work