简体   繁体   English

如何使用函数返回二维数组中行的索引?

[英]How can I return the Index of a row in a 2D array with a function?

Design a modular program that analyzes a year's worth of rainfall data.设计一个模块化程序来分析一年的降雨数据。 In addition to main, the program should have a getData function that accepts the total rainfall for each of 12 months from the user and stores it in a 2-D array of type double.除了 main 之外,该程序还应该有一个getData函数,该函数从用户那里接受 12 个月中每个月的总降雨量,并将其存储在 double 类型的二维数组中。 It should also have four value-returning functions that compute and return to main the totalRainfall , averageRainfall , driestMonth , and wettestMonth .它还应该有四个返回值的函数,它们计算并返回 main 的totalRainfallaverageRainfalldriestMonthwettestMonth These last two functions return the number of the month with the lowest and highest rainfall amounts, not the amount of rain that fell those months.最后两个函数返回降雨量最低和最高的月份数,而不是那些月份的降雨量。 Notice that this month number can be used to obtain the amount of rain that fell those months.请注意,这个月数可用于获取那几个月的降雨量。 This information should be used either by main or by a displayReport function called by main to print a summary rainfall report similar to the following:此信息应由 main 或 main 调用的displayReport函数使用,以打印类似于以下内容的汇总降雨报告:

Here Is the code I have written I continue getting 0 returned I'm not sure what I'm doing wrong.这是我编写的代码,我继续返回 0 我不确定我做错了什么。

const int months = 12;
const int rain = 1;

void getData(double[months][rain], int, int);
double totalRainfall(double[months][rain], int, int);
double averageRain(double, int);
double driestMonth(double[months][rain], int, int);


int main()
{   

    double totalRain;
    double averageRainfall;
    int leastMonth;
    double rainfall[months][rain];
    
    getData(rainfall, months, rain);
    totalRain = totalRainfall(rainfall, months, rain);
    averageRainfall = averageRain(totalRain, months);
    leastMonth = driestMonth(rainfall, months, rain);
    
    cout << averageRainfall << endl;
    cout << leastMonth;
    
    
    
    
}

void getData(double rainfall[months][rain], int months, int rain)
{   
    double rainAmount;
    
    for(int row = 0; row < months; row++)
    {   
        for(int colomn = 0; colomn < rain; colomn++)
        {
            cout << "enter rain for month " << (row+1) << "--->";
            cin >> rainAmount;
            while(rainAmount <= 0)
            {
                cout << "rainfall must be greater then 0" << endl;
                cout << "enter rainfall ---> ";
                cin >> rainAmount;
            }
            rainfall[row][colomn] = rainAmount;
        }
        
    }
}

double totalRainfall(double rainfall[months][rain], int months, int rain)
{   
    double totalRain = 0;
    for (int colomn = 0; colomn < months; colomn++)
    {
        for(int row = 0; row < rain; row++)
        {
            totalRain += rainfall[colomn][row];
        }
    }
    return totalRain;
}

double averageRain(double totalRain, int months)
{   
    double averageRainfall;
    averageRainfall = totalRain / months;
    return averageRainfall;
    
    
}

double driestMonth(double rainfall[months][rain], int months, int rain)
{
    double leastRainMonth = rainfall[0][0];
    int leastMonth;
    
    for(int row = 0; row < months; row++)
    {
        for(int colomn = 0; colomn < rain; rain++)
        {
            if(rainfall[row][colomn] < leastMonth)
            {
                leastRainMonth = rainfall[row][colomn];
                leastMonth = row;
            }   
            
        }   
            
    }
    
    return leastMonth;
}   

In the function driestMonth :在函数driestMonth

  • You should initialize leastMonth , or the indeterminate uninitialized value may be returned.您应该初始化leastMonth ,否则可能会返回不确定的未初始化值。
  • You should increment colomn instead of rain in the inner loop.您应该在内部循环中增加colomn而不是rain
  • You should compare rainfall[row][colomn] with leastRainMonth , not leastMonth .您应该将rainfall[row][colomn]leastRainMonth进行比较,而不是leastMonth

Fixed code:固定代码:

double driestMonth(double rainfall[months][rain], int months, int rain)
{
    double leastRainMonth = rainfall[0][0];
    int leastMonth = 0; // initialize this
    
    for(int row = 0; row < months; row++)
    {
        for(int colomn = 0; colomn < rain; colomn++) // increment proper thing
        {
            if(rainfall[row][colomn] < leastRainMonth) // compare with proper thing
            {
                leastRainMonth = rainfall[row][colomn];
                leastMonth = row;
            }   
            
        }   
            
    }
    
    return leastMonth;
}   

it looks like this is an assignment that you need to find yourself, so I'm not going to give you code.看起来这是一个你需要自己找到的任务,所以我不会给你代码。 you can declare an dbl_array[sizex][sizey];你可以声明一个dbl_array[sizex][sizey]; but there are two things:但有两件事:

The structure double dbl_array[sizex][sizey] is an array of sizex pointers to arrays of sizey doubles.结构double dbl_array[sizex][sizey]是一个sizex指针数组,指向sizey双精度数组。 So your data structure may not be the 2D array you need to produce.所以你的数据结构可能不是你需要生成的二维数组。 My guess is that would be something like this .我的猜测是会是这样的。

The other thing is the difference between passing variables to functions by value vs. by reference.另一件事是通过值通过引用将变量传递给函数之间的区别。 You are passing your functions by value, so changes made to the input array are lost when your function ends.您正在按值传递函数,因此在函数结束时对输入数组所做的更改会丢失。

That is a pretty important concept that you probably want to understand first, see here .这是一个非常重要的概念,您可能想先了解它,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM