Question

If I have the following Java code:

int[][] readAPuzzle()
{
    Scanner input = new Scanner(System.in);

    int[][] grid = new int[9][9];
    for (int i=0; i<9; i++)
      for (int j=0; j<9; j++)
        grid[i][j] = input.nextInt();

    return grid;
}

public static void main(String[] args) {
    // Read a Sudoku puzzle
    int[][] grid = readAPuzzle();
}

How can I convert this to C++? I get hung up on passing the array. Here is my attempt:

#include <iostream>

using namespace std;

const int puzzle_width = 9;
const int puzzle_height = 9;

void readAPuzzle(int (&grid)[puzzle_height][puzzle_width])
{
    for(int i = 0; i < 9; i++)
        for(int j = 0; j < 9; j++)
            grid[i][j] = cin >> grid[i][j];

    return;
}

int main()
{
    int[9][9] grid;
    readAPuzzle(grid);
}

What am I doing wrong in general?

Was it helpful?

Solution

You need to read in the input text into your array grid and pass it on.

grid[i][j] = cin >> grid[i][j];

Doesn't do what you think it does, it tries to assign an object of type istream to grid[ i ][ j ]

cin >> grid[i][j]; 

however suffices.

Also, note in C++ the dimensions follow the identifier as in:

int grid[9][9];

OTHER TIPS

Try

#include <iostream>
using namespace std;

const int puzzle_width = 9;
const int puzzle_height = 9;

void readAPuzzle(int grid[puzzle_height][puzzle_width])
{
    for(int i = 0; i < 9; i++)
        for(int j = 0; j < 9; j++)
            cin >> grid[i][j];
}

int main()
{
    int grid[9][9];
    readAPuzzle(grid);
}

In general, arrays are automatically passed by reference, and array sizes go after the name of the array not after their type.

And if you declared constants, you should always use puzzle_width and puzzle_height (perhaps shorten their names though) and not magic numbers like 9.

The simple answer is to use vectors instead of arrays. C++'s rules for passing arrays as function parameters are esoteric and derived from C. Here are some of the issues:

  • You can't use arrays for long without understanding and using pointers

Array subscripting is pointer subscripting. Arrays are accessed using pointer arithmetic. Arrays as function parameters are actually pointers in disguise.

  • Functions don't get information about array size when taking an array argument

Consider the declaration:

void inc_all(int myarray[]); /* increments each member of the array */

Unfortunately, that array parameter is not an array parameter! It's actually a pointer parameter:

void inc_all(int *myarray); /* Exactly the same thing! */

And a pointer doesn't know how many items are in the sequence it points at. As a result this function cannot have the information necessary to know when the array stops. You either need to pass the length:

void inc_all(int *myarray, size_t len); /* size_t rather than int */

or you need to use a sentinel value to mark the end of the array. Either way, an array is not a self-contained encapsulated datatype like a vector is.

  • You can't pass an arbitrarily-sized two-dimensional array to a function

If you try to create a function which takes a two-dimensional array:

void inc_all(int myarray[][]); /* XXX won't compile! */

it won't compile. The problem is you have an indeterminate length array of indeterminate length arrays of ints. The outer array doesn't know how large its members (the inner arrays) are and therefore doesn't know how to step through them in memory. You need to specify the size of the inner arrays:

void inc_all(int myarray[][10]);

at which point your code is probably not as general as you were hoping it was going to be.

If you use vectors and vectors of vectorss, these problems don't arise because the vectors themselves know how many members they have and carry that information with them.

If you still want to learn more about arrays and pointers I recommend section 6 of the comp.lang.c FAQ.

I think your should to use your constants first of all. You can pass as value pointer as well as ref on pointer (both 4bytes), also it is a pointer on memory (it is valueble).

#include <iostream>
using namespace std;

const int puzzle_width = 9;
const int puzzle_height = 9;

void readAPuzzle(int grid[puzzle_height][puzzle_width])
{
    for(int i = 0; i < puzzle_height; i++)
        for(int j = 0; j < puzzle_width; j++)
            cin >> grid[i][j];
}

int main()
{
    int grid[puzzle_height][puzzle_width];//I think your need to do this
    readAPuzzle(grid);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow