c++ class help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Wilhelm4
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Wed Mar 18, 2009 12:54 pm

c++ class help

Post by Wilhelm4 »

Hi, I havent been to these forums in years, but I'm in a programming 1010 class and I cannot figure out one small part on the assignment, I was hoping someone here would see what I'm missing. I have all the functions and code written and working, it is just the math that I cant get right for the following objective:
"The delivery charge for the first pound (i.e., 16 ounces) is $3.00 and $0.50 is added to the charge for each additional 4 ounces. For example, a package weighing more that 16 ounces but at most 20 ounces costs $3.50 to deliver; a package weighing more than 20 but at most 24 ounces costs $4.00 to deliver; etc."

Here is the code i wrote for this:

Code: Select all

double price;
int over16;

over16 = ((oz - 16)/4);
over16 = over16 + 1;

if (oz <= 16)
price = 3.00;
else
price = 3.00 + (over16 /2)


This code works properly for every number except 20, 24, 28, 32....

Is there possibly any way that I can tell over16 that if the answer is an integer when it does the calculations (and its already defined as one), it will not add 1 to itself? That is the only problem, I just have no idea how to achieve this.

Does anyone have any suggestions for how I can fix this? I have been looking at it for a few days and nothing has popped into my brain yet. Thanks in advance for any help.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: c++ class help

Post by avansc »

fuck it.

Code: Select all

#include <math.h>

double calc(int oz)
{
	if(oz > 16)
	{
		 return 3.0 + 0.5 * ceil(((double)oz-16)/4);
	}else
		return 3.0;
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Wilhelm4
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Wed Mar 18, 2009 12:54 pm

Re: c++ class help

Post by Wilhelm4 »

That did the trick, after a little playing around with it i got it to work. Thanks for the help avansc.

1 question:

Code: Select all

int oz;

(double)(oz/2)
Does that make it so the integer oz will display a decimal (if its odd of course)?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: c++ class help

Post by avansc »

Wilhelm4 wrote:That did the trick, after a little playing around with it i got it to work. Thanks for the help avansc.

1 question:

Code: Select all

int oz;

(double)(oz/2)
Does that make it so the integer oz will display a decimal (if its odd of course)?
well no that cast is useless by it self.
lets say oz = 3 then 3/2 is gonna give you 2 seeing as oz is of type int.

Code: Select all

int oz;

(double)oz/2
that is 1.5
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: c++ class help

Post by wtetzner »

Or if you don't want to cast, you could just use 2.0.

Code: Select all

int oz;

oz/2.0
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: c++ class help

Post by avansc »

wtetzner wrote:Or if you don't want to cast, you could just use 2.0.

Code: Select all

int oz;

oz/2.0
i prefer doing it this way to, but our coding standards force us to do casts, especially code that will be maintained by someone else. i guess its just for readability.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply