Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   C++ Program to Determine Cost Per Student for Regional (http://www.chiefdelphi.com/forums/showthread.php?t=71839)

neoshaakti 10-01-2009 19:34

C++ Program to Determine Cost Per Student for Regional
 
I'm writing a C++ program in Dev C++ to determine how much a trip would cost for each member of the team; however, I'm running into trouble since calculate values keep rounding down. If there are 5 people, and 1 room can hold 4 people, the program tells me I need 1 room (which is wrong).

How do I get rounded up values for "girls, boys, advisors"

Quote:

int main(int argc, char* argv[])
{
cout << "Enter number of boys, then Enter Number of Girls" << endl;
int boys ;
int girls;
cin >> boys ;
cin >> girls;
cout << "Number of Advisors needed:" << endl;
int advisors;
advisors = (boys + girls)/10 ;
cout << advisors << endl;

/////////////////////////////////////////////////////////////////////// pretty simple all Im doing here is figuring out how many advisors I need b/c our school has a policy that the ratio of students to teachers has to be 10:1


float room ;
room = ceil(boys/4) + ceil(girls/4) + ceil(advisors/4) ; // want to round each number individually because boys can only room with boys, girls with girls and advisors with advsiors (so they prefer :P)
cout << "Rooms nedeed:" << room << endl ;

kevin.li.rit 10-01-2009 19:37

Re: C++ Program to Determine Cost Per Student for Regional
 
Use % modulus to check for a remainder and add 1 more room if there is a remainder.

Richard McClellan 10-01-2009 19:40

Re: C++ Program to Determine Cost Per Student for Regional
 
import <math.h>
use ceil (advisor)
to round up

neoshaakti 10-01-2009 19:52

Re: C++ Program to Determine Cost Per Student for Regional
 
Quote:

Originally Posted by Richard McClellan (Post 797959)
import <math.h>
use ceil (advisor)
to round up

Hmm I tried all of those things

Quote:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <math.h>
using namespace std;

int main(int argc, char* argv[])
{
cout << "Enter number of boys, then Enter Number of Girls" << endl;
int boys ;
int girls;
cin >> boys ;
cin >> girls;
cout << "Number of Advisors needed:" << endl;
int advisors;
advisors = ceil( (boys + girls)/10 ) ;
cout << advisors << endl;

///////////////////////////////////////////////////////////////////////


float room ;
room = ceil(boys/4) + ceil(girls/4) + ceil(advisors/4) ;
cout << "Rooms nedeed:" << room << endl ;
but for some reason its not rounding up

kevin.li.rit 10-01-2009 20:22

Re: C++ Program to Determine Cost Per Student for Regional
 
I think it's because boys and girls are integers and it is doing integer division. It already drops the decimal before ceil can round it up. Either declare boys and girls as floats or convert them within the ceil function.


All times are GMT -5. The time now is 00:08.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi