math question, determine how many floor tiles to use

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 12969 Views - Last Post: 20 March 2010 - 09:09 AM Rate Topic: -----

#1 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

math question, determine how many floor tiles to use

Posted 30 January 2010 - 12:38 AM

I'm writing a program for C++ class and I need to figure out the area of a room in square inches, and how many floor tiles you would need for it.

The calculation works out correctly when there is an even number of feet for the room, but when I input both feet and inches, I don't know if it is calculating correctly.

Anyway my question is about the math. Particularly the number of tiles required. Did I do this right?

Also, is there a better way of rounding up the double to the nearest int? I want to round it up even if we need 9.1 boxes, since we would then need 10 boxes.

#include<iostream>
#include<conio.h> //for getch()
using namespace std;

//function prototypes
int getInfo(); 
int convertToInches(int, int);
int getArea(int, int);
double getTiles(int, int);

//functions
int getInfo(){
	int x;
	cin>>x;
	return x;
}

int convertIn(int ft, int in){
	int totalIn;
	ft = ft * 12;
	totalIn = ft + in;
	return totalIn;
}

int getArea(int tw, int tl){
	int area = tw * tl;
	return area;
}

double getTiles(int tile, int area){
	double need = area/(tile*tile);
	return need;
}


int main(){
	int rooms=0, tile=0, widthFt=0, widthIn=0,lengthFt=0, lengthIn=0;
	int totWidth=0, totLength=0, area =0, extra =0;
	double tilesNeeded=0, boxes =0;
	const int PERBOX = 20;

	cout<<"Enter number of rooms:";
	rooms = getInfo();

	cout<<"Enter size of tile in inches:";
	tile = getInfo();

	for(int i=0; i<rooms; i++){
		cout<<"Enter room width (feet and inches, separated by a space):";
		cin>>widthFt>>widthIn;

		cout<<"Enter room length (feet and inches, separated by a space):";
		cin>>lengthFt>>lengthIn;
	
		totWidth = convertIn(widthFt, widthIn);
		totLength = convertIn(lengthFt, lengthIn);
		area = getArea(totWidth, totLength);

	tilesNeeded = getTiles(tile, area);
	//test
	cout<<"\nTotal tiles needed: "<<tilesNeeded<<endl;

	boxes = tilesNeeded/PERBOX;

	boxes +=.9; //to round up in case there is a decimal & we need one more box

	// change number of boxes to int so it's an even number				   
	boxes = static_cast<int>(boxes);

	cout<<"\nNumber of boxes needed: "<<boxes<<endl;

	extra = (PERBOX*boxes) - tilesNeeded;
	
	//test
	cout<<"\nLeftover tiles: "<<extra<<endl;

	}
		   
getch();
return 0;
}


When I input the sample info from the book, it doesn't come out the same as theirs. I was hoping they had it wrong. lol

sample:
rooms: 2
tile size: 12
room width (ft, in): 17 4
room length (ft, in): 9 3
Room requires 180 tiles

room width (ft, in): 11 6
room length (ft, in): 11 9
Room requires 144 tiles

total tiles required: 324
boxes needed: 17
leftover tiles: 16

I cannot get it to compute the same number of tiles required as they have. I'm thinking it has to do with using partial tiles...or my math is just off.

Is This A Good Question/Topic? 0
  • +

Replies To: math question, determine how many floor tiles to use

#2 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: math question, determine how many floor tiles to use

Posted 30 January 2010 - 02:24 AM

For rounding up the value, use double ceil ( double x ); function defined in <cmath> header. It nicely rounds up the value. So if you give it 4.2, it gives you 5.0.

Edit: I added the total counts, now the code looks like this:
#include<iostream>
//#include<conio.h> //my compiler doesn't support this
#include <cmath>
using namespace std;

//function prototypes
int getInfo();
int convertToInches(int, int);
int getTiles(int, int, int);

//functions
int getInfo(){
	int x;
	cin>>x;
	return x;
}

int convertIn(int ft, int in){
	return ft*12 + in;
}

int getTiles(int tile, int w, int h){
	return ceil( double(w) / tile ) * ceil( double(h) / tile );
}


