我写了一个代码,应该提示用户输入一个数字,然后根据这个数字,他们会被问到(请输入 1 到 4 之间的数字)* 用户选择的数字。然后,将比较他们的输入以查看二维数组(行或列)中是否存在任何匹配项。
- 请输入一个数字:(例如 2)
- 请输入 1 到 4 之间的数字:3
- 请输入 1 到 4 之间的数字:1
这是您的网格: (用随机数填充的 2x2 网格)
然后程序应该查看二维数组和用户编号(在本例中:3 和 1)之间是否有任何匹配,如果匹配,则应输出一条消息:做得好,如果没有匹配,则不好运气。
我已经做了一切,但我完全停留在比较步骤,我花了很长时间试图弄清楚,但这是我到目前为止所做的
4 2 4
3 1 1
4 3 3
这是我的代码示例:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <time.h>
#include <ctime>
using namespace std;
int Atemp = 0;
int Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';
int Umain;
const int maxNum = 2;
int intArray[maxNum];
void printGrid(int &Umain);
void fillIntArray(int array[], int size);
void reverseArray(int array[], int size);
void outputIntArray(int array[], int n);
bool compareGrid(int intArray[], int &Atemp, size_t rows, size_t cols, size_t n);
int main(){
cout << "Please Enter numbers between 1 and 12: ";
cin >> Umain;
do{
if(Umain <=12){
fillIntArray(intArray, maxNum);
//outputIntArray(intArray, maxNum);
printGrid(Umain);
}
}while (Answer == 'y');
compareGrid(intArray, Atemp);
return 0;
}
void displayOverview(){
}
void fillIntArray(int array[], int size){
do{
for (Utemp = Umain; Utemp > 0; Utemp--){
cout << "Please enter a number between 1 and 4: ";
cin >> Atemp;
}
}while (Answer == 'y');
}
/*
void outputIntArray(int array[], int Atemp){
for(int i = 0; i < Atemp; i++){
printf("%d", array[i]);
}
}
*/
void printGrid(int &Umain){
cout<<endl;
cout<<" ";
int i=1,j;
for(j = 0; j <= 4*Umain; j++){
if(j%4==2){
cout<<" ";
}
}
cout<<endl;
for(i = 0; i <= 2*Umain; i++){
for(j = 0; j <= 2*Umain; j++){
if(i%2==0){
if(j==0){
cout<<" ";
}
if(j%2==0){
cout<<" ";
}else{
cout<<"---";
}
}else{
if(j%2==0){
cout<<" | ";
}else cout<< (rand()%4+1);
}
}
if(i%2!=0){
cout<<" ";
}
cout<<endl;
}
cout<<" ";
for(j = 0, i = 1; j <= 4*Umain; j++){
if(j%4==2){
cout<< " ";
}
}
cout<<endl;
}
bool compareGrid(int intArray[], int &Atemp, size_t rows, size_t cols, size_t n) {
for (size_t i = 0; i < rows; i ++) {
for (size_t j = 0; j < cols; j ++) {
for (size_t k = 0; k < n; k ++) {
if (intArray[i][j] == Atemp[k]) {
return true;
}
}
}
}
return false;
}
最佳答案
- 您没有填充
intArray
任何值 -
intArray
不适用于compareGrid
, 它必须作为参数传递。 -
void compareGrid
与bool compareGrid
冲突, 原型(prototype)必须符合定义。 -
Atemp
是一个int
,而不是数组,因此尝试访问元素 (Atemp[i]
) 是未定义的,编译器不允许。 -
intArray
是内存中的一维数组,您正试图使用intArray[i][j]
访问它的第二个索引这同样是未定义和不允许的。如果你希望它被表示为一个二维数组,那么在编译时不知道数组的维度(或者至少除了第一个)你需要使用一个动态数组,一个int**
在这种情况下。或者,如果您想将二维数组作为一维数组存储在内存中,则必须完成索引intArray[i * rows + j]
.
如果使用前者,则compareGrid
成为
bool compareGrid(int** intArray, int &Atemp) {
for (size_t i = 0; i < 4; i ++) { // i ++ instead of ++i because ++i will skip
for (size_t j = 0; j < 4; j ++) { // first row, similarly ++j will skip first column
if (intArray[i][j] == Atemp) {
return true; // return true only if match is found
}
}
}
return false; // otherwise return false
}
但是请注意,您必须单独比较每个输入,而不是等到所有输入都已收集到,如果您需要等到所有输入都已收集到,那么您必须将它们存储到一个数组中(您仍然可以使用Atemp
但它必须是一个 int
的数组)但您还需要传递 Atemp
的维度,即
bool compareGrid(int** intArray, int* Atemp, size_t n) {
for (size_t i = 0; i < 4; i ++) {
for (size_t j = 0; j < 4; j ++) {
for (size_t k = 0; k < n; k ++) {
if (intArray[i][j] == Atemp[k]) {
return true; // Again, return true only when a match is found
}
}
}
}
return false; // This statement is ONLY reached if there are no matches
}
当然如果你不知道 intArray
的尺寸在编译时(看起来你没有)那么你也必须通过那些 -
bool compareGrid(int** intArray, int* Atemp, size_t rows, size_t cols, size_t n) {
for (size_t i = 0; i < rows; i ++) {
for (size_t j = 0; j < cols; j ++) {
for (size_t k = 0; k < n; k ++) {
if (intArray[i][j] == Atemp[k]) {
return true;
}
}
}
}
return false;
}
在哪里可以替代 intArray[i * rows + j]
对于 intArray[i][j]
如果需要用一维数组表示二维数组。
注意事项
如果您不填充 intArray
,这些比较方法都不起作用值在 fillIntArray
中, 像
for (int i = 0; i < size; i ++) {
intArray[i] = value; // Where {value} is an integer
}
或 int**
,
for (int i = 0; i < rows; i ++) {
for (int j = 0; j < cols; j ++) {
intArray[i][j] = value;
}
}
代替行
for (int i = Atemp; i < Atemp; i ++);
这是一个无用的语句,除了有效地检查 Atemp < Atemp
之外什么都不做。 (始终为 false - 无循环)。
补充说明
如果您使用动态二维数组 (int**
),那么您可以使用以下代码对其进行初始化
int** intArray = new int*[rows];
for (int i = 0; i < rows; i ++) {
intArray[i] = new int[cols];
}
在你的情况下rows == cols
,显然情况并非总是如此。
希望对你有帮助
关于c++ - 如何将二维数组值与用户输入进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53491276/