Question

Please write code in three files. main.cpp, Grid.h, Grid.cpp. Thank you. Build two grids which are...

Please write code in three files. main.cpp, Grid.h, Grid.cpp. Thank you.

Build two grids which are each X wide and Y long where X and Y are user inputs to determine the size of the grids. Fill each cell of both grids with a ‘0’. Randomly fill 1/3rd of the grid’s cells with a ‘1’. You must randomly fill each of the two grids separately so they do not have all the same squares filled with ‘1’. Compare the two grids (square by square comparison) to find squares which have a ‘1’ in both grids. Create a third grid which contains a ‘1’ in squares where both of the compared grids contain a ‘1’ and has a ‘0’ in any other square.

For this assignment, you will need to create a Grid Class and the appropriate methods to perform the above operations.

Example in a 2 by 2 grid:

Grid 1

0 0
1 1
1 0
1 0

Grid 2

0 0
1 0

Grid 3 (result)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<cmath>
#include<ctime>
#include<cstdlib>
using namespace std;
//grid class

class Grid
{
int **grid;//to store gird cells
int x,y;//grid size

public:
//constructor
Grid()
{
//reading inputs
cout<<"Enter X :";
cin>>x;
cout<<"Enter Y :";
cin>>y;

//creating grid
grid = new int*[x];
for(int i=0;i<x;i++)
{
grid[i] = new int[y];
for(int j=0;j<y;j++)grid[i][j]=0;//initially setting all values to zeros

}

int c= ceil((x*y)/3.0);//finding 1/3 cells

//randomly filling 1/3 values with 1
srand(time(NULL));
int i,j;
while(0<c)
{
i = rand()%x;
if(i<0)i=i*-1;

j = rand()%y;
if(j<0)j=j*-1;


if(grid[i][j]==0){
grid[i][j]=1;
c--;

}


}

}
//parameterized contructor
Grid(int X,int Y)
{
x=X;
y=Y;

//creating grid
grid = new int*[x];
for(int i=0;i<x;i++)
{
grid[i] = new int[y];
for(int j=0;j<y;j++)grid[i][j]=0;//initially setting all values to zeros

}

}
//method to set value of a cell in grid
void setValue(int i,int j,int value)
{
grid[i][j]=value;
}

//method to get value of a cell in grid
int getValue(int i,int j)
{
return grid[i][j];
}

//method to get x value
int getX()
{
return x;
}
//method to get y value
int getY()
{
return y;
}


//method to compare two grids and returns the result
Grid Compare(Grid g2)
{
//creating new gird
Grid *r = new Grid(x,y);

for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
if(grid[i][j]==1 && g2->getValue(i,j)==1)//if both grid values are 1
r->setValue(i,j,1);//calling set value method
}
}

return r;//returning resulting grid

}

//method to display grid values
void Display()
{
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
cout<<grid[i][j]<<" ";
}
cout<<endl;
}

}


};
//testing method
int main()
{
cout<<"Grid 1:\n";
Grid *g1 = new Grid();
cout<<"Grid 2:\n";
Grid *g2 = new Grid();
Grid *g3 = g1->Compare(g2);//comparing g1 with g2

cout<<"Grid 1\n";
g1->Display();

cout<<"Grid 2\n";
g2->Display();

cout<<"Grid 3(Result)\n";
g3->Display();

return 0;
}

Add a comment
Know the answer?
Add Answer to:
Please write code in three files. main.cpp, Grid.h, Grid.cpp. Thank you. Build two grids which are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT