Using the 3rd parameter of a function - c++

Can anyone help me figure out how to get the 3rd parameter of the getLowest/getHighest function to reference the names array that has the months of the year and display the names of the month when I call for it? What's supposed to happen with those functions is that they are supposed to be able to give the name of the month that corresponds with the lowest/highest amount in the array. I can't seem to get it down. That's the last thing I need for this code and I'm trying very hard to figure it out. Any help would be much appreciated. Thank you.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Function prototypes
double getTotal(double [], int);
double getAverage(double [], int);
double getLowest(double [], int, int&);
double getHighest(double [], int, int&);
int main()
{
const int months = 12;
string names[months] = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
double rainfall[months];
double total, average, maxRain, minRain;
int indexofLowest, indexofHighest;
//Input from using for the rainfall amounts
std::cout << "Please enter the amount of rainfall in inches, that fell in each month.\n";
std::cout << "Enter the amount of rainfall for " << names[0];
std::cin >> rainfall[0];
std::cout << "Enter the amount of rainfall for " << names[1];
std::cin >> rainfall[1];
std::cout << "Enter the amount of rainfall for " << names[2];
std::cin >> rainfall[2];
std::cout << "Enter the amount of rainfall for " << names[3];
std::cin >> rainfall[3];
std::cout << "Enter the amount of rainfall for " << names[4];
std::cin >> rainfall[4];
std::cout << "Enter the amount of rainfall for " << names[5];
std::cin >> rainfall[5];
std::cout << "Enter the amount of rainfall for " << names[6];
std::cin >> rainfall[6];
std::cout << "Enter the amount of rainfall for " << names[7];
std::cin >> rainfall[7];
std::cout << "Enter the amount of rainfall for " << names[8];
std::cin >> rainfall[8];
std::cout << "Enter the amount of rainfall for " << names[9];
std::cin >> rainfall[9];
std::cout << "Enter the amount of rainfall for " << names[10];
std::cin >> rainfall[10];
std::cout << "Enter the amount of rainfall for " << names[11];
std::cin >> rainfall[11];
//Get total
total = getTotal(rainfall, months);
//Get average
average = getAverage(rainfall, months);
//Get the max amount of rain
maxRain = getHighest(rainfall, months, indexofHighest);
//Get the min amount of rain
minRain = getLowest(rainfall, months, indexofLowest);
//Display the total, average, highest/lowest
std::cout << "The total amount of rain for the year is " << total << " inches.\n";
std::cout << "The average amount of rain monthly is " << average << " inches per month.\n";
std::cout << "The month that had the highest amount of rainfall is " << names[indexofHighest] << " with " << maxRain << " inches.\n";
std::cout << "The month that has the lowest amount of rainfall is " << names[indexofLowest] << " with " << minRain << " inches.\n";
return 0;
}
//Definition of function getTotal
double getTotal(double rainfall[], int months)
{
double total = 0;
for (int count = 0; count < months; count++)
{
total += rainfall[count];
}
return total;
}
//Definition of function getAverage
double getAverage(double rainfall[], int months)
{
double total = 0;
double average = 0.0;
for (int count = 0; count < months; count++)
{
total += rainfall[count];
average = total / months;
}
return average;
}
//Defintion of function getLowest
double getLowest(double rainfall[], int months, int indexofLowest)
{
int count;
double lowest;
lowest = rainfall[0];
for (count = 1; count < months; count++)
{
if (rainfall[count] < lowest)
lowest = rainfall[count];
}
return lowest;
}
//Definition of function getHighest
double getHighest(double rainfall[], int months, int indexofHighest)
{
int count;
double highest;
highest = rainfall[0];
for (count = 1; count < months; count++)
{
if (rainfall[0] > highest)
highest = rainfall[count];
}
return highest;
}

I tried your code. You have a few bugs. I guess you need to try harder in the future. Anyways here is a working solution with minimal changes to your existing code -
//Defintion of function getLowest
double getLowest(double rainfall[], int months, int & indexofLowest)
{
int count;
double lowest;
lowest = rainfall[0];
for (count = 1; count < months; count++)
{
if (rainfall[count] < lowest)
{
lowest = rainfall[count];
indexofLowest = count;
}
}
return lowest;
}
//Definition of function getHighest
double getHighest(double rainfall[], int months, int & indexofHighest)
{
int count;
double highest;
highest = rainfall[0];
for (count = 1; count < months; count++)
{
if (rainfall[count] > highest)
{
highest = rainfall[count];
indexofHighest = count;
}
}
return highest;
}

You have to set indexofLowest and indexofHighest in your functions (and match their prototype):
//Defintion of function getLowest
double getLowest(double rainfall[], int months, int& indexofLowest)
int count;
double lowest;
lowest = rainfall[0];
for (count = 1; count < months; count++)
{
if (rainfall[count] < lowest) {
lowest = rainfall[count];
indexofLowest = count;
}
}
return lowest;
}
//Definition of function getHighest
double getHighest(double rainfall[], int months, int& indexofHighest)
{
int count;
double highest;
highest = rainfall[0];
for (count = 1; count < months; count++)
{
if (rainfall[count] > highest) { //there was a bug here in your code: ... rainfall[0]
highest = rainfall[count];
indexofHeighest = count;
}
}
return highest;
}
you can also use a function to input data (and maybe add a little error checking):
//Input from using for the rainfall amounts
int readData(string monthNames[], double rain[], int n) {
int i;
std::cout << "Please enter the amount of rainfall in inches, that fell in each month.\n";
for ( i=0; i<n; i++) {
std::cout << "Enter the amount of rainfall for " << monthNames[i];
std::cin >> rain[i]; //check this value somehow!
}
return i;
}

Related

Why does the following code shows the last month in the array instead of showing the months with the highest and the lowest rainfall?

I have an assignment where I need to get user input for the amount of rainfall for each month. At the end I need to average the rainfall as well as display the month (using the name of the month) that had the highest and the lowest rainfall. Everything works with the exception of displaying the lowest and highest month. For some reason my code always displays December instead of the actual lowest and highest month. The lowestMonth = MONTHS[count]; and highestMonth = MONTHS[countup]; are the lines of code that I suspect are causing some problems. Appreciate any help the community can offer.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
const int SIZE = 12;
double RAINFALL[SIZE];
string MONTHS[SIZE] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
for (int counter = 0; counter < SIZE; counter++)
{
cout << "Please enter rainfall for " << MONTHS[counter] << ": ";
cin >> RAINFALL[counter];
while (RAINFALL[counter] < 0.00) // Input validation to prevent neg amounts being entered
{
cout << "Invalid Data (negative rainfall)!" << endl;
cout << "Please re-enter rainfall for " << MONTHS[counter] << ": ";
cin >> RAINFALL[counter];
}
}
int tnum;
double average, sum = 0;
for (tnum = 0; tnum < SIZE; tnum++)
sum += RAINFALL[tnum];
average = sum / SIZE;
cout << "Average rainfall = " << average << endl;
int count;
int lowest;
string lowestMonth = MONTHS[0];
lowest = RAINFALL[0];
for (count = 1; count < SIZE; count++)
{
if (RAINFALL[count] < lowest)
lowest = RAINFALL[count];
lowestMonth = MONTHS[count];
}
cout << "Lowest rainfall in " << lowestMonth << " of: " << lowest << endl;
int countup;
int highest;
string highestMonth = MONTHS[0];
highest = RAINFALL[0];
for (countup = 1; countup < SIZE; countup++)
{
if (RAINFALL[countup] > highest)
highest = RAINFALL[countup];
highestMonth = MONTHS[countup];
}
cout << "Highest rainfall in " << highestMonth << " of: " << highest << endl;
return 0;
}
You are missing a bracket in your if-statement, so only the first line is executed.
for (count = 1; count < SIZE; count++)
{
if (RAINFALL[count] < lowest) { // <-- BRACKET
lowest = RAINFALL[count];
lowestMonth = MONTHS[count];
} // <-- BRACKET
}
There are more modular ways of doing this, of course:
std::string lowest_month = MONTHS[
std::min_element(&RAINFALL[0], &RAINFALL[SIZE]) - &RAINFALL[0]
];

Finding the subscript of an array in a function in c++

I have this homework and i've completed this up to now. where i am stuck...
Basically i need to get the largest amount of rainfall and display it (which i already do have completed it) but also the number of the month.
This is where i am having an intense headache...
could you guys help me out with some code?
#include <iostream>
#include <iomanip>
using namespace std;
double yearlyRainAverage(double[], const int);
double smallestRainfall(double [], const int);
double largestRainfall(double [], const int);
int searchHighestMonth(double[], int);
int main() {
const int months = 12;
double inchesOfRain[months];
double sumOfAllMonths=0;
int maxMonthPosition = searchHighestMonth(inchesOfRain, months);
for (int count = 0; count < months; count++)
{
cout<<"Enter the rainfall (in inches) for month #"<< count + 1<<": ";
cin>>inchesOfRain[count];
sumOfAllMonths += inchesOfRain[count];
if(inchesOfRain[count] < 0){
cout <<"Rainfall must be 0 or more.\n";
cout<<"please re-enter: "<<endl;
cout<<"Enter the rainfall (in inches) for month #"<< count + 1<<": ";
cin>>inchesOfRain[count];
}
}
cout << fixed << showpoint << setprecision(2) << endl;
cout<<"the total rainfall for the year is "<<sumOfAllMonths<<" inches"<<endl;
cout<<"the average is "<<yearlyRainAverage(inchesOfRain, 12)<<" inches"<<endl;
// cout<<"The smallest amount of rainfall was: "<<smallestRainfall(inchesOfRain, 12)<<" inches ";
// cout<<"in month "<<(monthPosition+1)<<endl;
cout<<"The largest amount of rainfall was: "<<largestRainfall(inchesOfRain, 12)<<" inches ";
cout<<"in month "<<maxMonthPosition+1<<endl;
return 0;
}
double yearlyRainAverage(double inchesofrain[], const int months){
double sum=0;
for(int i=0;i<months; i++){
sum+=inchesofrain[i];
}
return sum/months;
}
double smallestRainfall(double inchesofrain[], const int months){
double smallest;
int i;
smallest=inchesofrain[0];
for(i=0; i < months; i++){
if(inchesofrain[i] < smallest){
smallest = inchesofrain[i];
}
}
return smallest;
}
double largestRainfall(double inchesofrain[], const int months){
double largest;
int i;
largest=inchesofrain[0];
for(i=0; i < months; i++){
if(inchesofrain[i] > largest){
largest = inchesofrain[i];
}
}
return largest;
}
Here is where i think is the issue. i think my logic is wrong. But, i am not sure.
int searchHighestMonth(double inchesofrain[], int value){
int max = 0;
for ( int i=1; i < value; ++i) {
if ( inchesofrain[max] < inchesofrain[i] ) {
max = i;
}
}
return max;
}
The problem is that you are searching for your largest rainfall before you have taken the input of your rainfall from the user.
Move this line:
int maxMonthPosition = searchHighestMonth(inchesOfRain, months);
After the input for loop.
I went ahead and tested all of your code again, redirecting stdin (cin) from an input string, which I find very helpful for testing so I don't have to keep inputting. here is my code:
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
double yearlyRainAverage(double[], const int);
double smallestRainfall(double [], const int);
double largestRainfall(double [], const int);
int searchHighestMonth(double inchesofrain[], int value) {
int max = 0;
for (int i = 1; i < value; ++i) {
if (inchesofrain[max] < inchesofrain[i]) {
max = i;
}
}
return max;
}
int main() {
//#define testing // comment this line out to use std::cin for input
#ifdef testing
// code to get stdin input from a local buffer
std::string input_string{"4 5 6 7 8 9 10 3 2 3 4 5"};
std::streambuf *orig = std::cin.rdbuf();
std::istringstream input(input_string);
std::cin.rdbuf(input.rdbuf());
#endif
const int months = 12;
double inchesOfRain[months];
double sumOfAllMonths = 0;
for (int count = 0; count < months; count++) {
cout << "Enter the rainfall (in inches) for month #" << count + 1 << ": " << std::endl;
cin >> inchesOfRain[count];
sumOfAllMonths += inchesOfRain[count];
while (inchesOfRain[count] < 0) {
cout << "Rainfall must be 0 or more.\n";
cout << "please re-enter: " << endl;
cout << "Enter the rainfall (in inches) for month #" << count + 1 << ": " << std::endl;
cin >> inchesOfRain[count];
}
}
int maxMonthPosition = searchHighestMonth(inchesOfRain, months);
cout << fixed << showpoint << setprecision(2) << endl;
cout << "the total rainfall for the year is " << sumOfAllMonths << " inches" << endl;
cout << "the average is " << yearlyRainAverage(inchesOfRain, months) << " inches" << endl;
// cout<<"The smallest amount of rainfall was: "<<smallestRainfall(inchesOfRain, 12)<<" inches ";
// cout<<"in month "<<(monthPosition+1)<<endl;
cout << "The largest amount of rainfall was: " << largestRainfall(inchesOfRain, 12) << " inches ";
cout << "in month " << maxMonthPosition + 1 << endl;
#ifdef testing
std::cin.rdbuf(orig);
#endif
return 0;
}
double yearlyRainAverage(double inchesofrain[], const int months) {
double sum = 0;
for (int i = 0; i < months; i++) {
sum += inchesofrain[i];
}
return sum / months;
}
double smallestRainfall(double inchesofrain[], const int months) {
double smallest;
int i;
smallest = inchesofrain[0];
for (i = 0; i < months; i++) {
if (inchesofrain[i] < smallest) {
smallest = inchesofrain[i];
}
}
return smallest;
}
double largestRainfall(double inchesofrain[], const int months) {
double largest;
int i;
largest = inchesofrain[0];
for (i = 0; i < months; i++) {
if (inchesofrain[i] > largest) {
largest = inchesofrain[i];
}
}
return largest;
}
Your searchHighestMonth() function is almost good. But its loop must start at 0 not 1, like in the rest of your code. Loop is from 0 to 11 if it is a full year with 12 months. Besides, it is more readable if you store the current maximum rain value in some variable.
int searchHighestMonth(double inchesofrain[], int monthcount){
double maxrain = -1.0;
int max = -1;
for ( int i=0; i < monthcount; ++i) {
if ( maxrain < inchesofrain[i] ) {
maxrain = inchesofrain[i];
max = i;
}
}
return max;
}
Side remark: In actual production code, you would probably want to use std::vector objects, which maintain their own size, rather than plain old C-style arrays.
Side remark: if you want to give your user a second chance to enter a proper non-negative rainfall value, you must do it before including that value into the sum.

