| | |
Logic error, simple stat prog
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 22
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
#include<iostream> #include <iomanip> using namespace std; // Function prototypes double getTotal(double [], int); double getAverage(double [], int); double getLargest(double [], int, int &); double getSmallest(double [], int, int &); int main() { const int NUM_MONTHS = 12; double rainFall[NUM_MONTHS]; // Stores total rainfall for each month // Input rainfall amounts and store them in the 12 array locations for (int month = 0; month < NUM_MONTHS; month++) { cout << "Enter the rainfall (in inches) for month #"; cout << (month + 1) << ": "; cin >> rainFall[month]; while (rainFall[month] < 0) { cout << "Rainfall must be 0 or more. Please re-enter: "; cin >> rainFall[month]; } } // Display the total rainfall cout << fixed << showpoint << setprecision(2) << endl; cout << "Total rainfall for the year was "; cout << setw(5) << getTotal(rainFall, NUM_MONTHS) << " inches." << endl; // Display the average rainfall cout << "Average rainfall for the year was "; cout << setw(5) << getAverage(rainFall, NUM_MONTHS) << " inches." << endl << endl; // Display the months with the largest & smallest amounts of rain. // The variable index is passed by reference to the getLargest & // getSmallest functions, so they can assign it the subscript of the // array element having the largest, or smallest, amount of rainfall. int index; cout << "The largest amount of rainfall was " << setw(5); cout << getLargest(rainFall, NUM_MONTHS, index) << " inches in month "; cout << (index + 1) << "." << endl; cout << "The smallest amount of rainfall was " << setw(5); cout << getSmallest(rainFall, NUM_MONTHS, index) << " inches in month "; cout << (index + 1) << "." << endl; return 0; } double getTotal(double array[], int size) { double total = 0; for(int count = 0; count < size; count++) { total += array[count]; } return total; } double getAverage(double array[], int size) { double total; double average; total = 0; for(int count = 0; count < size; count++) { total += array[count]; } for (count = 0; count < size; count++) { average = total / array[count]; } return average; } double getLargest(double array[], int size, int &count) { double largest = array[100]; for (count = 0; count < size; count++) { if (array[count] > largest) largest = array[count]; } return largest; } double getSmallest(double array[], int size, int &count) { double smallest = array[100]; for (count = 0; count < size; count++) { if (array[count] < smallest) smallest = array[count]; } return smallest; }
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
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. •
•
Join Date: Oct 2008
Posts: 22
Reputation:
Solved Threads: 0
•
•
•
•
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: whydouble largest = array[100];?! No such element in the array at all. Start fromarray[0]value then loop from index 1.
•
•
Join Date: Oct 2008
Posts: 22
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
#include<iostream> #include <iomanip> using namespace std; // Function prototypes double getTotal(double [], int); double getAverage(double [], int); double getLargest(double [], int, int &); double getSmallest(double [], int, int &); int main() { const int NUM_MONTHS = 12; double rainFall[NUM_MONTHS]; // Stores total rainfall for each month // Input rainfall amounts and store them in the 12 array locations for (int month = 0; month < NUM_MONTHS; month++) { cout << "Enter the rainfall (in inches) for month #"; cout << (month + 1) << ": "; cin >> rainFall[month]; while (rainFall[month] < 0) { cout << "Rainfall must be 0 or more. Please re-enter: "; cin >> rainFall[month]; } } // Display the total rainfall cout << fixed << showpoint << setprecision(2) << endl; cout << "Total rainfall for the year was "; cout << setw(5) << getTotal(rainFall, NUM_MONTHS) << " inches." << endl; // Display the average rainfall cout << "Average rainfall for the year was "; cout << setw(5) << getAverage(rainFall, NUM_MONTHS) << " inches." << endl << endl; // Display the months with the largest & smallest amounts of rain. // The variable index is passed by reference to the getLargest & // getSmallest functions, so they can assign it the subscript of the // array element having the largest, or smallest, amount of rainfall. int index; cout << "The largest amount of rainfall was " << setw(5); cout << getLargest(rainFall, NUM_MONTHS, index) << " inches in month "; cout << (index + 1) << "." << endl; cout << "The smallest amount of rainfall was " << setw(5); cout << getSmallest(rainFall, NUM_MONTHS, index) << " inches in month "; cout << (index + 1) << "." << endl; return 0; } double getTotal(double array[], int size) { double total = 0; for(int count = 0; count < size; count++) { total += array[count]; } return total; } double getAverage(double array[], int size) { double total; double average; for(int count = 0; count < size; count++) { total += array[count]; } for (count = 0; count < size; count++) { average = getTotal(array, size) / array[count]; } return average; } double getLargest(double array[], int size, int &count) { double largest = array[0]; for (count = 1; count < size; count++) { if (array[count] > largest) largest = array[count]; } return largest; } double getSmallest(double array[], int size, int &count) { double smallest = array[0]; for (count = 1; count < size; count++) { if (array[count] < smallest) smallest = array[count]; } return smallest; }
Last edited by Foe89; Dec 1st, 2008 at 1:55 pm.
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:
2. Look again to getAverage code: it's WRONG!!!
Ooh, that's right code:
c++ Syntax (Toggle Plain Text)
double getAverage(double array[], int size) { return getTotal(array,size) / size; }
•
•
Join Date: Oct 2008
Posts: 22
Reputation:
Solved Threads: 0
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.
I should have know the getAverage function.
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
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. and back in main() use count to display the result;
cout << "the largerst rainfall was " << array[count];
C++ Syntax (Toggle Plain Text)
void getLargest(double array[], int size, int &count) { count = 0; //assume first element is largest for (int i = 1; i < size; i++) { if (array[i] > array[count]) { count = i; } } }
cout << "the largerst rainfall was " << array[count];
Klatu Barada Nikto
•
•
Join Date: Oct 2008
Posts: 22
Reputation:
Solved Threads: 0
Figured it out
C++ Syntax (Toggle Plain Text)
double getLargest(double array[], int size, int &i) { double largest = array[0]; for (int count = 0; count < size; count++) { if (array[count] > largest) {largest = array[count]; i = count;} } return largest; } double getSmallest(double array[], int size, int &i) { double smallest = array[0]; for (int count = 0; count < size; count++) { if (array[count] < smallest) {smallest = array[count]; i = count; } } return smallest; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Simple Varifacation please
- Next Thread: Copy constructor not being invoked
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






