Unecpaline Erroneous for value of lowest day and no return of index

The program outputs a value for lowest day but it's very of and it doesn't return the position of the lowest day

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<iostream>
#include<math.h>
#include<conio.h>



float getMonthly(const int);
float avg_daily( int days[], float month[], int number);
float lowest_day(float rainfall[],int day, int days[]);

using namespace std;

int main(){

	int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
	float Jan_Rain=0;
	float Feb_Rain=0;
	float Year_Rain=0;
	float month[1];
	int month_number=0;
	float avg_daily_rain=0;
	float Jan_avg=0;
	float Jan_Low=0;
	float rainfall[30];
	int day;
	int position;



    Jan_Rain = getMonthly(days[0]);
    cout<<"The monthly rainfall for January is ";
    cout<<Jan_Rain<<endl;
    month[0]=Jan_Rain;

    Feb_Rain = getMonthly(days[1]);
    cout<<"The monthly rainfall for February is ";
    cout<<Feb_Rain<<endl;
    month[1]=Feb_Rain;

    cout<<"The total rainfall for the year is ";
    cout<<Year_Rain;

	for ( month_number=0; month_number<=1; month_number++)
	{
		Year_Rain+=month[month_number];
	}

	cout<<Year_Rain<< endl;

	Jan_avg= avg_daily(&days[0],&month[0],0);
    cout<<"The average daily rainfall for january is ";
    cout<<Jan_avg<<endl;



	Jan_Low= lowest_day(rainfall, day, days);
    cout<<" The lowest rainfall for January is";
    cout<<Jan_Low<<" ";
    return position;

	getch();
}

float getMonthly( const int days )
{
  int smallest,largest;
  float rain_total = 0.0f;
  int day = 0;
  for ( int day = 0; day < days; ++day )
    {
		cout << "\nPlease enter the rainfall for day " << 1+day << ": ";
		float rainfall[30] ;
        cin >> rainfall[day];
		rain_total += rainfall[day];
    }

  return rain_total;
}

float avg_daily(int days[], float month[], int number)
{
    return month[number]/days[number];
}

float lowest_day(float rainfall[], int day, int days[])
{
	int smallest=0;
	int position=0;
    smallest =rainfall[0];

    for( int i = 0; i < sizeof(days); ++i )
    {
       if( rainfall[ i ] < smallest ){
        smallest=rainfall[ i ];
        position=i;
       }
    }
    return smallest;

}
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!
Topic archived. No new replies allowed.