在数组

时间:2016-01-12 18:19:50

标签: visual-c++

我必须在随机数组中找到最大值和最小值...这是我的代码:

#include<iostream>
using namespace std;
void populateArray();
int findMin();
int findMax();
int computeTotal();
int arr[50];
int small, big;
void main()
{
    big = small = arr[0];
    populateArray();
    findMin();
    findMax();
    computeTotal();
    system("pause");
}
void populateArray()
{
    for (int i = 0; i < 48; i++)
    {
        arr[i] = rand() % 1000;
        cout << arr[i] << " ";
    }
    cout << endl;
}
int findMin()
{
    for (int i = 0; i < 48; i++)
    {
        if (arr[i] < small)
        {
            small = arr[i];
        }
        cout << "The smallest number is " << small << endl;
        return 0;
    }
}
int findMax()
{
    for (int i = 0; i < 48; i++)
    {
        if (arr[i] > big)
        {
            big = arr[i];
        }
    }
    cout << "The biggest number is " << big << endl;
    return 0;
}
int computeTotal()
{
    int sum = 0;
    sum = big + small;
    cout << "Total= " << sum << endl;
    return 0;
}

但问题是它没有显示数组中的最小值和最大值...它显示最小值0和最大值995,即使在数组中也没有包含它们。 任何帮助??

0 个答案:

没有答案