C++ 函数不返回数组中输入的最高值和最低值
此函数无法准确返回数组中输入的最高值和最低值。我不确定我为程序输入了什么代码来执行此操作。此程序需要返回输入数组的所有元素的平均值(平均部分工作正常),并在输入数组的所有值中查找最高值和最低值。请帮忙C++ 函数不返回数组中输入的最高值和最低值,c++,arrays,max,min,C++,Arrays,Max,Min,此函数无法准确返回数组中输入的最高值和最低值。我不确定我为程序输入了什么代码来执行此操作。此程序需要返回输入数组的所有元素的平均值(平均部分工作正常),并在输入数组的所有值中查找最高值和最低值。请帮忙 #include <iostream> using namespace std; float temptotal = 0; float averagetemp = 0; float temperatures[50]; float average(int); void highest(i
#include <iostream>
using namespace std;
float temptotal = 0;
float averagetemp = 0;
float temperatures[50];
float average(int);
void highest(int);
void lowest(int);
int main()
{
int 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;
}
average(days);
highest(days);
lowest(days);
}
float average(int days)
{
for (int i = 1; i <= days; i++)
{
cout << "Enter the temperature for day number " << i << ": ";
cin >> temperatures[i];
temptotal += temperatures[i];
}
averagetemp = temptotal / days;
cout << "The average temperature is: " << averagetemp << endl;
return averagetemp;
}
void highest(int days)
{
int count;
int highest;
highest = temperatures[50];
for (count = 1; count < days; count++)
{
if (temperatures[count] > highest)
highest = temperatures[count];
cout << "The highest temperature is: " << highest << endl;
}
}
void lowest(int days)
{
int count;
int lowest;
lowest = temperatures[50];
for (count = 1; count < days; count++)
{
if (temperatures[count] < lowest)
lowest = temperatures[count];
cout << "The lowest temperature is: " << lowest << endl;
}
}
#包括
使用名称空间std;
浮点数=0;
浮动平均温度=0;
浮子温度[50];
浮动平均(int);
无效最高值(int);
无效最低值(int);
int main()
{
整数天=0;
不能>天;
如果(天数>50天)
{
库特
数组索引以0开头
将cout语句置于for循环之外
数组索引从0开始,但循环从1开始。例如,对于超过天的循环,这将在整个范围内迭代:
for (int i = 0; i < days; i++)
for(int i=0;i
数组从0到n-1进行索引,其中n
是数组中条目的总数。循环中的计数从1开始,而应从0开始。如果用户输入50个值,您将拥有越界访问权限
更正应为:
for (int i = 0; i < days; i++)
cout << "Enter the temperature for day number " << i+1 << ": ";
此函数
float average(int days)
{
for (int i = 1; i <= days; i++)
{
cout << "Enter the temperature for day number " << i << ": ";
cin >> temperatures[i];
temptotal += temperatures[i];
//...
或
这是错误的
函数可以像函数定义那样编写
void highest( int days )
{
int highest = temperatures[0];
for ( int count = 1; count < days; count++ )
{
if ( highest < temperatures[count] ) highest = temperatures[count];
}
cout << "The highest temperature is: " << highest << endl;
}
函数“highest”的更好版本:
void highest(int days)
{
if (days < 1)
{
cout << "No temperatures" << endl;
}
double highest = temperatures[0];
for (int count = 1; count < days; count++)
{
if (temperatures[count] > highest)
{
highest = temperatures[count];
}
}
cout << "The highest temperature is: " << temperatures[highest_index] << endl;
}
void最高值(整数天)
{
如果(天数<1)
{
cout数组索引从0开始,而不是1。您可能希望从最高=温度[0];
开始。使用温度[50]
是未定义的。
float average(int days)
{
for (int i = 0; i < days; i++)
{
cout << "Enter the temperature for day number " << i + 1 << ": ";
cin >> temperatures[i];
temptotal += temperatures[i];
//...
highest = temperatures[50];
void highest( int days )
{
int highest = temperatures[0];
for ( int count = 1; count < days; count++ )
{
if ( highest < temperatures[count] ) highest = temperatures[count];
}
cout << "The highest temperature is: " << highest << endl;
}
#include <algorithm>
//...
void highest( int days )
{
cout << "The highest temperature is: "
<< *std::max_element( temperatures, temperatures + days )
<< endl;
}
void highest(int days)
{
if (days < 1)
{
cout << "No temperatures" << endl;
}
double highest = temperatures[0];
for (int count = 1; count < days; count++)
{
if (temperatures[count] > highest)
{
highest = temperatures[count];
}
}
cout << "The highest temperature is: " << temperatures[highest_index] << endl;
}