The house is built of non-standard form. All four walls are of a rectangular shape, but different height and
length. The width of the walls is two bricks. There are two types of bricks. The outer part of the wall is
constructed from the first type of brick, and the interior - from the second type. Write a program to count the
number of required bricks of each type.

class brick
{ private: 
    int  length, width, height;// in mm
  public: 
    void set(int len, int wid, int hei);
    int getlength()
    {return length;}
    int getwidth()
    {return width;}
    int getheight()
    {return height;}
};

void brick::set(int len, int wid, int hei)

{   
    length=len;width=wid;height=hei;
}

// calculate no of bricks required for outerwall
int outerwall(brick b, double outerwalllen, double outerwallwid, double outerwallhei)
{
 return outerwalllen * 1000 / b.getlength() *
 outerwallwid * 1000 / b.getwidth() *
 outerwallhei * 1000 / b.getheight();
}

// calculate no of bricks required for innerwall
int innerwall(brick b, double innerwalllen, double innerwallwid, double innerwallhei)
{
 return innerwalllen * 1000 / b.getlength() *
 innerwalllen * 1000 / b.getwidth() *
 innerwalllen * 1000 / b.getheight();
}
#include<iostream>
using namespace std;

    void displaybrick(brick b)
{
 cout << "Brick height: " << b.getheight() << endl;
 cout << "Brick width: " << b.getwidth() << endl;
 cout << "Brick length: " << b.getlength() << endl;
}
int main ()
{
    brick b1;
    b1.set(250,120,88);
    cout<<"brick type1 length is:"<<b1.getlength()<<endl;
    cout<<"brick type1 width is:"<<b1.getwidth()<<endl;
    cout<<"brick type1 height is:"<<b1.getheight()<<endl;
    brick b2;
    b2.set(240, 115, 71);
    cout<<"brick type2 length is:"<<b2.getlength()<<endl;
    cout<<"brick type1 width is:"<<b2.getwidth()<<endl;
    cout<<"brick type1 height is:"<<b2.getheight()<<endl;

cout << "Required I brick type: "
 << 4 * outerwall(b1, outerwalllen, outerwallwid, outerwallhei);
cout << "Required II brick type: "
 << 4 * innerwall(b2, innerwallen, innerwallwid, innerwallhei) << endl;
return(0);
}

I am new in C++ but I made this code with the help of examples i did. I just want to ask that as the dimensions of wall is not given then how to make the code. Do I need to define a variable for this? Please help me as I am a daily and serious learner of C++.At last I just called the function outerwall and innerwall but I do not know what to do with wall dimensions.

In the comments, you state on line 8 that the brick sizes are in millimeters. Because you are multiplying by 1000, it looks like the wall lengths are in metres, but that should be in comments somewhere. I would recommend that if you need to consider other units of measure, perhaps create convertion functions. For example, to use inches with your class, multiply the inch measure by 25.4 to store as millimetres.

@nutster Thax and sorry to mention that walls dimensions are in metre and Bricks are in mm.My question is that values( dimensions) of wall are not given in the problem ...do I need to add cout statement in main() to ask user enter the wall length, width and height.

You need to firstly solve the math before you can code ...

It is often helpful to solve a much simpler problem at first.

If you start out with a retangular solid wall of height hw and width ww the surface area Aw = hw*ww

And if the side surface area of a brick is Ab = hb*wb

Then ... you would need a minimum of Aw / Ab bricks ...
(without allowing for mortar, openings, or losses due to cut bricks to fit height and width and openings exactly.)

Once you have an algorithim to find the minimum integer number of bricks for some real wall, you can then apply that to each of the 4 outer walls ... and then to each of the 4 inner walls ... once you know the exact spec's for the height and width of each outer (or inner wall) ... and the dimensions of the bricks ... and the space to be left between the walls.

Edited 1 Year Ago by David W

No of bricks should be calculated by volume of wall/volume of brick. wall is just a combination of inner wall(made of 2nd type bricjs) and outer wall(1st type bricks) as in problem. We have to find no of each type bricks. so no of 1st type of bricks = outer wall volume/volume of 1st type brick.........similarily for 2nd type....

It is not really the vol of the totalLength x totalHeight x totalWidth of a wall the determines the (min) integer number of bricks needed to brick up a real wall that has mortar between the bricks and openings for doors and windows and door and window frames and cut bricks to make them all fit neatly !!! (And do not forget what happens at the corners ... and that each next layer of bricks is symmetrically staggered with respect to the layer below.)

Edited 1 Year Ago by David W

You might start out with a loop ... something like this:

// bricks.cpp //  // 2015/07/22 //

// this version approximates count of bricks needed for a wall ... //

#include <iostream>
#include <string>
#include <cmath> // re. ceil

// ok to do this, for now
using namespace std;

const string INTRO =
    "This program asks for input in a loop ... \n";

const double MORTAR = 3.0/8 ; // in inches //

const double LOSSAGE = 0.05 ; // adjust here to what fits best //


template< typename T >
T takeIn( const std::string& msg,
          const std::string& errMsg = "Error! Try again ...\n" )
{
    T val = T();
    while( true )
    {
        std::cout << msg << std::flush;
        if( std::cin >> val && std::cin.get() == '\n' )
            break;
        else
        {
            std::cout << errMsg;
            std::cin.clear(); // clear error flasgs
            std::cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}

struct Box
{
    double length, height, width;

    // default ctor ...
    Box() : length(8), height(3), width(4) {} // default brick size in inches //

    void takeInSides()
    {
        length = takeIn< double > ( "Enter length in inches: " );
        height = takeIn< double > ( "Enter height in inches: " );
    }
    double sideArea() const { return length*height; }
    double sideAreaAugmented() const { return (length+MORTAR)*(height+MORTAR); }

    friend ostream& operator << ( ostream& os, const Box& bx )
    {
        return os << bx.length << " x " << bx.width << " x " << bx.height;
    }
} ;

bool more( const string& msg = "More (y/n) ? " );




int main() ////////////////// BEGIN MAIN ///////////////////
{
    cout << INTRO << endl;
    do
    {
        Box brick;
        cout << "For bricks ...\n";
        brick.takeInSides();

        Box wall;
        cout << "For walls ...\n";
        wall.takeInSides();

        cout << "For all openings in wall ...\n";
        int count = 0;
        double openArea = 0;
        do
        {
            Box space;
            cout << "For space " << ++count << " ...\n";
            space.takeInSides();
            openArea += space.sideAreaAugmented();
        }
        while( more( "More spaces (y/n)? " ) );

        double firstEstimate = (wall.sideArea() - openArea)
                               / brick.sideAreaAugmented();

        cout << "1st estimate of bricks needed is: "
             << firstEstimate << "\n";

        // round up //
        cout << "Integer number of " << brick << " bricks to buy (allowing 5% loss): "
             << int( ceil(firstEstimate*(1+LOSSAGE) )) << "\n\n";
    }
    while( more() );

} ////////////////////////// END MAIN //////////////////////


bool more( const string& msg )
{
    cout << msg << flush;
    string line;
    getline( cin, line );
    if( line.size() && tolower(line[0]) ==  'n' ) return false;
    // else // defaults to yes, more
    return true;
}

Edited 1 Year Ago by David W

Comments
Thax but I am required to do with only class and objects...also no mortar and filling is required..no need to corner filling as well...Simply rect wal

In C++ ... a 'struct' is an object.

The difference is that a C++ 'class' defaults to 'private' ...

but a C++ 'struct' defaults to 'public' ...

const double MORTAR = 3.0/8 ; // in inches //
const double LOSSAGE = 0.05 ; // adjust here to what fits best //

template< typename T >
T takeIn( const std::string& msg,
          const std::string& errMsg = "Error! Try again ...\n" );

class Box
{
public:
    // default ctor ...
    Box() : length(8), height(3), width(4) {} // default brick size in inches //

    void takeInSides()
    {
        length = takeIn< double > ( "Enter length in inches: " );
        height = takeIn< double > ( "Enter height in inches: " );
    }
    double sideArea() const { return length*height; }
    double sideAreaAugmented() const { return (length+MORTAR)*(height+MORTAR); }

private:
    double length, height, width;

    friend ostream& operator << ( ostream& os, const Box& bx )
    {
        return os << bx.length << " x " << bx.width << " x " << bx.height;
    }
} ;

Edited 1 Year Ago by David W

Actually ... the following is a lttle more accurate (and also less code) ... It (also) makes (some) allowance, re. the count of bricks, for the 'walls MORTAR' at the 'finishing edges' ...

// bricks2.cpp //  // 2015/07/23 //

// find approximate count of bricks needed for a wall //

#include <iostream>
#include <string>
#include <cmath> // re. ceil

// ok to do this, for now
using namespace std;

const string INTRO =
    "This program asks for input in a loop ... \n";

const double MORTAR = 3.0/8 ; // in inches //

const double LOSSAGE = 0.05 ; // adjust here to what fits best //


template< typename T >
T takeIn( const std::string& msg,
          const std::string& errMsg = "Error! Try again ...\n" ) ;


class Box
{
public:
    // default ctor ...
    Box() : length(8), height(3), width(4) {} // default brick size in inches //

    void takeInSides()
    {
        length = takeIn< double > ( "Enter length in inches: " );
        height = takeIn< double > ( "Enter height in inches: " );
    }
    double sideArea() const { return (length+MORTAR)*(height+MORTAR); }

private:
    double length, height, width;

    friend ostream& operator << ( ostream& os, const Box& bx )
    {
        return os << bx.length << " x " << bx.width << " x " << bx.height;
    }
} ;


bool more( const string& msg = "More (y/n) ? " );



int main() ////////////////// BEGIN MAIN ///////////////////
{
    cout << INTRO << endl;
    do
    {
        Box brick;
        cout << "For bricks ...\n";
        brick.takeInSides();

        Box wall;
        cout << "For walls ...\n";
        wall.takeInSides();

        cout << "For all openings in wall ...\n";
        int count = 0;
        double openArea = 0;
        do
        {
            Box space;
            cout << "For space " << ++count << " ...\n";
            space.takeInSides();
            openArea += space.sideArea();
        }
        while( more( "More spaces (y/n)? " ) );

        double firstEstimate = (wall.sideArea() - openArea)
                               / brick.sideArea();

        cout << "1st estimate of bricks needed is: "
             << firstEstimate << "\n";

        // round up //
        cout << "Integer number of " << brick
             << " bricks to buy (allowing "
             << 100*LOSSAGE << "% loss) is: "
             << int( ceil(firstEstimate*(1+LOSSAGE) )) << "\n\n";
    }
    while( more() );

} ////////////////////////// END MAIN //////////////////////



bool more( const string& msg )
{
    cout << msg << flush;
    string line;
    getline( cin, line );
    if( line.size() && tolower(line[0]) ==  'n' ) return false;
    // else // defaults to yes, more
    return true;
}

template< typename T >
T takeIn( const std::string& msg, const std::string& errMsg )
{
    T val = T();
    while( true )
    {
        std::cout << msg << std::flush;
        if( std::cin >> val && std::cin.get() == '\n' )
            break;
        else
        {
            std::cout << errMsg;
            std::cin.clear(); // clear error flasgs
            std::cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}

Edited 1 Year Ago by David W

This article has been dead for over six months. Start a new discussion instead.