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.
Answer from Gavin H on Stack Overflow
🌐
YouTube
youtube.com › godfredtech
2d arrays explained | C program to fill in a 2D array - YouTube
This video attempts to explain 2d arrays and give a nice way of conceptualizing them. This program will fill in a 2D Array. I provide full explanation on my ...
Published   June 18, 2020
🌐
IONOS
ionos.com › digital guide › websites › web development › 2d arrays in c
How to create and use 2D arrays in C - IONOS
January 7, 2025 - 2D arrays in C (or mul­ti­di­men­sion­al arrays) are most commonly used to create mul­ti­di­men­sion­al data struc­tures. In the following example, a two-di­men­sion­al array is filled al­ter­nate­ly with zeros and ones to represent a chess­board:
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 175432-teach-me-fill-2d-array-loop-commands.html
Teach Me: Fill a 2d array with loop commands
#include <stdio.h> main() { int x; int y; int array[10][2]; for ( x = 0; x < 10; x++ ) { for ( y = 0; y < 2; y++) { array[x][y] = 20-(2*x+y); } } for ( x = 0; x < 10; x++ ) { for ( y = 0; y < 2; y++) { printf( "%d ", array[x][y] ); } printf( "\n" ); } } Am I on the right track? ... It looks okay to me. Was the assignment specific about where the numbers go? ... Yes, the first program is closer to what my instructor wants. There was an earlier assignment where we had to fill an array manually with the same numbers (1 to 20 in descending order).
🌐
Quora
quora.com › How-does-one-fill-a-two-dimensional-array-with-integers-in-C
How does one fill a two-dimensional array with integers in C++? - Quora
Answer (1 of 2): C does not support multidimensional arrays. However, it does support arrays of arrays. Which is essentially the same thing, and gives you the key to how to initialize them. Suppose you have an array of 2 arrays of three [code ]int[/code]. The initializer is then two array initi...
🌐
YouTube
youtube.com › watch
Fill A 2D Array With Random Values | C Programming Example - YouTube
Example of initializing a 2D array with random integer numbers between 1 and a max integer using C. Source code: https://github.com/portfoliocourses/c-examp...
Published   December 17, 2021
🌐
Cplusplus
cplusplus.com › forum › beginner › 37796
Filling a 2D Array. - C++ Forum
March 2, 2011 - in my assignment he wanted us to define a constant that was the size of the array. Which it had to be a grid of 8x8. ... 2D arrays are nothing just set 1D arrays. Consider them in Matrix form. In you program it is 8*8 array. Compare the same with 8*8 matrix.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › c multidimensional arrays (two dimensional array in c)
Two Dimensional Array in C | Multidimensional Array in C - Scaler Topics
May 1, 2024 - Here, dataType can be any valid C data type, arrayName is your chosen identifier for the array, and numberOfRows and numberOfColumns define the array's size. This blueprint is crucial for preparing your data storage with precision. Once declared, initializing a 2D array breathes life into it, filling its cells with initial values.
🌐
Stack Overflow
stackoverflow.com › questions › 35442444 › how-do-i-fill-an-allocated-2d-array-with-data-from-a-file
c - How do I fill an allocated 2D array with data from a file? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... #define LINES 40 int i,j,k = 0; char **c; char tmp; // allocate key array memory if( (c = malloc(LINES*sizeof(char*))) == NULL) printf("Error allocating memory\n"); for(i=0;i<LINES;i++){ c[i] = malloc(10*sizeof(char)); } ... AsfAGHM5om ~sHd0jDv6X uI^EYm8s=| .... How can I fill the array allocated above with data from that file (for example using fgets or fgetc)?
🌐
Stack Overflow
stackoverflow.com › questions › 23087150 › printing-and-filling-a-2d-array-in-c
Printing and filling a 2D array in C - Stack Overflow
November 25, 2016 - F. Fill the array with a random number series. P. Print the array. Q. Query the array. Z. Terminate the program. ... -- Query will ask the user to enter a location (index), and will then print the number in that location. If the user enters an invalid location, it will print a message stating so. ... Copy/* Generate a random number permutation.*/ #include < stdio.h > #include < stdlib.h > #define ARY_SIZE 20 //Function Declarations void bldPerm (int randomNos[]); void printData (int data[], int size, int lineSize); int main(void) { //Local Declarations int randNos[ARY_SIZE]; //Statements print
Top answer
1 of 1
2

