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?
You need to read in the input text into your array
gridand pass it on.Doesn’t do what you think it does, it tries to assign an object of type
istreamtogrid[ i ][ j ]however suffices.
Also, note in C++ the dimensions follow the identifier as in: