Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello,

We were given an exercise about sudoko in C++.

task: to determine if the set of integers is a valid soduko solution.

the 81 numbers are pre-defined in this code. so far i have this.

Problem: how do I pass a multi-dimensional array.

I kinda need to print the sudoku table first

C#
#include<iostream>
using namespace std;





int main(){
     int row, column;
     row = column = 9;
     int array[row][column] = { { 4, 5, 2, 3, 9, 1, 8, 7, 6 },
                                { 3, 1, 8, 6, 7, 5, 2, 9, 4 },
                                { 6, 7, 9, 4, 2, 8, 3, 1, 5 },
                                { 8, 3, 1, 5, 6, 4, 7, 2, 9 },
                                { 2, 4, 5, 9, 8, 7, 1, 6, 3 },
                                { 9, 6, 7, 2, 1, 3, 5, 4, 8 },
                                { 7, 9, 6, 8, 5, 2, 4, 3, 1 },
                                { 1, 8, 3, 7, 4, 9, 6, 5, 2 },
                                { 5, 2, 4, 1, 3, 6, 9, 8, 7 }
                                };

}





Im using a sample pointer code given to us but this is a single-dimensional array.
#include<iostream>
using namespace std;

void printArray(int anArray[], int aSize);



int main(){
	 int array[5] = {52,94,4,9,7};
	 printArray(array, 5);
	
}

void printArray(int anArray[], int aSize){
	cout << aSize << endl;
	for(int *ptr=anArray; ptr-anArray < aSize; ptr++){
		
	
		
		cout << *ptr << endl;
	}
} 



can anyone teach me how do I pass this array to a function?
Posted
Updated 17-Feb-15 3:56am
v4
Comments
Frankie-C 17-Feb-15 10:46am    
How have you declared the multidimensional array?
Pass it the same way.

1 solution

C#
#include<iostream>
#include<iomanip>
using namespace std;


const int iRow = 9;
const int iColumn =9;

void PrintArray(int iNum[][iColumn], int iR, int iC){
    for(int i=0;i<iR;i++){

        cout<< "\n";
        for(int j=0; j<iC; j++){
            cout<< "|"<<iNum[i][j]<<"|";
                     }
    }

}
int main(){

     int iSud[iRow][iColumn] = { { 4, 5, 2, 3, 9, 1, 8, 7, 6 },
                                { 3, 1, 8, 6, 7, 5, 2, 9, 4 },
                                { 6, 7, 9, 4, 2, 8, 3, 1, 5 },
                                { 8, 3, 1, 5, 6, 4, 7, 2, 9 },
                                { 2, 4, 5, 9, 8, 7, 1, 6, 3 },
                                { 9, 6, 7, 2, 1, 3, 5, 4, 8 },
                                { 7, 9, 6, 8, 5, 2, 4, 3, 1 },
                                { 1, 8, 3, 7, 4, 9, 6, 5, 2 },
                                { 5, 2, 4, 1, 3, 6, 9, 8, 7 }
                                };


    PrintArray(iSud,iRow,iColumn);
}



ok I finally found a way on how to print it, but it needs some more designs to make it look like a sudoku table. can someone help how I can make the upper and lower portions lines to make the numbers look like inside the box
 
Share this answer
 
Comments
Richard MacCutchan 17-Feb-15 11:47am    
You just need to use vertical bars and hyphens to separate rows and columns. Draw the design on paper and you can see how to calculate the number of extra characters in each plane.
Stefan_Lang 19-Feb-15 4:44am    
I'd use '-' for horizontal lines, '| for vertical lines, and either '+' or '*' for intersections.

Also, I'd consider adjusting the aspect ratio of the printed grid, since the horizontal spacing of characters is considerable less than the vertical spacing. Look at your array initialization as an example: it has a one line vertical spacing, but three characters horizontal spacing, but in spite of that discrepancy the grid of numbers looks almost quadratic! Therefore I'd add one space to the left and right of each number to get a better saspect ratio, like this:
+---+---+---+
| 4 | 2 | 7 |
+---+---+---+
| 5 | 3 | 9 |
+---+---+---+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900