0

So I have trouble calling data from main function (an array with variables you input) and wasn't sure how to pass it to a float getTotal function. Here is my code:

#include <iostream>
#include <iomanip>

using namespace std;

float getTotal(float [], int )
   {
     double total = 0;
     for (int i=1; i<ARRAYSIZE; i++)
        {
           total += inputs[i];
        }
     cout << "The total rainfall for the year is " << total << "inches." << endl;
     return total;
   }

float getAverage(float [], int)
   {
     //code goes here
   }

int main()
   {
     const int ARRAYSIZE = 13;
     int inputs[ARRAYSIZE], i=1;
     do
        {
           cout << "Enter the rainfall (in inches) for month #" << i << ": ";
           cin >> inputs[i];
           if ( inputs[i] < 0 )
              {
                  cout << "Please enter a non-negative number for rainfall in month " << i << " :";
                  cin >> inputs[i];
              }
           i++;
        }
     while (i < 13);

     float getTotal(float inputs[], int ARRAYSIZE);
     float getAverage(float inputs[], int ARRAYSIZE);
   }

So I want to call the array data from main and calculate the total in the getTotal section. I've tried various ways, none of which worked.

Imran Chowdhry
  • 35
  • 1
  • 1
  • 8
  • Which are you having trouble with, populating an array in `main`, or passing an array to a function? – Beta Oct 21 '13 at 05:16
  • 2
    If possible, use an `std::vector` instead of an array. Right now, you're trying to use `ARRAYSIZE` in `getTotal`, but it's local to `main`, so it's not visible in `getTotal`. You also want to give names to the parameters so you can access them inside `getTotal`. – Jerry Coffin Oct 21 '13 at 05:17
  • @ Above I have trouble passing the array to a function, so I can calculate the total in the getTotal function @Jeffry, can you give me an example please? – Imran Chowdhry Oct 21 '13 at 05:24

4 Answers4

0
float getTotal(float inputs[], int ARRAYSIZE)
   {
     double total = 0;
     for (int i=1; i<ARRAYSIZE; i++)
        {
           total += inputs[i];
        }
     cout << "The total rainfall for the year is " << total << "inches." << endl;
     return total;
   }

float getAverage(float inputs[], int ARRAYSIZE)
   {
     //code goes here
   }

int main()
   {
     const int ARRAYSIZE = 13;
     int inputs[ARRAYSIZE], i=1;
     do
        {
           cout << "Enter the rainfall (in inches) for month #" << i << ": ";
           cin >> inputs[i];
           if ( inputs[i] < 0 )
              {
                  cout << "Please enter a non-negative number for rainfall in month " << i << " :";
                  cin >> inputs[i];
              }
           i++;
        }
     while (i < 13);

     float fTotal = getTotal(inputs, ARRAYSIZE);
     float fAverage = getAverage(inputs, ARRAYSIZE);
   }
t_smith
  • 89
  • 1
  • 1
  • 7
  • Here is my new and updated code, will paste the errors I get also. I'm unsure why this happens? Code: http://puu.sh/4VqJv.png The errors I get are found on this puu.sh link. http://puu.sh/4VqHi.png – Imran Chowdhry Oct 21 '13 at 05:35
  • EDIT: Errors are here, updated and fixed a small mistake. http://puu.sh/4VqNI.png – Imran Chowdhry Oct 21 '13 at 05:36
  • You didn't declare i in the function getTotal. Your errors are because you don't understand scope. Here's a quick [reference](http://msdn.microsoft.com/en-us/library/b7kfh662.aspx). A general rule is that a variable is only visible inside { } block, so other functions can't use any variable that is declared in main function block. – t_smith Oct 21 '13 at 06:23
0

Replace your function declarations:

float getTotal(float [], int )
float getAverage(float [], int)

to this

float getTotal(int* inputs, int ARRAYSIZE)
float getAverage(int* inputs, int ARRAYSIZE)

And this line inside main() function:

float getTotal(float inputs[], int ARRAYSIZE);
float getAverage(float inputs[], int ARRAYSIZE);

to this

