查找数组C ++中的最大数字

时间:2014-07-15 09:47:21

标签: c++ arrays function

所以这就是我要做的 - 编写一个包含50个值的数组的程序,获取数组中的最大数字并将其打印出来。我虽然打了一堵砖墙。我很确定我对函数中的返回感到非常困惑,例如为什么在findSmall和findBig函数的for循环中索​​引“未定义”?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int findSmallest(int array[], int size, int index)
{
    int index_of_smallest_value = index;
    for (int i = index + 1; i < size; i++)
    {
        if (array[i] < array[index_of_smallest_value])
        {
            index_of_smallest_value = i;
        }
    }
    return index_of_smallest_value;
}

int findBiggest(int array[], int size, int index)
{
    int index_of_biggest_value = index;
    for (int i = index + 1; i < size; i++)
    {
        if (array[i] > array[index_of_biggest_value])
        {
            index_of_biggest_value = i;
        }
    }
    return index_of_biggest_value;
}

int findSmall(int array[], int size)
{
    int index = 0;
    for (int i = 0; i < size; i++)
    {
        index = findSmallest(array, size, i);
        //cout << index << endl;
    }
    return index;
}

int findBig(int array[], int size)
{
    int index = 0;
    for (int i = 0; i < size; i++)
    {
        index = findBiggest(array, size, i);
        //cout << index << endl;
    }
    return index;
}

int main()
{
    int array[50];
    srand(time(NULL));

    for (int i = 0; i < 50; i++)
        array[i] = rand() % 100;

    cout << "The smallest digit is " << findSmall(array, 50) << endl;
    cout << "The biggest digit is " << findBig(array, 50);
    cin.get();
}

我编辑了上面的代码,但是我一直从findSmall和findBig函数返回49。

6 个答案:

答案 0 :(得分:3)

您可以使用C ++ / STL方式。这不仅适用于数组,也适用于其他STL容器。

#include <iterator>
#include <algorithm>

int minelem = *std::min_element(begin(array), end(array));
int maxelem = *std::max_element(begin(array), end(array));

此代码循环数组两次。出于性能原因,您可以考虑合并循环。或者在C ++ 11中你甚至可以

auto result = std::minmax_element(begin(array), end(array));

答案 1 :(得分:0)

这取自link,但问题略有改变。这是否符合您的要求?

#include <iostream>

using namespace std;

int main()
{
    int array[50] = {5,123,5,9,1,5,7,89.....};
    int temp = 0;

    for(int i=0;i<50;i++)
    {
        if(array[i]>temp)
        temp=i;
    }
    cout << "The biggest number is: " << temp << endl;
    return 0;
}

您显然必须更改代码以适合您的。

答案 2 :(得分:0)

对于单元素数组,唯一的元素是最大元素。对于任何更长的数组,最大的元素要么是最后一个子元素中最大的元素,要么是最后一个元素。所以这就是代码的样子:

int findBiggest(int array[], int size)
{
    int big = 0;
    for (int i = 1; i < size; i++)
        if (array[i] > array[big])
            big = i;

    return big;
}

这就是全部:

int index2 = findBiggest(array, 50);
cout << "\nThe biggest digit is " << array[index2] << " at position " << index2;

答案 3 :(得分:0)

这是一个版本,我还使用了std :: vector和迭代器。返回值的更好方法是例如一个自定义结构或者可能使用std :: tuple,所以你不必跟踪矢量索引。

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

int findBigOrSmall(bool bigOrSmall, int a[], int s){
int big=a[0];
int small=a[0];
for(int i=0; i<s; ++i){
    if(a[i]>big) big=a[i];
    if(a[i]<small) small=a[i];
}
if(bigOrSmall) return big;
else return small;
}
std::vector<int> findBigOrSmallVec(std::vector<int> a){
int big=a[0];
int bigIndex;
int smallIndex;
int small=a[0];

for(std::vector<int>::iterator it=a.begin();it!=a.end();++it){
    if(*it>big) {
        big=*it;
        bigIndex = std::distance(a.begin(),it);
    }
    if(*it<small) {
        small=*it;
        smallIndex = std::distance(a.begin(),it);
    }
}

std::vector<int> res;
res.push_back(bigIndex);
res.push_back(big);
res.push_back(smallIndex);
res.push_back(small);
return res;

}


int main()
{
int array[50];
srand(time(NULL));
std::vector<int> randVec;

for (int i = 0; i < 50; i++){
    int randInt= rand() % 100;
    array[i] =randInt;
    randVec.push_back(randInt);

}

for(int i=0;i<50;++i) cout<<i<<": "<<array[i]<<endl;
cout<<"\n\nBiggest val="<<findBigOrSmall(true,array,50)<<endl;
cout<<"Smallest val="<<findBigOrSmall(false,array,50)<<endl;

//Now more c++-like
std::vector<int> res =findBigOrSmallVec(randVec);

cout<<"Biggest value is at position "<<res[0]<< " and is "<<res[1]<<endl;
cout<<"Smallest value is at position "<<res[2]<< " and is "<<res[3]<<endl;

}

答案 4 :(得分:0)

无分支做同样的方法(可以在int / float / double上使用)

int findBiggest(int array[],int size){
  int hi = 0;
  for (int p = 0; p < size; p++) {

    int comp = (array[hi] < array[p]);

    int diff = (p - hi);

    // this is just to debug and see what is going on
    printf("\np = %d, array[hi] = %d, array[p] = %d; comp = %d, diff = %d", p, array[hi], array[p], comp, diff);

    hi += diff * comp;

}

printf("\nindex of the highest number (%d) is %f", size[hi], hi);

}

对于我们这些喜欢一个(少数)衬里的人来说,循环的版本是:

int hi = 0;
for(int p = 0; p < size; p++)
    hi += (array[hi] < array[p]) * (p - hi);

return array[hi];// the biggest number or...
return hi; // the index of the biggest number

答案 5 :(得分:0)

您可以遵循此答案。如果您在此处不清楚,则可以问/说我实际需要什么。而我的答案没有什么。

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int findSmallest(int * begin, int * end)
{
    int small = *begin;
    while(begin != end){
        if(small > (*begin)) small = *begin;
        begin++;
    }
    return small;
}

int findBiggest(int * begin, int * end)
{
    int big = *begin;
    while(begin != end){
        if(big < (*begin)) big = *begin;
        begin++;
    }
    return big;
}

int main()
{
    int array[50];
    srand(time(NULL));

    for (int i = 0; i < 50; i++)
        array[i] = rand() % 100;
    cout << "The smallest digit is " << findSmallest(array, array+50) << endl;
    cout << "The biggest digit is " << findBiggest(array, array+50) << endl;
    return 0;
}

要清楚地了解我的功能,可以遵循linkUse of array and a function to print first n elements in C [as like as sort(a+m, a+n)]