我的家庭作业存在问题而且我的教授没有回应。如何让我的数组返回getHighest
和getLowest
函数的月份字符串名称?
它一直告诉我必须添加更多细节,我从中移除了代码,所以我现在只剩下主要的getLowest
和getHighest
函数。它说我还需要更多细节,所以我输入这个以添加更多细节,以便我可以提交我的问题。
提前谢谢!
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//Function prototypes
void getRainfall(double[], int); //To retrieve the user input.
double getTotal(double[], int); //To total the rainfall amounts.
double getAverage(double[], int); //To get the average rainfall.
double getLowest(double[], int, int&); //Returns the lowest value, provides the index of the lowest value in the last parameter.
double getHighest(double[], int, int&); //Returns the highest value, provides the index of the highest value in the last parameter.
//Global Variable
const int NUM_MONTHS = 12;
//Array names
double rainfall[NUM_MONTHS];
string month[NUM_MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; //Month array to hold names of months
int main()
{
//Declare variables
double total, average;
int low, high;
string lowMonth, highMonth;
//Call Functions
getRainfall(rainfall, NUM_MONTHS); //To retrieve the user input.
total = getTotal(rainfall, NUM_MONTHS); //To total the rainfall amounts.
average = getAverage(getTotal, NUM_MONTHS); //To get the average rainfall.
lowMonth = getLowest(rainfall, NUM_MONTHS, low); //Returns the lowest value, provides the index of the lowest value in the last parameter.
highMonth = getHighest(rainfall, NUM_MONTHS, high); //Returns the highest value, provides the index of the highest value in the last parameter.
//Display the following:
cout << "The total rainfall for the year is: " << total << endl;
cout << "The average rainfall for the year is: " << fixed << showpoint << setprecision(2) << average << endl;
cout << "Least amount of rainfall fell in: " << highMonth << endl;
cout << "Most amount of rainfall fell in: " << lowMonth << endl;
return 0;
}
//*******************************************************************************************
// double getLowest(double amount[], int size) *
// Returns the lowest value, provides the index of the lowest value in the last parameter. *
//*******************************************************************************************
double getLowest(double amount[], int NUM_MONTHS, int &low)
{
low = amount[0]; //Variable to hold lowest value.
int lowMonth = 0; //Set low value to intial rainfall value. //Variable to return month element location.
for (int index = 0; index < NUM_MONTHS; index++)
{
if (amount[index] < low)
{
low = amount[index];
lowMonth = index;
}
}
return lowMonth;
}
//*********************************************************************************************
// double getHighest(double amount[], int size) *
// Returns the highest value, provides the index of the highest value in the last parameter. *
//*********************************************************************************************
double getHighest(double amount[], int NUM_MONTHS, int &highMonth)
{
//high = amount[0]; //Variable to hold highest value
months = amount[0];
double highMonth = 0; //Variable to hold highest value
for (int index = 0; index < NUM_MONTHS; index++)
{
if (amount[index] > high)
{
highMonth = amount[index];
months = index;
}
}
return highMonth;
}
答案 0 :(得分:0)
所以你的问题非常清楚,你的代码完全混乱,但我想你希望函数getHighest
也返回一个带有月份名称的字符串?
好吧,一个函数只能返回一种类型的变量,但你可以用引用来做:
double getHighest(double amount[], int NUM_MONTHS, int &highMonth, string &name)
{
//high = amount[0]; //Variable to hold highest value
months = amount[0];
double highMonth = 0; //Variable to hold highest value
for (int index = 0; index < NUM_MONTHS; index++)
{
if (amount[index] > high)
{
highMonth = amount[index];
months = index;
name = month[index];
}
}
return highMonth;
}
但通常你的代码有一些缺陷:例如覆盖
highMonth
可能会导致问题,为什么会传递NUM_MONTHS
,即使它是全局定义的?我并不完全明白为什么这段代码过于复杂。我还认为getHighest
和getLowest
功能甚至无法正常工作。作为一种更好的方法,我尝试这样的函数:
double getHighest(double amount[], string &name)
{
double highest = 0;
double highPos = 0;
for(unsigned int i = 0; i < NUM_MONTHS; i++)
{
if(highest <= amount[i])
{
highest = amount[i];
highPos = i;
}
}
name = month[highPos];
return highest;
}
编辑:或者,如果您只想返回字符串,可以这样做:
字符串和名称) { // high = amount [0]; //变量以保持最高值 月=金额[0]; double highMonth = 0; //变量保持最高值
for (int index = 0; index < NUM_MONTHS; index++)
{
if (amount[index] > high)
{
highMonth = amount[index];
months = index;
name = month[index];
}
}
return highMonth;
}
但通常你的代码有一些缺陷:例如覆盖
highMonth
可能会导致问题,为什么会传递NUM_MONTHS
,即使它是全局定义的?我并不完全明白为什么这段代码过于复杂。我还认为getHighest
和getLowest
功能甚至无法正常工作。作为一种更好的方法,我尝试这样的函数:
string getHighest(double amount[], string &name)
{
double highest = 0;
double highPos = 0;
for(unsigned int i = 0; i < NUM_MONTHS; i++)
{
if(highest <= amount[i])
{
highest = amount[i];
highPos = i;
}
}
name = month[highPos];
return name;
}