Jump to content

How to calculate ranges using arrays in C#

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Schpanky

Schpanky

    Newbie

  • Members
  • Pip
  • 2 posts
Hello everyone, i'm having a huge problem with a simplistic program.

I'm very new to C# and i have to create a program that "allows a user to enter a trip distance. The output is the ticket price."

Distance ticket price
0-99 25.00
100-299 40.00
300-499 55.00
500 and farther 70.00

i've used arrays before but im not sure about the distance ranges (ex. 0-99, 100-299)

any help is very much appreciated

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Using boolean operators, this could be implemented very simply. (Pseudocode below)


if distance >= 0 AND distance < 100

    ticket price = 25.00

else if distance >= 100 AND distance < 300

    ticket price = 40.00

else if ...

...


Just continue with the pattern above until all ranges are tested. The last condition won't need a boolean AND, because you're only testing for values greater than or equal to 500.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
Schpanky

Schpanky

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks alot, worked out perfectly! :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users