DanielAA9
DanielAA9

Reputation: 91

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

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).

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

Upvotes: 1

Views: 1046

Answers (1)

William Miller
William Miller

Reputation: 10320

  • You are not populating intArray with any values
  • intArray is not scoped to be available to compareGrid, it must be passed as a parameter.
  • void compareGrid conflicts with bool compareGrid, the prototype must match the definition.
  • Atemp is an int, not an array, so trying to access an element (Atemp[i]) is undefined and disallowed by the compiler.
  • intArray is a one dimensional array in memory that you are trying to access a second index of with intArray[i][j] which is similarly undefined and disallowed. If you want it to be represented as a two dimensional array then without knowing the dimensions of the array at compile time (or at least all but the first) you need to use a dynamic array, an int** in this case. Alternatively, if you want to store the 2D array as a 1D array in memory then the indexing must be done intArray[i * rows + j].

  If you use the former, then compareGrid becomes

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
}

But notice that you must compare each input on its own, instead of waiting until all inputs have been gathered, if you need to wait until all inputs are gathered then you would have to store them to an array (you can still use Atemp but it will have to an array of ints) but you will also need to pass the dimension of Atemp, i.e.

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
}

Of course if you don't know the dimensions of intArray at compile time (which it appears you do not) then you must pass those as well -

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

Where you can substitute intArray[i * rows + j] for intArray[i][j] if you need to use 1D array to represent the 2D array.


Note

None of those comparison methods will work if you do not populate intArray with values in fillIntArray, something like

for (int i = 0; i < size; i ++) {
  intArray[i] = value;      // Where {value} is an integer
}

or for int**,

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

in place of the line

for (int i = Atemp; i < Atemp; i ++);

which is a useless statement that does nothing but effectively check if Atemp < Atemp (always false - no loop).


Additional note

If you use a dynamic 2D array (int**) then you can initialize it with the following

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

where in your case rows == cols, which is obviously not always the case.

Hope this helps

Upvotes: 1

Related Questions