-1

I'm creating a program that depends on a set of 2d arrays. In the part that I'm currently working on, the user first inputs the desired (square' side) size and the program is supposed to initialize all the arrays and fill them with 0's, except for the 0 row and column, where labels are to be. The resulting array has to look somewhat like this:

0 1 2 3 4 ...
1 0 0 0 0 ...
2 0 0 0 0 ...
3 0 0 0 0 ...
4 0 0 0 0 ...
...

The code I've written looks okay, but once the labeling was implemented, the whole inside (labels not affected) started filling up with some random numbers, with 0's appearing about every 2nd field. This piece of code is made to work independently for now, but I'll move some of the variables out to global scope for connecting the pieces:

 int show_Board(int size){
    size++;
    int board[size][size];
    int i=0, x=0;
    for (i=0; i<size; i++){     //filling up the array
        for (x=0; x<size; x++){
            board[i][x]=0;  
        }
    for (i=0; i<=size; i++){    //filling in labels on the borders
        board[i][0]=i;          // only row 0 breaks if this loop is disabled
        for (x=0; x<=size; x++){
            board[0][x]=x;
        } 
    }       
    }
    for (i=0; i<size; i++){        //displaying the result
        for (x=0; x<size; x++){
            cout <<"  "<< board[i][x];
        }
        cout << endl;
    }
}

int main(){
  show_Board(20);
}

My question is how to make it so that the array fills up correctly (ie. labels and 0's)?

Cloudii
  • 1
  • 3
  • 5
    And when you used your debugger to run your program, what did you see? This is what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all bugs in this and all future programs you write, without having to ask anyone for help. – Sam Varshavchik Feb 01 '20 at 16:11
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Feb 01 '20 at 16:19
  • Thank you both for your advice. Unfortunately, trying to use the debugger has just crashed my dev a moment ago and I seem to not even know how to open all the files back up so they actually properly appear in the project tree and not as just individual source code files. There's still a long way to go for me – Cloudii Feb 01 '20 at 16:53
  • ...and/or I can't understand what's wrong in the debugger output now :/ – Cloudii Feb 01 '20 at 17:03

1 Answers1

2
for (int i = 0; i < size; i++){    
    for (x=0; x < size; x++){
            board[i][x]=0;  
    }
    for (int i = 0; i <= size; i++){    
        board[i][0]=i;          
        for (int x = 0; x <= size; x++){
            board[0][x]=x;
        } 
    }       
}

This is a snippet from your code, just slightly cleaned up. Notice that you close the first for loop in the wrong place, so it actually has the loops for setting the labels inside of it. This is, unfortunately, a result of writing ugly code. Not only is this code poorly written, it does more work than it needs to. It's easier to see mistakes, fix and optimise nice code, so that's something you should definitely look into.

EDIT: You also use i < size in the first loop, but i <= size in the second one, which will cause the loop to run for 1 additional time. I don't think this is what you actually want.

I'm not sure why you increment size at the beginning of the function, as that will create a square 1 bigger than the user asks.

Here's my solution:

#include <iostream>

int show_board(int size) {

    // size++;
    int board[size][size];

    for (int i = 0; i < size; ++i) {

        for (int j = 0; j < size; ++j) {
            // Set board tile to 0.
            board[i][j] = 0;
        }

        // Set vertical label.
        board[i][0] = i;
        // Set horizontal label.
        board[0][i] = i;

    }

    // Display board
    for (int i = 0; i < size; ++i) {
        for (int j = 0; j < size; ++j) {
            std::cout << board[i][j] << " ";
        }
        std::cout << std::endl;
    }

}

int main() {
    show_board(5);
    return 0;
}
Logorrhea
  • 38
  • 5
  • 1
    Thank you soo much! The increment of size is there because the user input size is actually the size of the playable field. Therefore it has to be adjusted for the presence of labels. If it wasn't there, the user would only have a 4x4 playable board when they ask for 5 etc. – Cloudii Feb 02 '20 at 12:03