Trying to find the lowest rainfall for entire year

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
#include <iostream>
#include <string>
using namespace std;

void findlowest(string month, float rainfall, float lowest, int count)
{
    cout<<"Enter Rainfall: "<<endl; // I am trying to input amount of rainfall for each month 
       for (count = 0; count <= 12; count++)
       {
          cout<<month[count]<< " : ";
          cin>>rainfall[count];
       }
                
        lowest = rainfall[0];	//tying to find the lowest rainfall for the whole year
	    for (count=0; count <= 12; count++) 
      	{
		  if (rainfall[count] <= lowest)
		     {
		        lowest = rainfall[count];
		        lowest = month[count];
  cout<<"Month: " month[count] <<"Rainfall: " rainfall[count] " "<<lowest<< " "<<count;   
  cout<<lowest<<endl;		
}     
       
}

int main()
{
  const int num = 12;
  string month[0] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  float rainfall[0];
  float lowest;
  int count = 0;   
    
  findlowest(month, rainfall, lowest, count); //trying to print results as follows Month: January Rainfall: 20 10 etc. 

  
  return 0;
}
Last edited on
1) Show the function prototype/declaration before main and define after main. A proper way as I think.
2) temporary variables used in loops should be better to declare in loop.
3) Use main function to take input, and use your own function just to fine the lowest rainfall.
4) Use string extraction operator << between a string and a variable in cout.
5) the program i typed is based on linear search, so any two smallest equal values, the program will show first of them as smallest rainfall.
6) the program will not search the negative values, i can do that but i think this is enough for you :P

The program is little complex for you as I can see your programming above is not much good. So, work hard and if you not understand then let me know anything with the line number in sourcecode.
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
//the program finding the lowest rainfall of the year using linear search
#include <iostream>
#include <string>
#include <conio.h> // using to stop Dos screen when program running, using getch() in this header file
using namespace std;

void findlowest(string[], float[],  int); // function declaration or prototype

int main()
{
	const int num = 12;
	string month[num] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	float rainfall[num];

	for (int a=0; a < num; ++a) // getting input rainfall from user for each month
	{
		cout << "Enter Rainfall " << month[a] << ":";
		cin >> rainfall[a];
	}
	
	findlowest(month, rainfall, num); //trying to print results as follows Month: January Rainfall: 20 10 etc.

	getch(); // to pass the screen
return 0;
} 

// function definition at end after the main
void findlowest(string fmonth[], float frainfall[], int fnum) // use varaiable name different to make function as difference from main
{	
	float temporary=frainfall[0];
	int low=0;
	int b;
	for (b = 0; b < fnum; ++b)
	{
		if ( frainfall[b] < temporary )
		{
			temporary = frainfall[b];
			low=b;
		}
	}
	cout<<"Month: " << fmonth[low] << " Rainfall: " << frainfall[low] <<  " " << "Month number " <<(low+1); 
}

thanks..
Topic archived. No new replies allowed.