C++ 比较数组中的两个值并获取更大的下标数



>我正在尝试在将数组传递给函数后比较数组中的值,并返回具有最大值的元素的下标来表示一年中的某个月(例如:Jan = 1,Feb = 2等)。我已经摆弄了我的算法,但我似乎无法让它工作。我(认为)我知道我应该做什么。有人可以简单地向我解释一下吗?(编程初学者)。这是我下面的代码:

#include <iostream>
using namespace std;
//this program determines the most wet and dry months
//in a year and calculates the annual rainfall and monthly average
//function declarations
double calculateTotal(double [], int);
double calculateAvg(double, int);
int determineWetMonth(double [], int);
int determineDryMonth(double [], int);
int main()
{
    const int SIZE = 12;
    double months[SIZE];//array stores rainfall each month
    //initialize array
    for(int count = 0; count < SIZE; count++)
    {
        months[count] = 0;
        }
    //populate array
    for(int i = 0; i < SIZE; i++)
    {
        int rain = 0;
        cout << "Enter the rainfall for month: " << i+1 << endl;
        cin >> rain;
        months[i] = rain;
            }

    //call total function
    double total = 0.0;
    total = calculateTotal(months, SIZE);
    //call average function
    double avg = 0.0;
    avg = calculateAvg(total, SIZE);
    //call wet function
    int highest = 0;
    highest = determineWetMonth(months, SIZE);
    //call dry function
    int lowest = 0;
    lowest = determineDryMonth(months, SIZE);
    //display results
    cout << "The total annual rainfall is: " << total << endl;
    cout << "The average monthly rainfall is: " << avg << endl;
    cout << "The WETTEST month is: " << highest << endl;
    cout << "The DRYEST month is: " << lowest;
    cin.get();
    cin.get();
}
double calculateTotal(double anArray[], int size)
{
    double theTotal = 0.0;
    for(int j = 0; j < size; j++)
    {
        theTotal = theTotal + anArray[j];
        }
    return theTotal;
    }
double calculateAvg(double someTotal, int amount)
{
    double theAvg = 0.0;
    theAvg = someTotal / amount;
    return theAvg;
    }
int determineWetMonth(double theArray[], int num)
{
    int most = 0;
    for(int k = 0; k < num; k++)
    {
        if(theArray[k] > theArray[0])
        {
            most = k+1;
            }
        }
    return most;
    }   
int determineDryMonth(double ourArray[], int dec)
{
    int least = 0;
    for(int m = 0; m < dec; m++)
    {
        if(ourArray[m+1] < ourArray[m])
        {
            least = m+1;
            }
        }
    return least;
    }   

您需要使用最大/最小值的值

int determineWetMonth(double theArray[], int num)
{
    int most = 1;
    for(int k = 1; k < num; k++)
    {
        if(theArray[k] > theArray[most-1])
        {
            most = k+1;
        }
    }
    return most;
}   
int determineDryMonth(double ourArray[], int dec)
{
    int least = 1;
    for(int m = 1; m < dec; m++)
    {
        if(ourArray[m] < ourArray[least-1])
        {
            least = m+1;
        }
    }
    return least;
} 
使用

算法的C++解决方案:

#include <algorithm>
//...
int determineDryMonth(double* ourArray, int siz)
{
    return std::distance(ourArray, std::min_element(ourArray, ourArray + siz));
} 
int determineWetMonth(double* theArray, int num)
{
    return std::distance(theArray, std::max_element(theArray, theArray + num));
}

min_element返回指向区域中最小项的指针,distance 函数告诉您起点与返回的指针的距离。 从本质上讲,您将获得最小值的位置。

对于最潮湿的月份,唯一的变化是获取指向最大值的指针的函数是 std::max_element

最新更新