As for the code structure, given that the entire 2D array is only local to print_tables and the only purpose is to print the array, you might just as well remove the array altogether and print the values directly instead of storing them temporarily only to re-iterate over the array printing them.

I'd also handle the “header” row and column separately from the data rows; this simplifies the code by removing the ifs, so it's easier to track what's going on. For example:

#include <stdio.h>
#include <math.h>

const static float MAX_ELAS = 1.0;

void print_tables(unsigned minDimples, unsigned maxDimples, float minElasticity) {
    const unsigned elasticityLength = (unsigned) ceilf(MAX_ELAS / minElasticity);
    float elasticity = minElasticity;

    // Header row
    (void) printf(" "); // Empty (0,0) cell
    for (unsigned col = 0; col < elasticityLength; ++col) {
        (void) printf("\t%.02f", elasticity);
        elasticity += minElasticity;
    }

    for (unsigned dimples = minDimples; dimples <= maxDimples; ++dimples) {
        float dimpleMultiplier = 120.0f - dimples;
        dimpleMultiplier = 800.0f - (dimpleMultiplier * dimpleMultiplier);
        elasticity = minElasticity;

        (void) printf("\n%u", dimples); // Header column
        for (unsigned col = 0; col < elasticityLength; ++col) {
            float distance = elasticity * dimpleMultiplier;
            (void) printf("\t%.1f", distance);
            elasticity += minElasticity;
        }
    }
    (void) printf("\n"); // Terminate last row
}

int main (void) {
    // TODO: Read input instead of hard-coded values (or use command-line?)
    print_tables(121, 125, 0.14);
    return 0;
}

Outputs:

         0.14    0.28    0.42    0.56    0.70    0.84    0.98    1.12
121     111.9   223.7   335.6   447.4   559.3   671.2   783.0   894.9
122     111.4   222.9   334.3   445.8   557.2   668.6   780.1   891.5
123     110.7   221.5   332.2   443.0   553.7   664.4   775.2   885.9
124     109.8   219.5   329.3   439.0   548.8   658.6   768.3   878.1
125     108.5   217.0   325.5   434.0   542.5   651.0   759.5   868.0

As a suggestion for further cleaning up, perhaps specify elasticityStep which is the increment of elasticity between columns, and separate minElasticity, maxElasticity instead of elasticityLength. Then the column loops would be like:

for (elasticity = minElasticity; elasticity <= maxElasticity; elasticity += step)
🌐
BeginnersBook
beginnersbook.com › 2014 › 01 › 2d-arrays-in-c-example
Two dimensional (2D) arrays in C programming with example
We already know, when we initialize a normal array (or you can say one dimensional array) during declaration, we need not to specify the size of it. However that’s not the case with 2D array, you must always specify the second dimension even if you are specifying elements during the declaration.
🌐
Stack Overflow
stackoverflow.com › questions › 53460048 › fill-a-2d-array-from-a-file-in-c
Fill a 2d array from a file in C - Stack Overflow
November 24, 2018 - Variations on fscanf(in, "%d,%d\n", &x, &y), as in OP's prior question, fail to detect end-of-line. The '\n' in the format will match any white-space on input including '\n', ' ', '\t',etc. Simplistic usage of fscanf(..., "%d",...) can readily fail as "%d" will consume all leading white-space with no discrimination between '\n' and other white-spaces. How can i fill my array ...