如何在C ++中打印多维数组 [英] how do I print a multi-dimensional array in C++
问题描述
你好,
我们在C ++中接受了关于sudoko的练习。
任务:到确定整数集是否是有效的soduko解决方案。
此代码中预先定义了81个数字。到目前为止,我有这个。
问题:我如何传递一个多维数组。
我有点需要首先打印数独表格
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
#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?
推荐答案
#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我终于找到了如何打印它的方法,但它需要更多设计才能让它看起来像数独表。有人可以帮助我如何制作上部和下部线条使数字看起来像在框内
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
这篇关于如何在C ++中打印多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!