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

时间:2015-06-22 00:10:12

标签: 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;
    }   

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

使用算法的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