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);
}
|