C++ - Passing an Array of Structure Type to a User Defined Function

I'm completely new to Stack OverFlow so excuse me if I'm breaking any rules or incorrectly asking a question (I would also appreciate advice on how to properly ask questions on Stack overflow).
Anyways so I get this error
*"argument of type 'data *' is incompatible with parameter of type 'data '."
when I call the user-defined function weather in int main(). I would appreciate it if someone could take a look at my code and guide me to what's causing the error. Just so you know, I am currently taking a programming fundamentals intro course and I have not learned about classes and pointers yet.
Thanks!
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 2;
void weather(struct data array[], int SIZE, string months[]);
int main()
{
string months[12] = { "January", "February", "March", "April", "May", "June"
"July", "August", "September", "October", "November", "December" };
struct data {
double totalRainfall;
double highTemp;
double lowTemp;
double avgTemp;
};
data annualWeather[SIZE];
//Asks user for total rainfall, high and low temperature, and calculates
//avg temperature of each month
for (int i = 0; i < SIZE; i++)
{
cout << "What was the total rainfall in " << months[i]
<< "?" << endl;
cin >> annualWeather[i].totalRainfall;
//Prompts user for temperature high
do
{
cout << "What was the temperature high in " << months[i]
<< "?" << endl;
cin >> annualWeather[i].highTemp;
} while (annualWeather[i].highTemp > 140);
//Prompts user for temperature low
do
{
cout << "What was the temperature low in " << months[i]
<< "?" << endl;
cin >> annualWeather[i].lowTemp;
} while (annualWeather[i].lowTemp < -100);
annualWeather[i].avgTemp = (annualWeather[i].highTemp + annualWeather[i].lowTemp) / 2;
}
**weather(annualWeather, SIZE, months); <--THIS IS WHERE I SEE ERROR**
return 0;
}
void weather(struct data array[], int SIZE, string months[])
{
double avgRainFall = 0;
double totalAnnualRainFall = 0;
double annualHigh = 0;
double annualLow = 0;
double avgMonthlyTemp = 0;
//Calculates sum annual rain fall, total annual rain fall, and total average monthly temperature
for (int i = 0; i < SIZE; i++)
{
avgRainFall += annualWeather[i].totalRainfall;
totalAnnualRainFall += annualWeather[i].totalRainfall;
avgMonthlyTemp += annualWeather[i].avgTemp;
}
//Calculates average annual rain fall and average annual monthly temperature
avgRainFall = avgRainFall / SIZE;
avgMonthlyTemp = avgMonthlyTemp / SIZE;
//Selection Sort of annualWeather[].highTemp
for (int index = 0; index < SIZE - 1; index++)
{
//Find location of smallest element
int smallestIndex = index;
for (int location = index + 1; location < SIZE; location++)
if (annualWeather[location].highTemp < annualWeather[smallestIndex].highTemp)
smallestIndex = location;
//Swap smallest element of annualWeather[].highTemp to front
int tempSales = annualWeather[smallestIndex].highTemp;
annualWeather[smallestIndex].highTemp = annualWeather[index].highTemp;
annualWeather[index].highTemp = tempSales;
//Swap smallest element of months[] to front
string tempMonths = months[smallestIndex];
months[smallestIndex] = months[index];
months[index] = tempMonths;
}
//Displays average monthly rainfall, total annual rainfall, annual high, annual low,
//And average of the monthly temperature averages
cout << "The average monthly rainfall is " << avgRainFall << "." << endl
<< "The total rainfall for the year is " << totalAnnualRainFall << "."
<< endl << "The high temperature of the year is " << annualHigh << " during "
<< months[0] << endl << "The low temperature of the year is " << annualLow << " during "
<< months[2] << endl << "The average temperature of all the monthly averages is "
<< avgMonthlyTemp << ".";
cin.get(); cin.get();
}
The struct data declared in your main function is different from the struct data used in the prototype of your weather function.
Move the definition of struct data out of main to the global namespace, somewhere before you use it to declare weather.
For the first please specified your personal data types in global space.
Your problem not in sending array to function. Problem inside your function
you send array to them with name array, and inside trying to access them by name from main. Just rename your function parameter.
void weather(Data array[], int SIZE, string months[])
{
double avgRainFall = 0;
double totalAnnualRainFall = 0;
double annualHigh = 0;
double annualLow = 0;
double avgMonthlyTemp = 0;
//Calculates sum annual rain fall, total annual rain fall, and total average monthly temperature
for (int i = 0; i < SIZE; i++)
{
avgRainFall += array[i].totalRainfall;
totalAnnualRainFall += array[i].totalRainfall;
avgMonthlyTemp += array[i].avgTemp;
}
//Calculates average annual rain fall and average annual monthly temperature
avgRainFall = avgRainFall / SIZE;
avgMonthlyTemp = avgMonthlyTemp / SIZE;
//Selection Sort of annualWeather[].highTemp
for (int index = 0; index < SIZE - 1; index++)
{
//Find location of smallest element
int smallestIndex = index;
for (int location = index + 1; location < SIZE; location++)
if (array[location].highTemp < array[smallestIndex].highTemp)
smallestIndex = location;
//Swap smallest element of annualWeather[].highTemp to front
int tempSales = array[smallestIndex].highTemp;
array[smallestIndex].highTemp = array[index].highTemp;
array[index].highTemp = tempSales;
//Swap smallest element of months[] to front
string tempMonths = months[smallestIndex];
months[smallestIndex] = months[index];
months[index] = tempMonths;
}
//Displays average monthly rainfall, total annual rainfall, annual high, annual low,
//And average of the monthly temperature averages
cout << "The average monthly rainfall is " << avgRainFall << "." << endl
<< "The total rainfall for the year is " << totalAnnualRainFall << "."
<< endl << "The high temperature of the year is " << annualHigh << " during "
<< months[0] << endl << "The low temperature of the year is " << annualLow << " during "
<< months[2] << endl << "The average temperature of all the monthly averages is "
<< avgMonthlyTemp << ".";
cin.get(); cin.get();
}

