tags:

views:

72

answers:

1
class FinalList {
    Location List[20];
    int MaxSize;
    int Size;


public:
    FinalList()
    {
        MaxSize = 20;
    }

    void RunIt();
    void Show();
    void Mean();
    void Menu();
};


void FinalList::Mean()
{
    int K;
    double Total;
    double Average;

    for(K=0 ; K < Size ; K++)
        Total += List[K].Value;

    Average = Total / Size ;
    cout << "Average: " << Average << endl;
}

void FinalList::Show()
{
    int Count;

    for(Count = 0; Count < Size ; Count++)
        List[Count].Display();

    cout << "Average: " << Average << endl;
}

I can calculate the average value from the array in Mean(). But how can I later access this value in Show()?

+4  A: 

You can either store it in an instance variable (like you're doing with MaxSize) or make Mean() return the average value instead of just printing it out.

If you make Mean() return the average, then you can call it in your Show() function like this:

cout << "Average: " << Mean() << endl;
Bill the Lizard
I'd take the `return` road.
LukeN
I got it to work both ways. Can I run the mean function with having to enter "Mean" into my command menu? For example can I get it to run the Mean() function when I run the "Show" command?
redcat
@redcat: Yes, see the edit to my answer.
Bill the Lizard
Or perhaps after I enter the values into the array in the input() function?
redcat
For some reason the Average value is not being calculated correctly. I am calling it in the Show() function.
redcat
@redcat: Probably because your `Size` variable is not being set anywhere.
Bill the Lizard