【问题标题】:Function not returning the highest and lowest values entered into the array函数不返回输入到数组中的最大值和最小值
【发布时间】:2015-01-18 12:41:36
【问题描述】:

此函数未准确返回输入到数组中的最高值和最低值。我不确定我为程序输入了什么代码来执行此操作。该程序需要返回输入到数组中的所有元素的平均值(平均值部分可以正常工作),并在输入到数组中的所有值中找到最高值和最低值。请帮忙!

#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;
    }
}

【问题讨论】:

  • 数组索引从 0 开始,而不是 1。
  • 你可能想从highest = temperatures[0];开始。使用 temperatures[50] 是未定义的。

标签: c++ arrays max min


【解决方案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];
//... 

已经错了,因为元素 temperatures[0] 不会被初始化。你必须写

float average(int days)
{
    for (int i = 1; i <= days; i++)
    {
        cout << "Enter the temperature for day number " << i << ": ";
        cin >> temperatures[i-1];
        temptotal += temperatures[i-1];
//... 

或者

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 和lowest 也是错误的。例如数组temperatures没有索引为50的元素。此外,用户可以输入小于50的天数。所以这个语句

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;
}

考虑到在标头&lt;algorithm&gt; 中声明的标准算法std::max_element、std::min_element、std::minmax_element 可以用来代替您的函数。

例如函数最高可以使用标准算法std::max_element定义如下

#include <algorithm>

//...

void highest( int days )
{

    cout << "The highest temperature is: " 
         << *std::max_element( temperatures, temperatures + days )
         << endl;
}

【讨论】:

    【解决方案2】:
    1. 数组索引以 0 开头
    2. 将 cout 语句放在 for 循环之外。

    【讨论】:

      【解决方案3】:

      数组的索引从 0 到 n-1,其中 n 是数组中的条目总数。当您应该从 0 开始时,您在循环中从 1 开始计数。如果用户输入了 50 个值,您将获得越界访问。

      更正应该是:

      for (int i = 0; i < days; i++)
      

      那么在你的输出语句中,应该是:

      cout << "Enter the temperature for day number " << i+1 << ": ";
      

      另一个问题是你的highest 函数。问题有几点:

      1. 您将局部变量 highest 声明为 int。它应该是 float 以匹配 temperature 数组中使用的类型。

      2. 您应该将最高设置为一个非常小的值,比您预期的要小。或者更好的是,将其设置为循环之前的第一个 temperature 条目,然后从第二个条目开始循环。

      3. 您将局部变量命名为highest,但您的函数名称也是highest。当其他人查看您的代码时,这会导致混淆。

      如果您对其他解决方案感兴趣,以下计算最高:

      #include <algorithm>
      //...
      void highest(int days)
      {
          float theHighest = *std::max_element(temperatures, temperatures + days);
          cout << "The highest temperature is: " << theHighest << endl;
      }
      

      【讨论】:

        【解决方案4】:

        函数'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;
        }
        

        C++ 中的所有数组都从索引 0 开始。在此实现中,通过使用第一个元素(索引 0)作为第一个最大值来考虑这一点。之后,循环只需处理其余部分,从索引 1 开始。

        变量“highest”必须来自 double 类型。否则,您可能会得到错误的结果,因为通常会向下舍入(double to int),因此最高值略低于实际最高值。下一个比较可能会被分配,即使它有点低。

        【讨论】:

          【解决方案5】:

          数组索引从 0 开始,但您的循环从 1 开始。例如,对于几天的循环,这将遍历整个范围:

          for (int i = 0; i < days; i++)
          

          【讨论】:

            猜你喜欢
            • 2016-05-24
            • 1970-01-01
            • 2021-06-06
            • 1970-01-01
            • 2018-07-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多