How to compare 2D array values with the user input?

时间:2018-11-27 00:55:14

标签: c++ arrays multidimensional-array

I have written a code which should prompt the user to enter a number and then according to that number, they will be asked (Please enter a number between 1 and 4)* the number the user chose. Then, their input will be compared to see if there is any match in the 2D array(Rows or columns).

  • Please enter a number: (for ex. 2)
  • Please enter a number between 1 and 4: 3
  • Please enter a number between 1 and 4: 1

Here is your grid: (2x2 grid filled filled with random numbers)

The program then should look if there is any match between the 2D array and the users numbers (in this case: 3 & 1), if they get a match, a message should output: Well done, if no match, Bad luck.

I already did everything but I am completely stuck at the comparing step, I spent way to long trying to figure it out, but this what I got to up till now

 4  2  4
 3  1  1
 4  3  3

Here is a sample of my code:

#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;
}

1 个答案:

答案 0 :(得分:1)

  • 您没有用任何值填充intArray
  • intArray的范围不适用于compareGrid,它必须作为参数传递。
  • void compareGridbool compareGrid冲突,原型必须匹配定义。
  • Atempint,而不是数组,因此编译器未定义和禁止尝试访问元素(Atemp[i])。
  • intArray是内存中的一维数组,您正尝试使用intArray[i][j]访问其第二个索引,该索引同样是未定义和不允许的。如果希望将其表示为二维数组,则在编译时不知道数组的大小(或至少除第一个以外的所有大小),需要使用动态数组,在这种情况下为int**。另外,如果要将2D数组作为1D数组存储在内存中,则必须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 s的数组),但您还需要传递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;
}

如果需要使用1D数组表示2D数组,则可以用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-无循环)之外,什么也不做。


附加说明

如果您使用动态2D数组(int**),则可以使用以下命令对其进行初始化

int** intArray = new int*[rows];
for (int i = 0; i < rows; i ++) {
  intArray[i] = new int[cols];
}

在您的情况下为rows == cols,显然并非总是如此。

希望这会有所帮助

相关问题