![]() |
| ||
| Logic error, simple stat prog I've got executable code that is partially working, so i know i've done something wrong in it. In it you type in the rainfall for each month in inches and will display the total, average, largest and smallest rainfalls(with the month). I have the the total, average and largest(except the month shows 13 everytime) and the smallest along with month being 13 are not working. It's probably some simple error here and there but I can't seem to find what I am doing wrong. I'm assuming that after I've gone through the array the index just adds another value that is making the months both 13 on the smallest/largest stats and I'm not sure what I'm to do to change it. The main function is fine as it is, it's the others that need help. #include<iostream> |
| ||
| Re: Logic error, simple stat prog Only getTotal code is correct. All three other fuctions are wrong. 1. getAverage: use getTotal to obtain a sum of all elements then divide sum by the number of elements! 2. getLargest and getSmallest: why double largest = array[100];?! No such element in the array at all. Start from array[0]value then loop from index 1. |
| ||
| Re: Logic error, simple stat prog Quote:
|
| ||
| Re: Logic error, simple stat prog Quote:
|
| ||
| Re: Logic error, simple stat prog I fixed the function call, now the only problem is that it won't display the month with the largest/smallest data. Here's what it shows, along with the code following. Enter the rainfall (in inches) for month #1: 2.5 Enter the rainfall (in inches) for month #2: 10.7 Enter the rainfall (in inches) for month #3: 32 Enter the rainfall (in inches) for month #4: 231 Enter the rainfall (in inches) for month #5: 7.8 Enter the rainfall (in inches) for month #6: 5.4 Enter the rainfall (in inches) for month #7: 2.5 Enter the rainfall (in inches) for month #8: 4.87 Enter the rainfall (in inches) for month #9: 5.8 Enter the rainfall (in inches) for month #10: 20.0 Enter the rainfall (in inches) for month #11: 2.6 Enter the rainfall (in inches) for month #12: 3.8 Total rainfall for the year was 328.97 inches. Average rainfall for the year was 86.57 inches. The largest amount of rainfall was 231.00 inches in month 13. The smallest amount of rainfall was 2.50 inches in month 13. Press any key to continue #include<iostream> |
| ||
| Re: Logic error, simple stat prog 1. That's because the modification (improvement) of getSmallest and getLargest functions was incorrect. It was wrong solution to assign a role of for loop counter to output parameter count (why "count" if it's an index of? ). Think: the 1st count value is 1 (must be 0) and the last is 12 (then you add 1 and print 13). Make obvious correction. 2. Look again to getAverage code: it's WRONG!!! Ooh, that's right code: double getAverage(double array[], int size) |
| ||
| Re: Logic error, simple stat prog I was using count because the examples that were shown used count as the name. I understand that that count should be 0, so the loop will end at 12, but i'm still struggling to understand how to get the correct index number with the largest and smallest values. I should have know the getAverage function. |
| ||
| Re: Logic error, simple stat prog In getlargest() count will act as the index of the element with the largest rainfall for that year. Since count is passed to getlargest() by reference you don't need to return anything if there are no other requirements of the function. Use some counter variable other than count to control the loop and keep track of count separately. void getLargest(double array[], int size, int &count)and back in main() use count to display the result; cout << "the largerst rainfall was " << array[count]; |
| ||
| Re: Logic error, simple stat prog the getLargest function needs to get both the index for the month and the largest value. I can't change anything in main, as it was given, so it needs to be a double. Do I need a nested for loop? |
| ||
| Re: Logic error, simple stat prog Figured it out double getLargest(double array[], int size, int &i) |
| All times are GMT -4. The time now is 6:20 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC