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

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

  • The Box Problem Take an 8% x 11 sheet of paper and cut out 4 congruent squares (one from each corner) as shown belo...

    The Box Problem Take an 8% x 11 sheet of paper and cut out 4 congruent squares (one from each corner) as shown below on the left. This creates a net for an open-topped box (rectangular prism) which can be folded up as shown on the right. We're going to use our box to carry as many M & M's as possible. If the side-length of each cut-out square is 1 inch, then the box created will have dimensions 1...

  • Quiz Question 1 (1 point) Saved The maximum value for an int is: Question 1 options:...

    Quiz Question 1 (1 point) Saved The maximum value for an int is: Question 1 options: 2147483647 65535 32767 9223372036854775804 Question 2 (1 point) A float has ____ decimal places of accuracy. Question 2 options: 15 none     7 3 Question 3 (1 point) It is a good practice to compare floats and doubles with ==. Question 3 options: True False Question 4 (1 point) Strings are reference data types. Question 4 options: True False Question 5 (1 point) Value data...

  • In Java: Requirements: 1. Create a class called Bike. 2. Create these private attributes: int currentSpeed...

    In Java: Requirements: 1. Create a class called Bike. 2. Create these private attributes: int currentSpeed int currentGear String paintColor 3. Create a constructor that takes 2 arguments: speed, gear, color. 4. Create another constructor that takes no arguments. 5. Create a method called goFaster(int amount) which increases the speed by the given amount. 6. Create a method called brake() which sets the speed to 0. 7. Create a method called print() which describes the bike and the speed it...

  • python code? 1. Create a class called Person that has 4 attributes, the name, the age,...

    python code? 1. Create a class called Person that has 4 attributes, the name, the age, the weight and the height [5 points] 2. Write 3 methods for this class with the following specifications. a. A constructor for the class which initializes the attributes [2.5 points] b. Displays information about the Person (attributes) [2.5 points] c. Takes a parameter Xage and returns true if the age of the person is older than Xage, false otherwise [5 points] 3. Create another...

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
ADVERTISEMENT