Home > OS >  How can I return the Index of a row in a 2D array with a function?
How can I return the Index of a row in a 2D array with a function?

Time:07-20

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. It should also have four value-returning functions that compute and return to main the totalRainfall, averageRainfall, driestMonth, and wettestMonth. 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:

Here Is the code I have written I continue getting 0 returned I'm not sure what I'm doing wrong.

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

CodePudding user response:

In the function driestMonth:

  • You should initialize leastMonth, or the indeterminate uninitialized value may be returned.
  • You should increment colomn instead of rain in the inner loop.
  • You should compare rainfall[row][colomn] with leastRainMonth, not 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;
}   

CodePudding user response:

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]; but there are two things:

The structure double dbl_array[sizex][sizey] is an array of sizex pointers to arrays of sizey doubles. 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.

  •  Tags:  
  • c
  • Related