C++ 使用函数的第三个参数
有人能帮我弄清楚如何获取getlower/getHighest函数的第3个参数,以引用具有一年中月份的名称数组,并在调用该数组时显示月份的名称吗?这些函数应该能够给出与数组中最低/最高数量相对应的月份名称。我好像记不下来了。这是我在这段代码中最不需要的东西,我正在努力想办法解决它。任何帮助都将不胜感激。多谢各位C++ 使用函数的第三个参数,c++,arrays,string,function,parameters,C++,Arrays,String,Function,Parameters,有人能帮我弄清楚如何获取getlower/getHighest函数的第3个参数,以引用具有一年中月份的名称数组,并在调用该数组时显示月份的名称吗?这些函数应该能够给出与数组中最低/最高数量相对应的月份名称。我好像记不下来了。这是我在这段代码中最不需要的东西,我正在努力想办法解决它。任何帮助都将不胜感激。多谢各位 #include <iostream> #include <iomanip> #include <string> using namespace st
#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;
}
#包括
#包括
#包括
使用名称空间std;
//功能原型
double getTotal(double[],int);
双精度平均值(双精度[],整数);
double getlower(double[],int,int&);
double-getHighest(double[],int,int&);
int main()
{
const int months=12;
字符串名称[月份]={“一月”、“二月”、“三月”、“四月”,
“五月”、“六月”、“七月”、“八月”,
“九月”、“十月”、“十一月”、“十二月”};
双倍降雨量[月];
双倍总计、平均值、最大降雨量、最小降雨量;
int indexofLowest,indexofHighest;
//从使用中输入降雨量
std::cout我尝试了你的代码。你有一些bug。我想你以后需要更加努力。无论如何,这里有一个对现有代码进行最小更改的有效解决方案-
//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;
}
//函数getLowest的定义
双倍最低(双倍降雨量[],整数月,整数和指数流量)
{
整数计数;
双最低;
最低值=降雨量[0];
用于(计数=1;计数<个月;计数++)
{
if(降雨量[计数]<最低值)
{
最低=降雨量[计数];
indexofLowest=计数;
}
}
回报率最低;
}
//函数getHighest的定义
双倍最高(双倍降雨量[],整数个月,整数和指数最高)
{
整数计数;
双高;
最高=降雨量[0];
用于(计数=1;计数<个月;计数++)
{
如果(降雨量[计数]>最高)
{
最高=降雨量[计数];
indexofHighest=计数;
}
}
回报最高;
}
您必须在函数中设置indexofLowest
和indexofHighest
(并匹配其原型):
//函数getLowest的定义
双倍最低(双倍降雨量[],整数月,整数和指数流量)
整数计数;
双最低;
最低值=降雨量[0];
用于(计数=1;计数<个月;计数++)
{
if(降雨量[计数]<最低值){
最低=降雨量[计数];
indexofLowest=计数;
}
}
回报率最低;
}
//函数getHighest的定义
双倍最高(双倍降雨量[],整数个月,整数和指数最高)
{
整数计数;
双高;
最高=降雨量[0];
用于(计数=1;计数<个月;计数++)
{
如果(raining[count]>highest){//您的代码中有一个bug:…raining[0]
最高=降雨量[计数];
INDEXOFHEIGHT=计数;
}
}
回报最高;
}
您还可以使用函数输入数据(并可能添加一些错误检查):
//用于降雨量的输入
int readData(字符串monthNames[],双雨[],int n){
int i;
std::难道你的getLowest
和getHighest
函数实际上没有设置indexofLowest
/indexofHighest
…而且,它们的定义与原型不匹配。你可以通过使用for
循环输入降雨数据来简化你的程序;或者更好的方法是,将数据放入一个文件中,从文件。文件方法意味着你不必每次都手工输入数据。我很感谢大家的建议。就像@immibis所说的,我只是将函数中的indexoflowst/indexofHighest设置为计数,然后一切都被修复了。修复比我想象的简单多了。非常感谢大家的输入。我没有意识到修复有多简单“是的。”鲍勃·休伊没有意识到这是一个简单的解决方案。只需设置indexofHighest和indexoflowsest来计数。我得更好地寻找小东西。@Samboy786
//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;
}
//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;
}