I need to compare a String array index to a double array's index to get the price of an item.
Each value corresponds to the other as item-to-price. What I'm trying to do check if the value of say variable choice is equal to one of the values from the String array, then check that value's index against the double array's indices for a match, afterwards it should return the value of that index. Any idea as to how a guy would go about doing that?
If I understand correctly, you want the user to enter a food like "lemonade", and you then want to determine the price of it. A simple loop could be used to find the index of "lemonade". If you are allowed to use the STL, a map would be easier.
wait...if the array "choices" holds items, and the array prices holds the price of the items in the other array, that is a BAD design. it is difficult to implement, and impossible to maintain. things will very easily get out of sync.
What you should be doing is defining your own class that has both a price and a description.
Also, don't ever store money as a floating point type. You will start getting rounding errors. Money should always be stored as atomic units - in the U.S., that would be in pennies. So store $1.99 as 199, and convert as needed.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:Also, don't ever store money as a floating point type. You will start getting rounding errors. Money should always be stored as atomic units - in the U.S., that would be in pennies. So store $1.99 as 199, and convert as needed.
Certainly money shouldn't be stored as a FP type in most real apps, but as an educational exercise I think it's acceptable, as long as the pitfalls are mentioned.
As a proper representation, as an alternative to atomic units, isn't BigDecimal also acceptable?
Keith Lynn wrote:If I understand correctly, you want the user to enter a food like "lemonade", and you then want to determine the price of it. A simple loop could be used to find the index of "lemonade".
This is precisely what I'm trying to do, I just don't know how to make the loop... but I'll surf the net until I find an example to learn off of - thanks! Looping should have been the obvious answer... lol.
Aj Prieto wrote:Also if you look in the example you posted, you're missing index 3 for your prices array.
Thanks for telling me, that probably would've caused some headaches. ^^
Okay, I found a pretty straight-forward way:It seems to work for now, we'll see what happens I guess.
Works perfectly, thanks guys!
Levi Neuxell wrote: . . . Okay, I found a pretty straight-forward way: . . .
Works perfectly, thanks guys!
It is expensive on memory, creating a new List.
It is very error‑prone, because a change in the List or the prices array will give incorrect prices for many items.
It does not constitute object‑oriented programming; you were suggested an object‑oriented solution and ignored it.
It is slow; you are using an algorithm which runs in linear time. If you had a sorted array of objects encapsulating description and price, or a Map linking description to item, you could search faster (sorted = logarithmic time, map = constant time).
posted 5 years ago
Have you not come across array initialisers? We can change your code toOf course that is still error‑prone. Because you missed out prices, many of those prices are out of synch with the descriptions. You also need to verify that both arrays are the same length.
He's my best friend. Not yours. Mine. You can have this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database