Rainfall

Write a modular program that analyzes a year's worth of rainfall data. In addition to main, the program should have a getData function that accepts the total rainfall for each of the 12 months from the user, and stores it in a double array. It should also have four value-returning functions that compute and return to main the totalRainfall, averageRainfall, driestMonth, and wettestMonth. These last two functions return the number of the month with the lowest and highest rainfall amounts, not the amount of rain that fell those months. Notice that this month number can be used to obtain the amount of rain that fell those months. This information should be used either by main or by a displayReport function called by main to print a summary rainfall report similar to the following:

2010 Rain Report for Neversnows County

Total rainfall: 23.19inches
Average monthly rainfall: 1.93inches
The least rain fell in January with 0.24inches
The most rain fell in April with 4.29inches


Would this be how I program it?
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
101
102
103
104
#include <iostream>
#include <string>
using namespace std;

//Prototypes
double getData(string[], double[]);
double averageRainfall(double[]);
double driestMonth(double[]);
double wettestMonth(double[]);
void displayReport (string[],double[],double);

//Array Size
const int ARRAY_SIZE = 12;

int main()
{
	//Array Info
	string name[ARRAY_SIZE] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
	double month[ARRAY_SIZE];

	//Show Info
	cout<<"This program collects the rainfall totals"<<endl;
	cout<<endl;

	double totalRainfall = getData(name,month);
	displayReport(name,month,totalRainfall);
	
	return 0;
}

//Collect Data
double getData(string name[], double month[])
{
	
	int total=0;

	cout << "Please enter the amount of rainfall for each month" << endl;
	for (int count=0; count < ARRAY_SIZE; count++)
	{
		cout<< name[count] << " : ";
		cin >> month[count];
		total += month[count];
		while (month[count] < 0)
		{
			cout << "Please re-enter a positive number for the month of " << name[count] << endl;
			cin >> month[count];
		}
	}
	return total;
}

//Show The Data
void displayReport(string name[],double rain[], double total)
{
	int hiRainMonth=wettestMonth(rain);
	int loRainMonth=driestMonth(rain);
	int avgRainMonth=averageRainfall(rain);

	cout<<"2010 Rainfall Report for Neversnows County"<<endl;
	cout<<"\nThe total rainfall is: "<<total<<endl;
	cout<<"Average monthly rainfall is"<<name[avgRainMonth];
	cout<<"The most rain fell in"<<name<< "with "<<name[hiRainMonth]<<"inches"<<endl;
	cout<<"The least rain fell in"<<name<< "with "<<name[loRainMonth]<<"inches"<<endl;
}

//Find Wettest Month
double wettestMonth (double array[])
{
	int indexOfLargest=0;

	for(int pos=0; pos<ARRAY_SIZE; pos++)
	{
		if(array[pos]>array[indexOfLargest])
			indexOfLargest=pos;
	}
	return indexOfLargest;
}

//Find Driest Month
double driestMonth (double array[])
{
	int indexOfSmallest=0;

	for(int pos=0; pos<ARRAY_SIZE; pos++)
	{
		if(array[pos]<array[indexOfSmallest])
			indexOfSmallest=pos;
	}
	return indexOfSmallest;
}

double avgRainfall(double month [])
{
	int count=0;
	double avgRain=0;
	double rainSum=0;

	for (count =0; count <=11; count++)
	{
		rainSum = rainSum + month[ARRAY_SIZE];
	}
	avgRain = rainSum / 12;
	return avgRain;
}
It sounds to me like your professor also wants a separate function that computes the total rainfall. It sounds like getData shouldn't return anything, and their should be a separate totalRainfall function. Other then that, yes.
But if I try to run it, I get these errors:

c:\users\jimmy\documents\visual studio work\final\final\final.cpp(42): warning C4244: '+=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\jimmy\documents\visual studio work\final\final\final.cpp(55): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
1>c:\users\jimmy\documents\visual studio work\final\final\final.cpp(56): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
1>c:\users\jimmy\documents\visual studio work\final\final\final.cpp(57): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>final.obj : error LNK2019: unresolved external symbol "double __cdecl averageRainfall(double * const)" (?averageRainfall@@YANQAN@Z) referenced in function "void __cdecl displayReport(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * const,double * const,double)" (?displayReport@@YAXQAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QANN@Z)
1>C:\Users\Jimmy\Documents\Visual Studio Work\final\Debug\final.exe : fatal error LNK1120: 1 unresolved externals

Edit:
I tried doing it with a different way of coding.
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <string>

using namespace std;
	 
const int NUM_MONTHS = 12;
const int STRING_SIZE = 10;
	 
void wettestMonth(double [], char [][STRING_SIZE]);
void driestMonth(double[], char [][STRING_SIZE]);
void totalRain(double[]);
void averageRain(double[]);
void zeroArray(string []);
	 
int main()
{
    double rain[NUM_MONTHS];
    int count;
	 
    char months[NUM_MONTHS][STRING_SIZE] =
                { "January", "February", "March",
                  "April", "May", "June", "July",
                  "August", "September", "October",
                  "November", "December" };
 
	
   for (count = 0; count < NUM_MONTHS; count++)
    {
        cout << "Enter the rainfall for the month of ";
		cout << months[count] << " in inches: ";
		cin >> rain[count]; 
	
        while (rain[count] < 0)
        {
            cout << "ERROR: You can't have negative rainfall!\n";
            cout << "Please enter a number greater or equal to 0:\n";
            cin >> rain[count];
        }
    }
		
	    cout << "\n2010 Rain Report for Neversnows County\n";
	    cout << "----------------\n";
	 
	   
	 
	   totalRain(rain);

	   averageRain(rain);

	   driestMonth(rain, months);

	   wettestMonth(rain, months);
	 
 return 0;
}

void wettestMonth(double rain[], char months[][STRING_SIZE])
{
    string month[12];
 
    zeroArray(month);
 
    double highest = rain[0];
    month[0] = months[0];
	 
    for(int i = 1; i < 12; ++i)
    {
        if(rain[i] > highest) {
            zeroArray(month);
            month[i] = months[i];
            highest = rain[i];
     }
	        else if(rain[i] == highest)
        {
            month[i] = months[i];
        }
    }
	 
    cout << "Highest Rainfall: " << highest << " inches.\n";
     

    for(int i = 0; i < 12; ++i) {
        if(month[i] != "")
           cout << "Month with highest rainfall: " << month[i] << endl;
    }
    cout << endl;
}  
	 
void driestMonth(double rain[], char months[][STRING_SIZE])
{
    string month[12];
 
    zeroArray(month);
	 
    double lowest = rain[0];
	    month[0] = months[0];
	 
    for(int i = 1; i < 12; ++i)
    {
        if(rain[i] < lowest) {
           zeroArray(month);
           month[i] = months[i];
           lowest = rain[i];
        }
        else if(rain[i] == lowest)
        {
            month[i] = months[i];
        }
    }
	 
    cout << "Lowest Rainfall: " << lowest << " inches" << endl;
	 
    for(int i = 0; i < 12; ++i)
	{
        if(month[i] != "")
           cout << "Month with lowest rainfall: "<< month[i] << endl;
    }
    cout << endl;
}

void totalRain(double rain[])
{
   double total = 0;
   for (int index = 0; index < NUM_MONTHS; index++)
   total += rain[index];
 
    cout << "Total Rainfall: " << total << " inches.\n\n";
}

void averageRain(double rain[])
{
	
	int count=0;
	double avgRain=0;
	double rainSum=0;

	for (count =0; count <=11; count++)
	{
		rainSum = rainSum + rain[count];
	}
	avgRain = rainSum / 12;
	cout<< "Average Rainfall: " <<avgRain<<" inches. \n\n";
}
 
void zeroArray(string arr[])
{
    for(int i = 0; i < 12; ++i)
        arr[i] = "";
}
Last edited on
These linker error messages can be a little difficult to read sometimes. But if you read them carefully you should be able to understand what its telling you.

1>final.obj : error LNK2019: unresolved external symbol "double __cdecl averageRainfall(double * const)" (?averageRainfall@@YANQAN@Z) referenced in function "void __cdecl displayReport(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * const,double * const,double)" (?displayReport@@YAXQAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QANN@Z)

displayReport is calling "averageRainfall" but the compiler cant find it.
Did you mean "averageRain" ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

//Show The Data
void displayReport(string name[],double rain[], double total)
{
	int hiRainMonth=wettestMonth(rain);
	int loRainMonth=driestMonth(rain);
	int avgRainMonth=averageRainfall(rain);

	cout<<"2010 Rainfall Report for Neversnows County"<<endl;
	cout<<"\nThe total rainfall is: "<<total<<endl;
	cout<<"Average monthly rainfall is"<<name[avgRainMonth];
	cout<<"The most rain fell in"<<name<< "with "<<name[hiRainMonth]<<"inches"<<endl;
	cout<<"The least rain fell in"<<name<< "with "<<name[loRainMonth]<<"inches"<<endl;
}


Function appears to be called "averageRain" not "averageRainfall".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void averageRain(double[]);
void averageRain(double rain[])
{
	
	int count=0;
	double avgRain=0;
	double rainSum=0;

	for (count =0; count <=11; count++)
	{
		rainSum = rainSum + rain[count];
	}
	avgRain = rainSum / 12;
	cout<< "Average Rainfall: " <<avgRain<<" inches. \n\n";
}
Topic archived. No new replies allowed.