Question

C++ code: **Please show me a way to do this without the use of pointers** Build...

C++ code: **Please show me a way to do this without the use of pointers**

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/3 of the grid’s cells with a ‘1’. You must randomly fill each of the two grids separately so they do not have the all of the same squared filled with a ‘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.

Example in a 2 by 2 grid:

0

0

1

1

Grid 1

1

0

1

0

Grid 2                         

0

0

1

0

Grid 3 (Result)

For this assignment, you will need to create a Grid Class and the appropriate member functions and member variables to perform the above operations. Also, please demonstrate the usage of the Grid class and its methods from main function.

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

Code Screenshots:

Sample Output:

Code To Copy:

//Include the required

//header files.

#include <iostream>

#include <cstdlib>

#include <cmath>

//Use the standard

//namespace.

using namespace std;

//Define the maximum

//size of the grid.

#define MAX_GRID_SIZE 100

//Define the

//class Grid.

class Grid

{

    private:

    //Declare an array to

    //store the grid values.

    int grid[MAX_GRID_SIZE][MAX_GRID_SIZE];

    //Declare the variables

    //to store the values

    //of X and Y.

    int x;

    int y;

    public:

    //Define the

    //parametrized constructor.

    Grid(int x_val, int y_val)

    {

        //Set the values

        //of x and y.

        x = x_val;

        y = y_val;

       

        //Fill the grid

        //with zeroes.

        fill_zeroes();

    }

    //Define the function get_val().

    int get_val(int x_val, int y_val)

    {

        //Return the value present

        //at the (x_val,y_val) co-ordinates.

        if(x_val < x && y_val < y)

        {

            return grid[x_val][y_val];

        }

        

        //If the reieved co-ordinates

        //are outside the grid then,

        //return -1.

        return -1;

    }

    //Define the function

    //set_val().

    void set_val(int x_val, int y_val, int val)

    {

        //If the recived co-ordinates

        //are valid then, set the value.

        if(x_val < x && y_val < y)

        {

            grid[x_val][y_val] = val;

        }

    }

    //Define the function

    //fill_zeroes().

    void fill_zeroes()

    {

        //Run the loop to fill

        //the grid with zeroes.

        for(int i=0; i<x; i++)

        {

            for(int j=0; j<y; j++)

            {

                grid[i][j] = 0;

            }

        }

    }

    //Define the

    //function display_grid().

    void display_grid()

    {

        cout << endl;

        for(int i=0; i<x; i++)

        {

            for(int j=0; j<y; j++)

            {

                cout << grid[i][j] << " ";

            }

            cout << endl;

        }

        cout << endl;

    }

};

//Define the

//main() function.

int main() {

    //Set the seed value

    //of the random function.

    srand(time(NULL));

    //Declare the

    //required variables.

    int x;

    int y;

    int total_values_by_3;

    //Prompt the user to

    //enter the value of 'X'.

    cout << "Enter the value of X: ";

   

    //Read and store

    //the value of 'X'.

    cin >> x;

    //Prompt the user to

    //enter the value of 'Y'.

    cout << "Enter the value of Y: ";

    //Read and store

    //the value of 'Y'.

    cin >> y;

    //Calculate the 1/3

    //of the total values.

    total_values_by_3 = ceil((x*y)/3.0);

    //Define the objects

    //of the grid class.

    Grid grid1(x, y);

    Grid grid2(x, y);

    Grid grid3(x, y);

    //Run the loop to fill

    //1/3 of total values

    //of grid 1 with 1.

    for(int i=0; i<total_values_by_3; i++)

    {

        int rand_x = rand()%x;

        int rand_y = rand()%y;

        grid1.set_val(rand_x, rand_y, 1);

    }

    //Run the loop to fill

    //1/3 of total values

    //of grid 2 with 1.

    for(int i=0; i<total_values_by_3; i++)

    {

        int rand_x = rand()%x;

        int rand_y = rand()%y;

        grid2.set_val(rand_x, rand_y, 1);

    }

    //Run the loop to check the

    //grid values of the grid 1

    //and the values of grid 2.

    for(int i=0; i<x; i++)

    {

        for(int j=0; j<y; j++)

        {

            int grid1_val = grid1.get_val(i, j);

            int grid2_val = grid2.get_val(i, j);

            //If both the grids have

            //the value 1 at the

            //current square then, set

            //the value of the respective

            //square of grid as 1.

            if(grid1_val == 1 && grid2_val == 1)

            {

                grid3.set_val(i, j, 1);

            }

        }

    }

    //Display the grid 1.

    cout << "Grid 1:"<<endl;

    grid1.display_grid();

    //Display the grid 2.

    cout << "Grid 2:"<<endl;

    grid2.display_grid();

    //Display the grid 3.

    cout << "Grid 3:"<<endl;

    grid3.display_grid();

    //Return from the

    //main() function.

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
C++ code: **Please show me a way to do this without the use of pointers** Build...
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
  • *Using C++* You will create a program that uses a Critter class to move around a...

    *Using C++* You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • with C++ You will create a program that uses a Critter class to move around a...

    with C++ You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • Need help building this C++ program For your program you will create a class that represents...

    Need help building this C++ program For your program you will create a class that represents square shapes. I provide you an incomplete client program that will test your class and an incomplete header file where you have to declare and implement the class. In the client program you have to complete certain statements according to the indications provided in the corresponding comments. In the header file you have to create class square according to the following specifications: Member functions...

  • Please use C++ and Please do all file separate separately. And also I need output too....

    Please use C++ and Please do all file separate separately. And also I need output too. thanks. Please do it complete work. ShapeNodePoly.cpp Avaliable from: Wednesday, July 27, 2016, 10:20 AM Requested files: Point.h, Point.cpp, Shape.h, Shape.cpp, Polygon.h, Polygon.cpp, Ellipse.h, Ellipse.cpp, ShapeNodePoly.cpp, ShapeNodePoly_test.cpp (Download) Type of work: Individual work In this assignment, you will create a class that can behave as any of the shapes listed below. You will use object inheritance to enable all shapes to be managed using...

  • Please i need the .H, .cpp and main.cpp files. Please use vector. its for my midterm so correct code that can run only....

    Please i need the .H, .cpp and main.cpp files. Please use vector. its for my midterm so correct code that can run only. Thank you. Pointers and Dynamic Memory – Chapter 11 1. Write code using pointers and the Accounts class from week 1 assignment as follows – create a program in which you a. Create 10 accounts using an array with id’s 0 to 9 and refer to each account using pointer notation b. Add an initial balance of...

  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • Please help me code the following in: JAVA Please create the code in as basic way...

    Please help me code the following in: JAVA Please create the code in as basic way as possible, so I can understand it better :) Full points will be awarded, thanks in advance! Program Description Write a program that demonstrates the skills we've learned throughout this quarter. This type of project offers only a few guidelines, allowing you to invest as much time and polish as you want, as long as you meet the program requirements described below. You can...

  • Please do not use SYMS package. It does not work on Octave for me. Matlab code...

    Please do not use SYMS package. It does not work on Octave for me. Matlab code needed for: 1. Apply the Explicit Trapezoid Method on a grid of step size h = 0.1 in [0, 1] to the initial value problems in Exercise 1. Print a table of the t values, approximations, and global truncation error at each step. IVP (Exercise 1): (a) y'=1 (b) y' = 12y (c) y' = 2(t+1)y (d) y = 564, (e) y'=1/y² (1) y'=1/y2...

  • please show step by step work. 4. Scatter plots and calculating correlation Suppose you are given...

    please show step by step work. 4. Scatter plots and calculating correlation Suppose you are given the following five pairs of scores: X Y 0 10 Create a scatter diagram of these scores in the following diagram. For each of the five Cx. v) pain, elik on the plotting embel (the black xi.e upper right corner of the tool, and drag it to the approcriate location on the grid Based on your scatter diagram, you would expect the correlation to...

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