Question

C++ Create a class box, each box has an int ID, and an east and west...

C++

  1. Create a class box, each box has an int ID, and an east and west side, these are represented as Booleans, all of these are randomly assigned true or false. If true you can pass through that side. These are ONE WAY doors. If false it is a wall. Create a ROW of 8 random boxes. Determine which box has the maximum reachability. Use : to represent a door and a | to represent a wall in your display, example follows:

|0|       :1|        :2:        |3:       |4|       |5:       :6:       |7:

Box 0 you can go no where

Box 1 you can visit 0           ((box 1 has a West door))

Box 2 you can visit 1 and 0 AND 3 and 4

Box 3 you can visit 4

Box 4 you can go no where

Box 5 you can visit 6 and 7

Box 6 you can visit 5 and 7

Box 7 you can go no where

The box with maximum reachability is box 2 where you can reach 0,1,3,4

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

ANSWER:-

Screen shot of program:-

Sample output:

Copy program:-

#include <iostream>
#include <stdlib.h>

using namespace std;
//box class
class Box
{
int Id;
bool east;
bool west;

public:
Box(){};
//set box values
void setBox(int i, bool e, bool w){
this->Id=i;
this->east=e;
this->west=w;
}
//return id
int getID(){
return Id;
}
//return east
bool getEast(){
return east;
}
//return west
bool getWest(){
return west;
}
};

//display the box
void display(Box b[])
{
for(int i=0;i<8;i++)
{
if(b[i].getWest()) //if west door
cout<<":";
else
cout<<"|"; //if no door
cout<<b[i].getID();

if(b[i].getEast()) //if east door
cout<<":";
else
cout<<"|"; //if no east door
}
}

//check the box can visit and return the count
int visit(Box b[],int i)
{
int arr[7],n=0;
int m=i,j;

//if not door present
if(!b[i].getWest() && !b[i].getEast()){
cout<<"\nBox "<<i<<" you can go no where";
return 0;
}

//check the west side doors
while(m>0&& b[m].getWest() )
{
arr[n]=m-1;
n++;
m=m-1;
}
//print the doors
cout<<"\nBox "<<i<<" you can visit ";
for(j=0;j<n;j++)
cout<<arr[j]<<" ";
m=i;

//check the east side doors
while(m<7 && b[m].getEast())
{
arr[n]=m+1;
n++;
m=m+1;
}
if(m>j)
cout<<" AND ";

//print the doors
for(int k=j;k<n;k++)
cout<<arr[k]<<" ";

return n; //return the boo count can visit

}

//print the maximum reachability
void print(Box b[],int id)
{
cout<<"\nThe box with maximum reachability is box "<<id<<" where you can reach ";
int n=visit(b,id);

}
int main()
{
//8 boxes
Box b[8];
int n,m;
bool d1,d2;
int arr[8],max=0,id;

//set the boxes id, doors
for(int i=0;i<8;i++)
{
d1=d2=false;
n=rand();
m=rand();
if(n%2==0)
d1=true;
if(m%2==0)
d2=true;

b[i].setBox(i,d1,d2);


}

display(b); //call display
for(int i=0;i<8;i++)
arr[i]=visit(b,i); //call visit function


//check the count
for(int i=0;i<8;i++)
{
if(max<arr[i]){
max=arr[i];
id=i;
}

}
print(b,id); //print the maximum reachability

cout << endl;
return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Create a class box, each box has an int ID, and an east and west...
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
  • This is the Java Object-oriend project. Please show the detail code and comment for each class....

    This is the Java Object-oriend project. Please show the detail code and comment for each class. Thank you Create a class Door with the following UML specification: +---------------------+ | Door | +---------------------+ | - isOpen: boolean | +---------------------+ | + Door() | | + isOpen(): boolean | | + open(): void | | + close(): void | | + testDoor(): void | +---------------------+ where isOpen is an instance variable indicating whether the door is currently open or not. The default...

  • java problem here is the combination class class Combination {    int first,second,third, fourth;    public...

    java problem here is the combination class class Combination {    int first,second,third, fourth;    public Combination(int first, int second, int third,int fourth)    {        this.first=first;        this.second=second;        this.third=third;        this.fourth=fourth;    }    public boolean equals(Combination other)    {               if ((this.first==other.first) && (this.second==other.second) && (this.third==other.third) && (this.fourth==other.fourth))            return true;        else            return false;    }    public String toString()    {   ...

  • The Lockers Problem There is a well-known mathematical logic problem called the lockers problem. Here is...

    The Lockers Problem There is a well-known mathematical logic problem called the lockers problem. Here is a description of the problem 23 24 25 26 A new high school has just opened! There are 50 lockers in the school and they have been numbered from 1 through 50. When the school opens the first student to enter the school walks into the hallway and opens all the locker doors! Afterwards, the second student closes each door whose number is a...

  • Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet...

    Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet object holds integers in the range 0-100 Represented by an array of booleans, such that array element a[i] is set to true if integer i is in the set, and false otherwise Create these constructors and methods for the class IntegerSet() public IntegerSet union(IntegerSet iSet) public IntegerSet intersection(IntegerSet iSet) public IntegerSet insertElement(int data) public IntegerSet deleteElement(int data) public boolean isEqualTo(IntegerSet iSet) public String toString()...

  • Create a function in JavaScript called "sum" that takes in an array as a parameter and...

    Create a function in JavaScript called "sum" that takes in an array as a parameter and returns the sum of all of the EVEN numbers in a given array. Please note: the array passed in can have any data type, i.e. Strings, Numbers, Booleans, undefined, etc. HINT: the expression typeof will help discern var types. For example if I have: var x = 4, and I do typeof(x) - the output would be "Number" Example run: sum([1, 4, 8, 10,...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • Starting code: #include <stdio.h> #include <stdbool.h> #include <assert.h> bool checkSudoku(int sudoku[9][9]) { //code goes here }...

    Starting code: #include <stdio.h> #include <stdbool.h> #include <assert.h> bool checkSudoku(int sudoku[9][9]) { //code goes here } int main() { int sudoku[9][9] = {{7, 3, 5, 6, 1, 4, 8, 9, 2}, {8, 4, 2, 9, 7, 3, 5, 6, 1}, {9, 6, 1, 2, 8, 5, 3, 7, 4}, {2, 8, 6, 3, 4, 9, 1, 5, 7}, {4, 1, 3, 8, 5, 7, 9, 2, 6}, {5, 7, 9, 1, 2, 6, 4, 3, 8}, {1, 5, 7, 4,...

  • Your goal is to create an ‘Array’ class that is able to hold multiple integer values....

    Your goal is to create an ‘Array’ class that is able to hold multiple integer values. The ‘Array’ class will be given functionality through the use of various overloaded operators You will be given the main() function for your program and must add code in order to achieve the desired result. Do not change any code in main(). If something is not working, you must change your own code, not the code in main(). Assignment 5: overloading member functions. Overview:...

  • Using c++, use a 4x4 maze(for ex this as input: THE NUMBER OF ROOMS DOORS LISTED...

    Using c++, use a 4x4 maze(for ex this as input: THE NUMBER OF ROOMS DOORS LISTED IN ORDER: NORTH, EAST, SOUTH, WEST FOR EACH ROOM: 0 = NO DOORS 0 = 0100 1 = 0111 2 = 0111 3 = 0001 4 = 0100 5 = 1011 6 = 1010 7 = 0010 8 = 0110 9 = 1001 10 = 1100 11 = 1001 12 = 1100 13 = 0101 14 = 0101 15 = 0001 ), Create 3...

  • Mailboxes Java Peter the postman was bored one evening and he decided to experiment with the...

    Mailboxes Java Peter the postman was bored one evening and he decided to experiment with the mailboxes in the mailroom. The mailboxes were numbered from zero to 200 (there were 201 total mailboxes). Initially all boxes were closed. Starting with mailbox #2, he opened the door of every even numbered mailbox. Next, beginning with mailbox #3 and going to every third mailbox, he opened its door if it was closed and closed it if it was open. Then he repeated...

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