You've been working at this for a while, so your code has become a little scattered. I'm sorry to say it, but you need to open a
new project and start over -- that will help you best.
The reason is that you have too many variables and conditions scattered about, many duplicating storage for the same information, and most of them unused. [That is the reason you are having trouble. Your
rainfall[] array on line 24 is never assigned any value, so it has
random data, and finding the lowest day (which would actually work correctly except for one error) will only find the smallest of 30 random numbers.]
You cannot use sizeof() on an array. Make sure to pass the size of the array as an additional argument to the functions that use it. (In the following examples,
days is the size of the
rainfall[] array.)
The maximum number of days in a month is 31, so your
rainfall[] array must not have less than 31 elements.
When you rewrite this, try to make it so that there is ever only
one place that a piece of data is stored.
For example, if you want to track the rainfall for a month:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
void get_months_rainfall_from_user( float rainfall[], int days );
int main()
{
int days_per_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int month;
float rainfall[ 31 ];
// Do whatever is necessary here to get the month number from the user.
// For simplicity in this example, I'll just assume April.
month = 3; // April = 4-1
get_months_rainfall_from_user( rainfall, days_per_month[ month ] );
...
}
void get_months_rainfall_from_user( float rainfall[], int days )
// function:
// Get each day's rainfall from the user and store it in the supplied array.
// arguments:
// rainfall[] -- where to put each day's rainfall
// days -- the number of days in the month
{
for (int day = 0; day < days; ++day)
{
cout << "\nPlease enter the rainfall for day " << (1+day) << ": ";
cin >> rainfall[ day ];
}
}
|
Make sure each thing you do has just one simple purpose. For example, the
get_months_rainfall_from_user() function just gets the user to type-in the daily rainfall for the proper number of days and fill the main function's
rainfall[] array.
If you also want to know the total amount of rainfall in the month, create a
separate function to help:
1 2 3 4 5 6 7 8 9
|
float get_total_rainfall( float rainfall[], int days )
// function:
// Sum the elements of the rainfall[] array.
// arguments:
// rainfall[] -- a list of the amount of rain that fell per day
// days -- the number of days in the list
{
...
}
|
In the main function, you can then get that number:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void get_months_rainfall_from_user( float rainfall[], int days );
float get_total_rainfall( float rainfall[], int days );
int main()
{
int days_per_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int month;
float rainfall[ 31 ];
float total_rainfall;
// Do whatever is necessary here to get the month number from the user.
// For simplicity in this example, I'll just assume April.
month = 3; // April = 4-1
get_months_rainfall_from_user( rainfall, days_per_month[ month ] );
total_rainfall = get_total_rainfall( rainfall, days_per_month[ month ] );
...
}
|
Each function has a simple, explicit purpose. Do that with your code.
Good luck!