Author Topic: Speaking of programming, I need some help...  (Read 979 times)

0 Members and 1 Guest are viewing this topic.

Offline redsniper

  • 211
  • Aim for the Top!
Speaking of programming, I need some help...
I'm taking this n00b-level programming class where we're all learning the basics of PASCAL and I'm stumped on something.

Let's say I've got a situation where I need things to round up. That is, I can't have fractional values. Like on this one problem calculating the postage for a letter based on it's weight, it cost 30 cents for the first ounce and then 8 cents for every half ounce after that, but if you had 1.1 oz. it would cost 38 cents because you don't need a full half ounce for it to count. I couldn't find an elegant way to get this working on the problem that needed it and I managed a crude workaround instead, but now I'm further along on the assignment, and I need to know the proper way to do this.

Gracias.
"Think about nice things not unhappy things.
The future makes happy, if you make it yourself.
No war; think about happy things."   -WouterSmitssm

Hard Light Productions:
"...this conversation is pointlessly confrontational."

 

Offline Hippo

  • Darth water-horse
  • 211
  • Grazing.
    • All Hands to War
Re: Speaking of programming, I need some help...
i don't know PASCAL, but the c++ pseudocode could be:

Code: [Select]
double amount;
int tmp;
tmp = amount; //here you lose the decimals

if(tmp>amount){amount++;}

amount then becomes the base whole number + 1, or stays the same if there's no decimals


if PASCAL has issues with a double to int conversion like that, there's a slightly longer but also useful way

Code: [Select]
double amount;
for(int i = 0; i < amount; ){i++;}

i is now your rounded up number, but this becomes a much more time consuming calculation on larger numbers (ok, so barely but still, its hardly optimal)
« Last Edit: September 28, 2007, 12:50:33 am by Hippo »
VBB Survivor -- 387 Posts -- July 3 2001 - April 12 2002
VWBB Survivor -- 100 Posts -- July 10 2002 - July 10 2004

AHTW

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
Re: Speaking of programming, I need some help...
PASCAL?! :wtf:
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Speaking of programming, I need some help...
when i took my newb programmer class back in like '97, they started us out on c, then we did c++, then we did vb last for some reason. sence i knew how to code graphics for vb, and i didnt for c/++, i did that for most of my time there. we never had to touch crap like pascal. but im sure if youre instructor was fond of pascal thats what youd end up doing.
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

 

Offline Hippo

  • Darth water-horse
  • 211
  • Grazing.
    • All Hands to War
Re: Speaking of programming, I need some help...
here's another more graceful one

tmp = amount
tmp mod 1
if tmp > 0
   amount++




or there's the useful roundup(); in cmath
VBB Survivor -- 387 Posts -- July 3 2001 - April 12 2002
VWBB Survivor -- 100 Posts -- July 10 2002 - July 10 2004

AHTW

 

Offline Nuke

  • Ka-Boom!
  • 212
  • Mutants Worship Me
Re: Speaking of programming, I need some help...
i usually just use floor() or ceil()
I can no longer sit back and allow communist infiltration, communist indoctrination, communist subversion, and the international communist conspiracy to sap and impurify all of our precious bodily fluids.

Nuke's Scripting SVN

  

Offline redsniper

  • 211
  • Aim for the Top!
Re: Speaking of programming, I need some help...
I ended up doing this:
Code: [Select]
if wt > 1 then
    cost := trunc((wt - 1) * 2); {Finds the number of half ounces after
                                  the first ounce.}

 if cost < wt then     {Rounds up the the next half ounce.}
    cost := cost + 1;

trunc is truncate, which knocks the decimals off the end of any number.
"Think about nice things not unhappy things.
The future makes happy, if you make it yourself.
No war; think about happy things."   -WouterSmitssm

Hard Light Productions:
"...this conversation is pointlessly confrontational."