float getTotal(inputs, ARRAYSIZE);
float getAverage(inputs, ARRAYSIZE);
Ismail Faruqi
  • 482
  • 4
  • 16
  • I did, got an error so I changed the code with this float fTotal = getTotal(inputs, ARRAYSIZE); float fAverage = getAverage(inputs, ARRAYSIZE); getting a "cannot convert parameter 1 from int[13] to float[] error now. – Imran Chowdhry Oct 21 '13 at 05:38
  • Sorry, I have edited my answer, let me know if the compiler still spits error. I am currently without a compiler :-( – Ismail Faruqi Oct 21 '13 at 05:58
  • no problem!! I appreciate the help!! It seems to have work, but I have to fix previous errors before confirming. Error I get now is error C2078: too many initializers Only error left this is on line float getTotal(inputs, ARRAYSIZE); – Imran Chowdhry Oct 21 '13 at 06:18
  • Replace the floats in function declaration with int; I have (again) updated my answer above :-) – Ismail Faruqi Oct 21 '13 at 06:36
0

There is so many errors in our source code. example " int inputs[ARRAYSIZE] " . Your array is int data type but you are passing this array as float data type. there is a compiler
error. Second, you should declare function and its data type outside the main function but
you are doing inside the main function float getTotal(float inputs[], int ARRAYSIZE); there is another compiler error. While calling function you don't need to specify the data type of argument . So there is a possible code I have written , Hope it will help you.

#include <iostream>
#include <iomanip>

using namespace std;

float getTotal(int *inputs, int ARRAYSIZE )
  {
   double total = 0;
     for (int i=1; i<=ARRAYSIZE; i++)
      {
        total += inputs[i];
      }
      cout << "The total rainfall for the year is " << total << "inches." << endl;
  return total;

}

 float getAverage(int *inputs, int ARRAYSIZE)
    {
     //code goes here

    return 1.4;
   }

 int main()
    {
     const int ARRAYSIZE = 3;
     int inputs[ARRAYSIZE], i=1;
     do
       {
       cout << "Enter the rainfall (in inches) for month #" << i << ": ";
       cin >> inputs[i];
       if ( inputs[i] < 0 )
          {
             cout << "Please enter a non-negative number for rainfall in month " << i     
                 << " :";
              cin >> inputs[i];
          }
       i++;
    }
 while (i <= 3);

     getTotal(inputs, ARRAYSIZE);
     getAverage(inputs, ARRAYSIZE);

return 0;

}

     While calling function, the return value you can store in variable but data type of   

      variable should be the same of return type of the function 

      e.g  float a = getTotal(inputs, ARRAYSIZE);
     float b = getAverage(inputs, ARRAYSIZE);
      or you can directly call this function in cout output command statement.like 

cout<< getTotal(inputs, ARRAYSIZE);

Birendra Kumar
  • 431
  • 1
  • 7
  • 18
  • hmm, I have everything working except converting parameters 1 from int[13] to float[]. any help there? – Imran Chowdhry Oct 21 '13 at 07:43
  • I think you need to see this link.Hope it will be useful for you [link](http://stackoverflow.com/questions/12344400/convert-int-to-float-in-c-no-casting) – Birendra Kumar Oct 21 '13 at 12:04
0

hi @user2901840 a slight modification in the code allows it to run fine:

#include <iostream>
#include <iomanip>

using namespace std;

float getTotal(float floatarray[], int size)
{
    float total = 0.0f;
    for (int i=0; i<size; i++)
    {
        total += floatarray[i];
    }
    return total;
}

float getAverage(float floatarray[], int size)
   {
        return (getTotal(floatarray, size))/size;
   }

int main()
   {
        int ARRAYSIZE = 12, i=0;
        float inputs[ARRAYSIZE];
        do
        {
            cout << "Enter the rainfall (in inches) for month #" << i << ": ";
            cin >> inputs[i];
            if ( inputs[i] < 0.0 )
                {
                    cout << "Please enter a non-negative number for rainfall in month " << i << " :";
                    cin >> inputs[i];
                }
            i++;
        }
        while (i < ARRAYSIZE);
        cout << "\n\nThe total rainfall for the year is " << getTotal(inputs, ARRAYSIZE) << " inches." << endl;
        cout << "The average rainfall per month during the year was: " << getAverage(inputs, ARRAYSIZE) << " inches."<< endl;
   }

You needed to predefine the classes correctly, and the calls needed to be modified. To clarify the code I have: - renumbered the i value to match common C++ conventions (running 0 to 11 for 12 values) - re-utilised functions to minimise code - re-declared the array as an array of floats (you initially had it as an array of ints)

Hope this helps:)

Let me know if you need more detail or explanation and I can fill in more information.

GMasucci
  • 2,834
  • 22
  • 42