Connecting 2 arrays, a string array and a number array

So, I have an array called rainfall which has data of rainfall for 12 months stored in. And also a string array for the months (jan,feb...). And I will have to sort the rainfall array from highest to lowest. How do I connect this 2 arrays, so I can print them out together properly after sorting out the rainfall array.
This is my code :
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void selectionSort(double array[],int size)
{
int startScan, minIndex, maxValue;
for(startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
maxValue = array[startScan];
for(int index = startScan + 1; index < size; index++)
{
if (array[index] > maxValue)
{
maxValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = maxValue;
}
}
void highestfunction(double highestrainfall[], string monthtemp[])
{
double highest;
highest = highestrainfall[0];
string highestmonth = "January";
for (int count = 1; count < 12; count++)
{
if (highestrainfall[count] > highest)
{
highest = highestrainfall[count];
highestmonth = monthtemp[count];
}
}
cout << highestmonth << " has highest amount of rainfall: " << highest << " ml" << endl;
}
void lowestfunction(double lowestrainfall[], string monthtemp[])
{
double lowest;
lowest = lowestrainfall[0];
string lowestmonth = "January";
for (int count = 1; count < 12; count++)
{
if(lowestrainfall[count] < lowest)
{
lowest = lowestrainfall[count];
lowestmonth = monthtemp[count];
}
}
cout << lowestmonth << " has lowest amount of rainfall: " << lowest << " ml" << endl;
}
int main()
{
string months[] = { "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
double rainfall[12];
double total = 0;
double average;
int count;
for (count = 0; count < 12; count++)
{
cout << "Enter the total rainfall (in milliliter) of " << months[count] << ": ";
cin >> rainfall[count];
while(rainfall[count] < 0)
{
cout << "Invalid input!!! Please try again. " << endl;
cout << "Enter the total rainfall (in milliliter) of " << months[count] << ": ";
cin >> rainfall[count];
}
}
for (count = 0; count <12; count++)
{
total += rainfall[count];
}
cout << "----------------------------------------" << endl;
cout << setprecision(2) << showpoint << fixed;
cout << "The total rainfall for a year is: " << total << " ml" << endl;
average = (total / 12);
cout << "The average rainfall monthly rainfall is " << average << " ml" << endl;
highestfunction(rainfall, months);
lowestfunction(rainfall, months);
selectionSort(rainfall, 12);
cout << "List of the months, sorted in order of rainfall from highest to lowest" << endl;
for(count = 0; count < 12; count++)
{
cout << rainfall[count] <<" ml" << endl;
}
return 0;
}
As I understand, you want label each rainfall with a month.
Therefore you can define a struct. For example;
struct RainfallDefinition
{
string month;
double rainfall;
}
now you can use array of RainfallDefinition and sort with respect to rainfall attribute.
If you do not use structs, you can use pair. However, in my opinion using struct is more reliable and readable. You can extend your RainfallDefinition any time. For example, you can also add int year; , int day; attributes etc. and then you can sort without losing any info.
i could suggest a simple algo for it...
take two arrays one array of strings which contains the months...name it
`
string month[12];
/* i.e. 12 months initially give em the month names in regular order...i.e. starting from jan to dec...*/
second array would be a 2 dimensional array...
`
int rainfall[12][2];
// rainfall[i][j] likewise you know rows and columns respectively i m considering
`
put values in it initially like
`
rainfall[0][0]=150; // this is the amount of rainfall in litre or whatever unit
rainfall[0][1]=0;
/* here 2nd column contains the index of month in string, i.e. for now it is 0 that is January...likewise*/
now you sort the rainfall array for the values in rainfall[i][0] and where ever you swap two values for sorting... suppose you swap rainfall[1][0] to rainfall[3][0] then in the next statement in which you swap them...also swap their respective indices...
so in the end u would be having array rainfall in a sorted manner so when u want rainfall[i][0] 's month.. you can check it by month[rainfall[i][1]] ...
**This thing can also be done using pointers and linked list...even a little more efficiently but i did this because when i have choice between using linked list using pointers and without it... i prefer without it
**this could also be done using structure but here is the most organic way i found...

Variable initialization in functions

The variable days will not carry over throughout the function. I get an error saying that days isn't initialized in the highest and lowest functions and I have no idea how to fix it. Here's the code that I have so far.
#include <iostream>
using namespace std;
float temptotal = 0;
float averagetemp = 0;
float temperatures[50];
float average();
void highest();
void lowest();
int main()
{
average();
highest();
lowest();
}
float average()
{
float days = 0;
cout << "Enter the number of days: ";
cin >> days;
if (days > 50)
{
cout << "You may only enter temperatures for 50 days." << endl;
return 0;
}
for (int i = 1; i <= days; i++)
{
cout << "Enter the temperature for day number " << i << ": ";
cin >> temperatures[i];
temptotal += temperatures[i];
return temperatures[i];
}
averagetemp = temptotal / days;
cout << "The average temperature is: " << averagetemp << endl;
}
void highest()
{
float max = -9999999999999;
for (int i = 0; i < days; i++)
{
if (temperatures[i] > max)
max = temperatures[i];
cout << "The highest temperature is: " << max << endl;
}
}
void lowest()
{
float min = 9999999999999;
for (int i = 0; i < days; i++)
{
if (temperatures[i] < min)
min = temperatures[i];
cout << "The lowest temperature is: " << min << endl;
}
}
You need to pass days to highest() and lowest(). If it's a common parameter for all 3 functions, you could set its value in main(), and then pass it to them. And I think days should be an int from your usage of for loop, so:
int main()
{
int days = 0;
cout << "Enter the number of days: ";
cin >> days;
average(days);
highest(days);
lowest(days);
}
float average(int days)
{
... ...
}
void highest(int days)
{
... ...
}
void lowest(int days)
{
... ...
}