C ++程序查找数组中最小和最大的数字

时间:2016-11-13 06:12:06

标签: c++ arrays function

这里的C ++初学者和学习数组。下面的程序应该使用两个单独的函数返回数组中最小和最大的数字。一个用于最大,一个用于最小数量。但是,它一直为函数lastLowestIndex返回0,我不确定我可能做错了什么。

有人可以如此友善地建议并向我展示该功能中的错误以及可以采取哪些措施来纠正它以使其返回正确的值?我显然没有看到和/或理解什么是不正确的。

非常感谢你的帮助和提前的时间!!!

#include <iostream>
#include <cstdlib>

int lastLargestIndex(int [], int);
int lastLowestIndex(int [], int );

using namespace std;

int main()
{
   const int N = 15;
   int arr[N] = {5,198,76,9,4,2,15,8,21,34,99,3,6,13,61};
   int location;
   //int location2;

   location = lastLargestIndex( arr, N );

   cout << "The last largest number is:" << location << endl;

  location = lastLowestIndex(arr, N);

  cout << "The last smallest number is:" << location << endl;


  // std::system ("pause");

   return 0;
}

int lastLargestIndex( int arr[], int size )
{
   int highNum = 0;

   for( int i = 0; i < size; i++ )
   {
      if ( arr[i]  > highNum )
      {
         highNum = arr[i];
      }
   }

   return highNum;
}

int lastLowestIndex(int arr[], int size)
{

    int smallest = 0;

    for (int i = 0; i < size; i++)
    {
        if (arr[i] < smallest)
        {

            smallest = arr[i];

        }

    }

    //cout << smallest << '\n';

    return smallest;


}

3 个答案:

答案 0 :(得分:2)

  

然而,它一直为函数lastLowestIndex返回0,我不确定我可能做错了什么。

在函数smallest中初始化0lastLowestIndex()时出现逻辑错误 - 如果所有输入都if (arr[i] < smallest)条件未评估为true是积极的。相反,您应该将其初始化为数组arr的第一个成员。该函数应如下所示:

int lastLowestIndex(int arr[], int size)
{

    int smallest = arr[0];
    for (int i = 0; i < size; i++)
    {
        if (arr[i] < smallest)
        {
            smallest = arr[i];
        }
    }
    return smallest;
}

答案 1 :(得分:1)

lastLowestIndex()smallest初始化为0,然后将数组的所有元素(在您的示例中为正数)与它进行比较。所有正值都大于零,因此最小值将保持为零。

请注意,您的逻辑对于查找最大值也不常见。如果数组的所有元素都是负数,请考虑代码将执行的操作。

你最好采用一种不对数组做任何假设的逻辑,除了它的大小,它包含整数值。例如;

int lastLargestIndex( int arr[], int size )
{
    int highNum = arr[0];

    for( int i = 1; i < size; i++ )
    {
        if ( arr[i]  > highNum )
        {
            highNum = arr[i];
        }
    }
    return highNum;
}

这并没有表现出你所遇到的问题,因为它用数组的第一个元素初始化highNum,并迭代其余的元素(如果有的话)。这确实假设大小是正的。

您的函数也以误导的方式命名,因为它们(尝试)返回数组中的最大值(或最小值),但是它们的名称表明它们将返回该值的索引。我将解决这个小问题作为一种练习。

答案 2 :(得分:1)

这是正确的工作代码!

#include <iostream>
#include <cstdlib>

int lastLargestIndex(int [], int);
int lastLowestIndex(int [], int );

using namespace std;

int main()
{
   const int N = 15;
   int arr[N] = {5,198,76,9,4,2,15,8,21,34,99,3,6,13,61};
   int location;

   location = lastLargestIndex( arr, N );
   cout << "The last largest number is:" << location << endl;
   location = lastLowestIndex(arr, N);
   cout << "The last smallest number is:" << location << endl;
   // std::system ("pause");
   return 0;
 }

int lastLargestIndex( int arr[], const int size )
{
   int highNum = -100001;
   for( int i = 0; i < size; i++ )
   {
      if ( arr[i]  > highNum )
      {
         highNum = arr[i];
      }
   }
   return highNum;
}

int lastLowestIndex(int arr[], const int size)
{
    int smallest = 100001;
    for (int i = 0; i < size; i++)
    {
        if (arr[i] < smallest)
        {
            smallest = arr[i];
        }
    }
    //cout << smallest << '\n';
   return smallest;
}

完成修改:

  1. 将函数中的参数从int size替换为const int size,因为Nconst int函数中声明为main

  2. highNum

  3. 替换-100001
  4. smallest

  5. 替换100001
相关问题