| pq1wx1p (17) | |||
|
The findLargestDollarLoss function keeps getting the output of 0, did I mess up somewhere in the function? I tried setting largestLoss_so_far to equal dollarProfitLoss[d], but it got the same result. The input file looks like this IBM 100 93.2 98.6 MER 200 67.2 43.89 QQQ 300 78.9 70.0 C 200 35.78 50.02 CSCO 400 45.67 57.23 And the output file looks like this now. //In this order: Name, shares, buy price, current price, dollar profit loss, percent loss, IBM 100.00 93.20 98.60 540.00 5.79 MER 200.00 67.20 43.89 -4662.00 -34.69 QQQ 300.00 78.90 70.00 -2670.00 -11.28 C 200.00 35.78 50.02 2848.00 39.80 CSCO 400.00 45.67 57.23 4624.00 25.31 Largest gain: $4624.00 Largest loss: $0.00
| |||
|
Last edited on
|
|||
| hanst99 (2845) | ||
|
Too many arrays. There is a reason stuff like vectors, structs etc exists. As to your error:
I can derive it from the context, but if you calculate loss as a positive value you'd have to use < instead of >. | ||
|
|
||
| buffbill (416) | |
|
To find the largest number in an array: 1.) Iterate through the array using a loop and assign the first element (ie array[0]) the value 'largest'. 2.) for the remaining elements if the value is greater than 'largest' assign it the value 'largest' 3.)after exiting the loop print out the value 'largest' 4.)Use exactly the same principle for finding 'smallest' or the greatest negative number | |
|
|
|
| Framework (3242) | |||
I wrote a template method to find the largest value. This is it, exactly:
It's pretty old and I'm sure there are faster ways of doing this, but it works. However, it only works with numerical types. | |||
|
|
|||
| hanst99 (2845) | |
| Framework - you do realize that there is an STL algorithm called max_element that just does that, just faster and without the need to provide the array size and without the restriction to types that implement the > operator, do you? | |
|
|
|
| Framework (3242) | |
| Nope, never knew that. Thanks for telling me that by the way. Why didn't you mention it before? | |
|
|
|