## zulkefli82

anybody can help me? How to culculate totalsale for each division..

I've tried many time but failed..
my coding is below

``````#ifndef DIVISIONSALE_H
#define DIVISIONSALE_H

class DivisionSale{
private:
double sales [3];
double totalSales;
public:
DivisionSale(double,double ,double ,double);
double getQuaterSale(int);
double getDivisionSale();
void culculateTotalSale(DivisionSale[], int);
};
#endif``````
``````#include "DivisionSale.h"
#include<iostream>
using namespace std;

DivisionSale::DivisionSale(double qtr1, double qtr2, double qtr3, double qtr4){
sales[0] = qtr1;
sales[1] = qtr2;
sales[2] = qtr3;
sales[3] = qtr4;
}
double DivisionSale::getQuaterSale(int y){
return sales[y];
}

double DivisionSale::getDivisionSale(){
}

void DivisionSale::culculateTotalSale(DivisionSale arr[], int size){
for(int i=0; i<size; i++){
totalSales += arr[i].sales[i];
}
}``````
``````#include<iostream>
#include "Division.cpp"
#include<iomanip>
using namespace std;

int main(){
const int NUM_DIVS = 6;
const int NUM_QTRS = 4;
DivisionSale div [NUM_DIVS]={DivisionSale(120.50, 250.65, 300.85, 2000.10),
DivisionSale(789.56, 2500.46, 500.15, 120.60),
DivisionSale(800.55, 950.15, 150.25, 400.22),
DivisionSale(350.29, 450.45, 750.15, 800.25),
DivisionSale(450.50, 500.15, 750.15, 800.25),
DivisionSale(400.65, 650.22, 780.10, 250.35)};
/*for(int i=0; i<NUM_DIVS; i++){
div[i].culculateTotalSale(div,NUM_DIVS);
}*/

cout<< fixed << showpoint << setprecision (2);
for(int i=0; i<NUM_DIVS; i++){
cout<<"Sales for division: "<<(i+1) <<endl;
for(int j=0; j<NUM_QTRS; j++){
cout<<"Quater "<<(j+1)<<" :RM "<< div[i].getQuaterSale(j)<<endl;
}
cout<<"Total Year Sales: "<<div[i].getDivisionSale()<<endl<<endl;
}

system("pause");
return 0;
}``````

## JasonHippy 724

For starters you have a problem in your DivisionSale class.
The size of the sales array needs to be 4 not 3. There are 4 quarters in a year, so you need to allocate space for 4 double values in your sales array:

``````class DivisionSale{
private:
double sales [3]; // <**** This should be 4 NOT 3!
...``````

As it stands: `sales[3]` will allocate space for 3 doubles that can be accessed by the indices 0,1 and 2. This is probably the main problem you're having! You're allocating space for 3 doubles in your array, but actually trying to use 4, which will overflow the array! A very definite definite no-no. So change that to 3 to a 4!

Also, in order to calculate the total sales for a single division, can't you do that in the constructor for the DivisionSale class?
e.g.

``````DivisionSale::DivisionSale(double qtr1, double qtr2, double qtr3, double qtr4)
{
sales[0] = qtr1;
sales[1] = qtr2;
sales[2] = qtr3;
sales[3] = qtr4;
totalSales = qtr1 + qtr2 + qtr3 + qtr4; // total yearly sales for this division
}``````

That way each instance of the class stores each quarters sales and calculates it's own total yearly sales when the class is constructed!

To get the total yearly sales for each individual division you can then use your DivisionSale::getDivisionSale() function.

So if you need to calculate the sum of the total yearly sales for of all of the divisions, you just need to loop through your array of divisions and add the value returned by each divisions getDivisionSale() function to the grand total. Simple!

## zulkefli82

but can you explain more about how to calculate the sum of the total yearly sales for of all of the divisions..

do you mean do like this? coding below..

``````double total_yearly_sales =0.0;
for(int i=0; i<NUM_DIVS; i++){
total_yearly_sales +=div[i].getDivisionSale();
}
cout<<"Total Year Sales for all of the divisions:RM ";
cout<<total_yearly_sales <<endl;``````

void DivisionSale::culculateTotalSale(DivisionSale arr[], int size)

I want to include this function into my coding...
any suggestion??

## Fbody 682

>>Also, in order to calculate the total sales for a single division, can't you do that in the constructor for the DivisionSale class?
I'm not too sure I agree with this suggestion. From a purely logical standpoint, that makes sense. From a class design standpoint it really doesn't though.

That value (as can be seen in the OP's code) would be another variable that you need to store which you would have to update every time you modify the contents of the class and unnecessarily creates more members that you need to keep track of. Additional variables make for additional complexity and add to the space requirements/size of the class/program.

It's a quick enough operation that it makes more sense to quickly calculate it when you need it. That way, you know that the value is current and hasn't been made obsolete in the mean time.

## Fbody 682

but can you explain more about how to calculate the sum of the total yearly sales for of all of the divisions..

do you mean do like this? coding below..

``````double total_yearly_sales =0.0;
for(int i=0; i<NUM_DIVS; i++){
total_yearly_sales +=div[i].getDivisionSale();
}
cout<<"Total Year Sales for all of the divisions:RM ";
cout<<total_yearly_sales <<endl;``````

void DivisionSale::culculateTotalSale(DivisionSale arr[], int size)

I want to include this function into my coding...
any suggestion??

You have the right idea here:

``````void DivisionSale::culculateTotalSale(DivisionSale arr[], int size){
for(int i=0; i<size; i++){
totalSales += arr[i].sales[i];
}
}``````

But you need to re-initialize totalSales to (0.0) before you start your calculation. Otherwise, you keep adding the current year's sales to the previous, etc... and don't get an accurate picture of the years sales.

It's funny how objects work when you put them into arrays. The operator precedence rules can play games with you. Put parentheses around the array and index, then use your member access operators. This will ensure that you actually have the object that you are trying to access the member(s) of:

``(myObjectArray[index]).member;``

## JasonHippy 724

>>Also, in order to calculate the total sales for a single division, can't you do that in the constructor for the DivisionSale class?
I'm not too sure I agree with this suggestion. From a purely logical standpoint, that makes sense. From a class design standpoint it really doesn't though.

That value would be another variable that you need to store which you would have to update every time you modify the contents of the class. Additional variables make for additional complexity and add to the space requirements/size of the class/program.

It's a quick enough operation that it makes more sense to quickly calculate it when you need it. That way, you know that the value is current and hasn't been made obsolete in the mean time.

This is very true, but from the code the OP posted, the class already had the totalSales member variable in the class (albeit unused). And the sales values are only set once and there are no get/set functions to get or alter the intitial sales figures, so it would make sense to do it at construction time and simply return the stored value from getDivisionSale, rather than having to alter the getDivisionSale function to loop through the sales figures and create a total.

However, if the OPs requirements do change and they do require this, then it's not going to be a biggie to make the change!

@ OP's last post....
Yes that's pretty much what I was getting at.
However, as Fbody has correctly pointed out, you could alter getDivisionSale to calculate the total sales by looping through the quarterly sales and return the sum, which would ensure that the value returned was up to date.

As for your calculateTotalSale function, which takes an array of DivisionSale objects. I'm not sure that would need to be a member function of the class...At least not a normal member function. Perhaps a static public member function! Personally I'd keep that out of the class myself. No need to pollute the class with something like this...But whatever floats your boat!

Oh hang on, I see what you're aiming at now!
So the totalSales member variable of your class is supposed to represent the total sales made by all Divisions, not the sales made by a single division?!
So instead of having it as a private member, you might want to consider removing it and then alter your calculateTotalSales to calculate and return the value.

So I guess if you want everything contained in your class, what you could do is:
1. Alter getDivisionSale to loop through the quarterly sales and return the sum
2. Remove totalSales from the class (you don't need it!)
3. Make calculateTotalSale a public static function which returns a double.
In the function you'll need to loop through the passed-in array, call each DivisionSale objects getDivisionSale() function and return the sum.
The value returned by the function will be the Total yearly sales for all divisions.

Again personally, instead of putting calculateTotalSales in the class, I'd put it elsewhere. But having it as a public static function would also work!
I hope that wasn't too confusing for you!

## Fbody 682

>>Again personally, instead of putting calculateTotalSales in the class, I'd put it elsewhere. But having it as a public static function would also work!
Hmmm.... I'd be interested to see how that would work because a static method doesn't get a this pointer and can only access other static members. You would have to pay extremely close attention to how you're accessing the individual objects.

## JasonHippy 724

From what I've seen of his original code:

``void DivisionSale::calculateTotalSale(DivisionSale arr[], int size)``

He's passing an array of DivisionSale objects into his function, so it looks like more of a general helper function to use with the class rather than a member function which acts on a specific instance of the class.
So if he really wants this to be in his class, he should declare it to be a public static function. Then in the implementation he can do something like this:

``````double DivisionSale::calculateTotalSales(DivisionSale arr[], int size)
{
double sum=0.0;
for(int i=0; i<size; ++i)
sum+=arr[i].getDivisionSale();

return sum;
}``````

So he's effectively looping through the passed-in array, calling each divisions getDivisionSale function (which calculates and returns the current divisions yearly sales) and add it to the sum which is then returned.

So using the static function in main he'd do this:

``double grand_total = DivisionSales::calculateTotalSales(div, NUM_DIVS);``

Where div is the array of objects he has set up and NUM_DIVS is the number of divisions in the array!

Again, I would keep this kind of thing outside of the class myself, but it's certainly valid!

## zulkefli82

Oh. .great!! you solved it!! thank you very much..