简体   繁体   中英

Filling two dimensional array C

#include <stdio.h>

int main()
{
    //Variables
    int x=0,y=0;
    int my_data[7][7];
    for (x=0;x<9;x++) {
        printf("Enter rows:");
        scanf("%i",&x);
       // printf("%i\n",my_data[x][y]);
    }
    for(y=0;y<9;y++) {
        printf("Enter columns: ");
        scanf("%i", &y);
        //printf("%i\n",my_data[x][y]);
    }
    for(y=0;y<9;y++) {
    printf("%i\n",my_data[0][y]);
    }
    return 0; //Return process complete.
}
  1. declares an 8X8 integer array my_data
  2. prompts the user for data to fill the 8X8 array my_data
  3. prints the sum of all the elements for each row
  4. prints the sum of all the elements in the 8X8 array
  5. prints the sum of all the elements in the diagonal of the 8x8 array.

What did I do wrong? When I enter 1,2,3,4,5,6,7,8; 1,2,3,4,5,6,7,8 I get back:

3612392
2686632
1966142592
3604480
0
1966142601
1825884643
4
3612396

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;
    }
}
for (x=0;x<9;x++) {
    printf("Enter rows:");
    scanf("%i",&x);
   // printf("%i\n",my_data[x][y]);
}

What happens here is that you change the value of x . You do not save anything on the array.

Similarly for y

Also you should iterate from 0 to 6 (inclusive)

Try

int i;
for(i = 0 ; i < 7 ; i++)
{
     scanf("%d" , &my_data[0][x]);
}

Or :

int row , col;
for (row=0; row<7; row++) 
{
    for (col=0; col<7; col++) 
    {
        scanf("%d" , &ticTacToeBoard[row][col]);
    }
}

What you see is the values that your array has when it was declared. The reason for that is when you declare a variable in C without initializing it, as in your case, your program just uses enough memory to store your data into the memory but does not set that memory to 0 , hence seeing what the contents of the memory are from previous usage.

int my_data[7][7]; is a 7 x 7 array. Use int my_data[8][8]; for 8 x 8.

my_data is an array with space for 49 int , not 64!

In your for loops you are changing the loop control variable itself by using scanf with its address!

I think you want:

#include <stdio.h>

#define ROWS 8
#define COLUMNS 8

int main(void) {
  int my_data[ROWS][COLUMNS];
  int rows[ROWS];
  int cols[COLUMNS];
  size_t i, row, col;

  /* input rows */
  printf("Enter rows: ");
  fflush(stdout);
  for (i = 0; i < ROWS; i++) scanf("%d", &rows[i]);

  /* input columns */
  printf("Enter cols: ");
  fflush(stdout);
  for (i = 0; i < COLUMNS; i++) scanf("%d", &cols[i]);

  /* calculate sums */
  for (row = 0; row < ROWS; row++) {
    for (col = 0; col < COLUMNS; col++) {
      my_data[row][col] = rows[row] + cols[col];
    }
  }

  /* print results */
  for (row = 0; row < ROWS; row++) {
    for (col = 0; col < COLUMNS; col++) {
      printf("%d ", my_data[row][col]);
    }
    puts("");
  }

  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM