I have an assignment that wants me to create two squares with the dimensions of the user input. Once I have the length and width inputs from the user I pass the info to my class which creates the square with a 2D array thats declared in my private member variables. I then fill the array with 1s and 0s.
My question is, once my I create two objects, or squares, of the same class how do I compare the two objects that are arrays and tell the compiler "if square one has a 1 in the same spot([row][column]) as square 2 then create a 3rd array and put a 1 in the same spot.
//Grid.h
#ifndef OOP_HW3_GRID_H
#define OOP_HW3_GRID_H
class Grid {
private:
int length, width; //variables for size of grids
// Initialize Array
staticconstint numRows = 20; //rows for 2D Array
staticconstint numCols = 20; //columns for 2D Array
int array1[numRows][numCols]; //Array of Grid One
public:
Grid(); //Default Constructor
Grid(int length, int width); //initializes all length/width
// Set Setters functions
// set length of grids
void setLength(int x1) { length = x1; }
// Set Width of grids
void setWidth(int y1) { width = y1; }
// Set Getters functions
// Get lengths of the 3 grids
int getLength() { return length; }
// Get Widths of the 3 grids
int getWidth() { return width; }
// Fill array
void fillArray(); //Fills Array
// Display array
void displayArray();
};
#endif //OOP_HW3_GRID_H
//Grid.cpp
#include "Grid.h"
#include <iostream>
#include <cstdlib>
usingnamespace std;
Grid::Grid()
{
length = 0;
width = 0;
}
Grid::Grid(int _length, int _width)
{
length = _length;
width = _width;
}
// Function fills array
void Grid::fillArray()
{
for (int i = 0; i < length; i++) //Fills row
for (int j = 0; j < width; j++) //Fills column
{
int randNum = rand() % 2; //Generates random number divides by modulo of 2
if (randNum == 1 || randNum == 0) //if randNUm is equal to 0 or 1
{
array1[i][j] = randNum;
}
}
}
// Function displays array
void Grid::displayArray()
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < width; j++)
{
cout << array1[i][j] << " ";
}
cout << endl;
}
}
int main() {
int x1, y1; //x & y size for 1st grid
int x2, y2; //x & y size for 2nd grid
Grid Object1;
cout << endl;
cout << "Lets build two grids and compare which ones " << endl
<< "have matching squares filled with '1' !"<< endl
<< "Please enter the width (Between 1-20) of grid one : ";
cin >> x1; //user inputs x of grid1
cout << "Please enter the length(Between 1-20) of grid one: ";
cin >>y1; //user input y of grid1
cout << "Now enter the width (Between 1-20) of grid TWO: ";
cin >> x2; //user inputs x of grid2
cout << "And, finally, the length (Between 1-20) of grid TWO: ";
cin >> y2; //user inputs y of grid2
// Use setters for setting dimensions of Grids
Object1.setLength(x1); //Length of Grid one set
Object1.setWidth(y1); //Width of Grid one set
// Equivalent to previous 2 lines
Grid Object2(x2, y2); //uses constructor to set 2nd object and array.
// Fills two arrays
Object1.fillArray();
Object2.fillArray();
// Displays two arrays
Object1.displayArray();
cout << "\n\n";
Object2.displayArray();
// Compare two arrays
return 0;
}