在C ++中的函数中查找数组的下标

时间:2019-03-30 22:56:40

标签: c++ c++14

我有这份作业,到目前为止,我已经完成了。我被困在那里... 基本上,我需要获得最大的降雨量并显示(我已经完成了),还需要显示月份数。

这是我头疼的地方... 你们可以帮我一些代码吗?

#include <iostream>
#include <iomanip>
using namespace std;

double yearlyRainAverage(double[], const int);
double smallestRainfall(double [], const int);
double largestRainfall(double [], const int);
int searchHighestMonth(double[], int);

int main() {

    const int months = 12;
    double inchesOfRain[months];
    double sumOfAllMonths=0;


    int maxMonthPosition = searchHighestMonth(inchesOfRain, months);

    for (int count = 0; count < months; count++)
    {
        cout<<"Enter the rainfall (in inches) for month #"<< count + 1<<":  ";
        cin>>inchesOfRain[count];

        sumOfAllMonths += inchesOfRain[count];


        if(inchesOfRain[count] < 0){

            cout <<"Rainfall must be 0 or more.\n";
            cout<<"please re-enter: "<<endl;
            cout<<"Enter the rainfall (in inches) for month #"<< count + 1<<":  ";
            cin>>inchesOfRain[count];

        }

    }
    cout << fixed << showpoint << setprecision(2) << endl;

    cout<<"the total rainfall for the year is "<<sumOfAllMonths<<" inches"<<endl;
    cout<<"the average is "<<yearlyRainAverage(inchesOfRain, 12)<<" inches"<<endl;

//    cout<<"The smallest amount of rainfall was: "<<smallestRainfall(inchesOfRain, 12)<<" inches ";
//    cout<<"in month "<<(monthPosition+1)<<endl;

    cout<<"The largest amount of rainfall was: "<<largestRainfall(inchesOfRain, 12)<<" inches ";
    cout<<"in month "<<maxMonthPosition+1<<endl;

    return 0;
}

double yearlyRainAverage(double inchesofrain[], const int months){
    double sum=0;
    for(int i=0;i<months; i++){
        sum+=inchesofrain[i];
    }
    return sum/months;
}

double smallestRainfall(double inchesofrain[], const int months){
    double smallest;
    int i;

    smallest=inchesofrain[0];
    for(i=0; i < months; i++){
        if(inchesofrain[i] < smallest){
            smallest = inchesofrain[i];
        }
    }
    return smallest;

}


double largestRainfall(double inchesofrain[], const int months){
    double largest;
    int i;

    largest=inchesofrain[0];
    for(i=0; i < months; i++){
        if(inchesofrain[i] > largest){
            largest = inchesofrain[i];
        }
    }
    return largest;
}

我认为这是问题所在。我认为我的逻辑是错误的。但是,我不确定。

int searchHighestMonth(double inchesofrain[], int value){

    int max = 0;
    for ( int i=1; i < value; ++i) {
        if ( inchesofrain[max] < inchesofrain[i] ) {
            max = i;
        }
    }
    return max;
}

2 个答案:

答案 0 :(得分:1)

问题是您在从用户那里输入降雨量之前正在搜索最大降雨量。

移动此行:

    int maxMonthPosition = searchHighestMonth(inchesOfRain, months);

在输入for循环之后。

我继续并再次测试了所有代码,从输入字符串重定向stdin(cin),这对测试非常有帮助,因此不必继续输入。这是我的代码:

#include <iostream>
#include <sstream>
#include <iomanip>

using namespace std;

double yearlyRainAverage(double[], const int);
double smallestRainfall(double [], const int);
double largestRainfall(double [], const int);

int searchHighestMonth(double inchesofrain[], int value) {

    int max = 0;
    for (int i = 1; i < value; ++i) {
        if (inchesofrain[max] < inchesofrain[i]) {
            max = i;
        }
    }
    return max;
}

int main() {
//#define testing  // comment this line out to use std::cin for input
#ifdef testing
    // code to get stdin input from a local buffer
    std::string input_string{"4 5 6 7 8 9 10 3 2 3 4 5"};
    std::streambuf *orig = std::cin.rdbuf();
    std::istringstream input(input_string);
    std::cin.rdbuf(input.rdbuf());

#endif

    const int months = 12;
    double inchesOfRain[months];
    double sumOfAllMonths = 0;

    for (int count = 0; count < months; count++) {
        cout << "Enter the rainfall (in inches) for month #" << count + 1 << ":  " << std::endl;
        cin >> inchesOfRain[count];

        sumOfAllMonths += inchesOfRain[count];

        while (inchesOfRain[count] < 0) {
            cout << "Rainfall must be 0 or more.\n";
            cout << "please re-enter: " << endl;
            cout << "Enter the rainfall (in inches) for month #" << count + 1 << ":  " << std::endl;
            cin >> inchesOfRain[count];
        }
    }

    int maxMonthPosition = searchHighestMonth(inchesOfRain, months);

    cout << fixed << showpoint << setprecision(2) << endl;

    cout << "the total rainfall for the year is " << sumOfAllMonths << " inches" << endl;
    cout << "the average is " << yearlyRainAverage(inchesOfRain, months) << " inches" << endl;

//    cout<<"The smallest amount of rainfall was: "<<smallestRainfall(inchesOfRain, 12)<<" inches ";
//    cout<<"in month "<<(monthPosition+1)<<endl;

    cout << "The largest amount of rainfall was: " << largestRainfall(inchesOfRain, 12) << " inches ";
    cout << "in month " << maxMonthPosition + 1 << endl;

#ifdef testing
    std::cin.rdbuf(orig);
#endif

    return 0;
}

double yearlyRainAverage(double inchesofrain[], const int months) {
    double sum = 0;
    for (int i = 0; i < months; i++) {
        sum += inchesofrain[i];
    }
    return sum / months;
}

double smallestRainfall(double inchesofrain[], const int months) {
    double smallest;
    int i;

    smallest = inchesofrain[0];
    for (i = 0; i < months; i++) {
        if (inchesofrain[i] < smallest) {
            smallest = inchesofrain[i];
        }
    }
    return smallest;
}

double largestRainfall(double inchesofrain[], const int months) {
    double largest;
    int i;

    largest = inchesofrain[0];
    for (i = 0; i < months; i++) {
        if (inchesofrain[i] > largest) {
            largest = inchesofrain[i];
        }
    }
    return largest;
}

答案 1 :(得分:0)

您的searchHighestMonth()函数几乎不错。但是它的循环必须从0而不是1开始,就像其余的代码一样。如果是12个月的全年,则循环从0到11。此外,如果将当前的最大降雨值存储在某个变量中,则更具可读性。

int searchHighestMonth(double inchesofrain[], int monthcount){

    double maxrain = -1.0;
    int max = -1;

    for ( int i=0; i < monthcount; ++i) {
        if ( maxrain < inchesofrain[i] ) {
            maxrain = inchesofrain[i];
            max = i;
        }
    }
    return max;
}

旁注:在实际的生产代码中,您可能希望使用std :: vector对象,它们保持自己的大小,而不是普通的C样式数组。

旁注:如果您想让用户有第二次机会输入正确的非负降雨值,则必须之前输入该值,总和。