int main(){
	int rooms=0, tile=0, widthFt=0, widthIn=0,lengthFt=0, lengthIn=0;
	int totWidth=0, totLength=0, area =0, extra =0;
	int tilesNeeded=0, boxes=0, totalTiles=0, totalBoxes=0, totalLeft = 0;
	const int PERBOX = 20;

	cout<<"Enter number of rooms:";
	rooms = getInfo();

	cout<<"Enter size of tile in inches:";
	tile = getInfo();

	for(int i=0; i<rooms; i++){
		cout<<"Enter room width (feet and inches, separated by a space):";
		cin>>widthFt>>widthIn;

		cout<<"Enter room length (feet and inches, separated by a space):";
		cin>>lengthFt>>lengthIn;

		totWidth = convertIn(widthFt, widthIn);
		totLength = convertIn(lengthFt, lengthIn);
		//area = getArea(totWidth, totLength); Not needed

		tilesNeeded = getTiles(tile, totWidth, totLength);
		totalTiles += tilesNeeded;
		//test
		cout<<"\nTotal tiles needed: "<<tilesNeeded<<endl;

		boxes = ceil( (double)tilesNeeded/PERBOX ); //rounds up the value, so for example 3.4 will be 4
		totalBoxes += boxes;

		//boxes +=.9; //to round up in case there is a decimal & we need one more box

		// change number of boxes to int so it's an even number
		//boxes = static_cast<int>(boxes);

		cout<<"\nNumber of boxes needed: "<<boxes<<endl;

		extra = (PERBOX*boxes) - tilesNeeded;
		totalLeft += extra;

	//test
		cout<<endl<<"Leftover tiles: "<<extra<<endl;

	}
	cout<<endl<<"Total Number of Tiles: "<<totalTiles;
	cout<<endl<<"Total Number of Boxes: "<<totalBoxes;
	cout<<endl<<"Total Leftovers: "<<totalLeft<<endl;
//I had to remove getch as my compiler doesn't have conio.h
cin.get();
return 0;
}

This post has been edited by Anarion: 30 January 2010 - 08:08 AM

Was This Post Helpful? 1
  • +
  • -

#3 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: math question, determine how many floor tiles to use

Posted 30 January 2010 - 03:57 AM

You can't divide the total area by the area of a tile ...

1. count the number of tiles along the length ... round up (using ceil)
2. count the number of tiles along the width ... round up ...

then 1 x 2 gives the number of squares of tiles you need
Was This Post Helpful? 1
  • +
  • -

#4 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: math question, determine how many floor tiles to use

Posted 30 January 2010 - 04:00 AM

Edit:
Look at the next post

This post has been edited by Anarion: 30 January 2010 - 04:46 AM

Was This Post Helpful? 0
  • +
  • -

#5 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: math question, determine how many floor tiles to use

Posted 30 January 2010 - 04:05 AM

Try something like this 'oo' ...

#include <iostream>
#include <cmath>

using namespace std;

//function prototypes
int getInfo( char[] );
int convertToInches(int, int);
double getTiles(int, int, int);

//functions
int getInfo( char prompt[] )
{
    int x;
    for( ; ; ) // forever loop ... until return a good int
    {
        cout << prompt << flush;
        cin >> x;
        if( cin.good() )
        {
            cin.sync();
            return x;
        }
        // else ...
        cout << "Integers only please ...\n";
    }
}

int convertIn(int ft, int in)
{
    return ft*12 + in;
}

double getTiles(int tile, int width, int length)
{
    return ceil( double(width) / tile ) * ceil( double(length) / tile );
}


int main()
{
    const int PERBOX = 20;
    
    int rooms = getInfo("Enter number of rooms: ");
    int tile = getInfo("Enter size of tile in inches: ");
    
    for(int i=0; i<rooms; i++)
    {
        cout << "Enter room width (feet and inches) ...\n";
        int widthFt = getInfo("feet: ");
        int widthIn = getInfo("inches: ");
        int totWidth = convertIn(widthFt, widthIn);
        // test ...
        cout << "width: " << totWidth << " inches." << endl;
        
        cout << "Enter room length (feet and inches) ...\n";
        int lengthFt = getInfo("feet: ");
        int lengthIn = getInfo("inches: ");
        int totLength = convertIn(lengthFt, lengthIn);
        // test ...
        cout << "length: " << totLength << " inches." << endl;
        
        double tilesNeeded = getTiles(tile, totWidth, totLength);
        //test ...
        cout << "\nTotal tiles needed: " << tilesNeeded <<endl;
        
        double boxes = ceil( double(tilesNeeded)/PERBOX );
        cout << "\nNumber of boxes needed: " << boxes << endl;

        double extra = PERBOX*boxes - tilesNeeded;

        //test ...
        cout<<"\nLeftover tiles: "<<extra << endl;
    }

    cin.get();
}


Was This Post Helpful? 1
  • +
  • -

#6 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: math question, determine how many floor tiles to use

Posted 30 January 2010 - 04:13 AM

Oh sorry I was mixing it with some other code. Now it works :D

I was mistakenly writing this:
int getTiles(int tile, int w, int h){
	int need = ceil( (double)w/(tile) * (double)h/(tile) );
	return need;
}

That was giving me the same wrong numbers. They have to be rounded up separately :)

Edited the code in my post to be correct

This post has been edited by Anarion: 30 January 2010 - 04:47 AM

Was This Post Helpful? 0
  • +
  • -

#7 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: math question, determine how many floor tiles to use

Posted 30 January 2010 - 09:46 AM

They have to be rounded up separately...I see. That makes sense.

Thanks guys for the help. The program seems to be working correctly now. :)
Was This Post Helpful? 0
  • +
  • -

#8 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: math question, determine how many floor tiles to use

Posted 30 January 2010 - 05:37 PM

View PostOliveOyl3471, on 30 Jan, 2010 - 08:46 AM, said:

They have to be rounded up separately...I see. That makes sense.

Thanks guys for the help. The program seems to be working correctly now. :)


Shalom 'oo',

Oh how we need fresh '00' ... in our lamps ... every-day .. these days ...
And why? Please see ...
http://sites.google....eyeshallseehim/

Thank you for your 'thanks' ... but I left off 2 lines in my haste that make the 'int' input validation function ... work properly ... the way I wanted it to ... so as to NOT crash out of a running program if a non-int was entered ...

//functions
int getInfo( char prompt[] )
{
    int x;
    for( ; ; ) // forever loop ... until return a good 'int'
    {
        cout << prompt << flush;
        cin >> x;
        if( cin.good() )
        {
            cin.sync();
            return x; // ok ... we have an 'int' ... so return it ...
        }
        // else ...
        cout << "Integers only please ...\n";
        cin.clear(); // clear error flags if 'non-int' was entered, so don't crash
        cin.sync(); // 'flush' cin stream ... to ensure waiting for next cin
    }
}

Was This Post Helpful? 0
  • +
  • -

#9 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: math question, determine how many floor tiles to use

Posted 30 January 2010 - 06:31 PM

Here is a revision that includes the above ...

#include <iostream>
#include <cmath>

using namespace std;

// globals

const int NUM_TILES_PER_BOX = 20;
const char PROMPT_TEXT[] = "please enter feet first, then inches ...\n";

//function prototypes
int takeInIntNum( char[] );
int convertFeetToInches(int, int);
double calculateNumTiles(int, int, int);

//functions
int takeInIntNum( char prompt[] )
{
    int x;
    for( ; ; ) // forever loop ... until return a good 'int'
    {
        cout << prompt << flush;
        cin >> x;
        if( cin.good() && x > 0 )
        {
            cin.sync();
            return x; // ok ... we have an 'int' > 0 ... so return it ...
        }
        // else ...
        cout << "Valid input is integer greater than 0 ...\n";
        cin.clear(); // clear error flags if 'non-int' was entered, so dont crash
        cin.sync(); // 'flush' cin stream ... to ensure waiting for next cin
    }
}

int convertFeetToInches(int ft, int in)
{
    return ft*12 + in;
}

double calculateNumTiles(int numTiles, int width, int length)
{
    return ceil( double(width) / numTiles ) * ceil( double(length) / numTiles );
}



int main()
{
    int numRooms = takeInIntNum("Enter number of rooms: ");
    int numTiles = takeInIntNum("Enter size of tiles in inches: ");
    
    for(int i=0; i<numRooms; i++)
    {
        cout << "For room " << i+1 << "'s width, " << PROMPT_TEXT;
        int widthFt = takeInIntNum("feet: ");
        int widthIn = takeInIntNum("inches: ");
        int totWidth = convertFeetToInches(widthFt, widthIn);
        // test ...
        cout << "Room width is " << totWidth << " inches." << endl;
        
        cout << "For room " << i+1 << "'s length, " << PROMPT_TEXT;
        int lengthFt = takeInIntNum("feet: ");
        int lengthIn = takeInIntNum("inches: ");
        int totLength = convertFeetToInches(lengthFt, lengthIn);
        // test ...
        cout << "Room length is " << totLength << " inches." << endl;
        
        double numTilesNeeded = calculateNumTiles(numTiles, totWidth, totLength);
        //test ...
        cout << "\nTotal number of tiles needed is " << numTilesNeeded <<endl;
        
        double boxes = ceil( double(numTilesNeeded)/NUM_TILES_PER_BOX );
        cout << "Number of boxes needed, with " << NUM_TILES_PER_BOX
             << " tiles each, is " << boxes << endl;

        double extra = NUM_TILES_PER_BOX * boxes - numTilesNeeded;

        //test ...
        cout << "Number of tiles 'left-over' is " << extra << endl << endl;
    }

    cout << "Press 'Enter' to continue ... " << flush;
    cin.get();
}


This post has been edited by David W: 30 January 2010 - 07:11 PM

Was This Post Helpful? 0
  • +
  • -

#10 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: math question, determine how many floor tiles to use

Posted 30 January 2010 - 06:42 PM

I think a casting function shouuld work
int round_up(double &input)
{
	if(input>(int)input)
		return (int)input+1;
	else
		return (int)input;
}



Was This Post Helpful? 0
  • +
  • -

#11 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: math question, determine how many floor tiles to use

Posted 30 January 2010 - 07:09 PM

View PostImaSexy, on 30 Jan, 2010 - 05:42 PM, said:

I think a casting function shouuld work
int round_up(double &input)
{
	if(input>(int)input)
		return (int)input+1;
	else
		return (int)input;
}




Nice work ... and great if you can't use a double that ceil( ... ) returns ... or if you don't like casting the returned value of ceil to an 'int'
Was This Post Helpful? 0
  • +
  • -

#12 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: math question, determine how many floor tiles to use

Posted 30 January 2010 - 07:32 PM

With a little nudge ... :D

here is a 'static_cast' you may like ...

#include <iostream>
#include <cmath>

using namespace std;

// globals

const int NUM_TILES_PER_BOX = 20;
const char PROMPT_TEXT[] = "please enter feet first, then inches ...\n";

//function prototypes
int takeInIntNum( char[] );
int convertFeetToInches(int, int);
int calculateNumTiles(int, int, int);

//functions
int takeInIntNum( char prompt[] )
{
    int x;
    for( ; ; ) // forever loop ... until return a good 'int'
    {
        cout << prompt << flush;
        cin >> x;
        if( cin.good() && x > 0 )
        {
            cin.sync();
            return x; // ok ... we have an 'int' > 0 ... so return it ...
        }
        // else ...
        cout << "Valid input is integer greater than 0 ...\n";
        cin.clear(); // clear error flags if 'non-int' was entered, so dont crash
        cin.sync(); // 'flush' cin stream ... to ensure waiting for next cin
    }
}

int convertFeetToInches(int ft, int in)
{
    return ft*12 + in;
}

int calculateNumTiles(int numTiles, int width, int length)
{
    return static_cast<int> (ceil( double(width) / numTiles ) * ceil( double(length) / numTiles ));
}



int main()
{
    int numRooms = takeInIntNum("Enter number of rooms: ");
    int numTiles = takeInIntNum("Enter size of tiles in inches: ");
    
    for(int i=0; i<numRooms; i++)
    {
        cout << "For room " << i+1 << "'s width, " << PROMPT_TEXT;
        int widthFt = takeInIntNum("feet: ");
        int widthIn = takeInIntNum("inches: ");
        int totWidth = convertFeetToInches(widthFt, widthIn);
        // test ...
        cout << "Room width is " << totWidth << " inches." << endl;
        
        cout << "For room " << i+1 << "'s length, " << PROMPT_TEXT;
        int lengthFt = takeInIntNum("feet: ");
        int lengthIn = takeInIntNum("inches: ");
        int totLength = convertFeetToInches(lengthFt, lengthIn);
        // test ...
        cout << "Room length is " << totLength << " inches." << endl;
        
        int numTilesNeeded = calculateNumTiles(numTiles, totWidth, totLength);
        //test ...
        cout << "\nTotal number of tiles needed is " << numTilesNeeded <<endl;
        
        int boxes = static_cast<int> (ceil( double(numTilesNeeded)/NUM_TILES_PER_BOX ));
        cout << "Number of boxes needed, with " << NUM_TILES_PER_BOX
             << " tiles each, is " << boxes << endl;

        int extra = NUM_TILES_PER_BOX * boxes - numTilesNeeded;

        //test ...
        cout << "Number of tiles 'left-over' is " << extra << endl << endl;
    }

    cout << "Press 'Enter' to continue ... " << flush;
    cin.get();
}


Was This Post Helpful? 0
  • +
  • -

#13 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: math question, determine how many floor tiles to use

Posted 31 January 2010 - 02:00 AM

ceil supports floats too :D not just double.
See Here
Was This Post Helpful? 0
  • +
  • -

#14 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: math question, determine how many floor tiles to use

Posted 31 January 2010 - 02:11 AM

View PostAnarion, on 31 Jan, 2010 - 01:00 AM, said:

ceil supports floats too :D not just double.
See Here


That's good to know ... B) and also long doubles ... :D

double ceil ( double x );
float ceil ( float x );
long double ceil ( long double x );

So ... now our dear 'oo' (olive oil ) is 'oiled' ... and all set to go, go, go ...
Was This Post Helpful? 0
  • +
  • -

#15 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: math question, determine how many floor tiles to use

Posted 31 January 2010 - 02:26 AM

I didn't realize what you meant by oo at first, thought Object Oriented :lol:

This post has been edited by Anarion: 31 January 2010 - 02:27 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2