views:

128

answers:

4

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?

+5  A: 

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];
dirkgently
+2  A: 

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.

IVlad
@Vlad, as a side question, if I wanted to make it so they couldn't change grid in the method, is this where I would declare a const reference? Or does that just prevent them from changing what grid is pointed to?
Salaban
You would just change the function to `void readAPuzzle(const int grid[puzzle_height][puzzle_width])`if I understand you right, however this prevents change to any elements of grid. Is that what you want, or do you mean something else? You can't change grid itself anyway, because it's an array. If you were to pass grid as a pointer to a pointer, then you could talk about changing grid itself, which could be prevented by passing it as a const pointer. Read more about const pointers here: http://www.codeguru.com/cpp/cpp/cpp_mfc/general/article.php/c6967
IVlad
And here: http://www.parashift.com/c++-faq-lite/const-correctness.html
IVlad
+2  A: 

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.

Philip Potter
A: 

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);
}
den bardadym