I'm constructing a rainfall program. I have a preset list of rainfall values as follow:
January 3.35
February 2.83
March 3.9
April 3.19
May 4.37
June 3.74
July 3.9
August 3.7
September 4.09
October 3.35
November 3.43
December 3.19
As you can see the lowest value should be February with 2.83 inches of rain, and highest should be May with 4.37 inches of rain. However, with the following code,it says that
The highest rainfall of 4.09 occurred in September
The lowest rainfall of 3.35 occurred in January
Here is my code. Please assist me in finding errors as I've worked on this for several hours now to no answers.
Both your highest and lowest functions re a bit off. In highest you are checking the "rainfalls" against "highest", but "highest" never changes its value. This is a quick fix for your code. I think it could be done better and still return the index that you need:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int findHighest(double rainfalls[], string months[], int size)
{
int highest = 0;
double high{ 0.0 };for (int index = 0; index < MONTHSIZE; index++)
{
//rainfalls[index]; // <--- Not needed. Does nothing.
if (rainfalls[index] > high)
{
high = rainfalls[index];
highest = index;
}
}
return highest;
}
The same concept is used for finding the lowest, except that low would start with a high number to work properly.
Not sure what you input file looks like, but from what you posted here I had to filter out the month names to get to the amount.
In main I changed the output of the final or loop to outputFile << std::setw(10) << months[c] << setw(20) << std::fixed << std::showpoint << std::setprecision(2) << rainfalls[c] << endl;. Call me crazy, but it lines up the output better.
I noticed in the "readRainfalls" function that it return an int, bur back in main you do not use the return value. Just for thought in the else section I use "exit(1);" to leave the program at that point. When you think about it if the file does not pen there is no point in continuing the program. Especially when you do not use the return value.
Since everyhing is contained in one file and "MONTHSIZE" is a global variable it does not need to be passed to the functions that would use it.
Thank you so much for your help! My program works normally now. As for the return values, I put them there because they were required--I'm not sure why they are either. Also, thank you very much for the formatting advice! I was just going to go with it because I didn't know a simple way to make the numbers line up, but they look beautiful now all thanks to you :)
If you don't mind, could you please clarify why in my code before, " 'highest' never changes its value"? I do have some comprehension issues with loops.
Once again, I really appreciate your assistance! Who knows how much longer I would've spent on this without you.
rainfalls[index];
if (rainfalls[index] > highest)
{
highest = index;
}
highest is the index in the array, presumably 0,1,2,3, ...
and rainfalls[index] is your data, could be whatever, lets say 10, 40, 20, 5, 100 ....
you can't compare those values in any meaningful way, this is a bug.
highest / index lets you get back to the biggest one (rainfalls[highest]).
But you also need the value, to compare them against each other (rainfalls[old biggest] vs rainfalls[unknown value] which is what Andy's code is doing with his changes.
Sorry for my bad typing. Sometimes my ole lap top keyboard does not work right.
Let us try this:
In the function for highest the first iteration of the loop "highest" has a value zero. So the comparison "rainfalls[0]" is greater than zero and "highest" is set equal to "index" which is zero. On the second iteration of the loop "rainfalls[1] is compared to "highest" which is still zero, so "highest" is now set to one. On the second iteration of the loop "rainfalls[2] is compared to "highest" which is now one. Follow this through and eventually "index" will set "highest" to a number greater than the highest number in the file. The point here is that you are comparing "rainfalls[x]" to the value of index not the highest value in the array.
Now when I added the variable "high" and compare "rainfalls[x]" against "high", when this is true high is set to the value of "rainfalls[x]" and on the next iteration of the loop "rainfalls[x]" has a new value to compare to and if true it changes again. I false it goes to the next iteration of the loop. Now when the entire array has been checked "high" contains the largest value in the array and "highest" contains the index number of that array element.
The function for lowest value works mostly the same way except that a variable like "low" is set to a number higher than what might be in the array, so that you can work down instead of up.
You say the the functions need to return a value, but you never make use of some of the return values. A good example is "readRainfalls" function. In the function's else block I would change "-1" to "1" and then in main you could change the function call to:
1 2 3 4 5 6
if (readRainfalls(rainfalls, months)) // <--- "MONTHSIZE" not needed since it is a global variable.
{
// Error message.
// pause code.
exit(1);
}
Since "0" and "1" are the same as false and true it makes the if statement work because the if statement is evaluated after the function has returned a value. A value of "0" and the if statement is passed over. A value of "1" will execute the block and end the program. Normally I tend to check the file stream after the open statement and deal with it then instead of through a return value, but a return value will work.
I'm starting to get the hang of loops now, thank you jonnin and Andy!
The return values are interesting--something I haven't really learned yet. I will look more into it, but I have included the exit in my code now so certain lines of main does not have to execute. Thank you again for your help!! Hope you can get some keyboard update--not that I mind ;)