helixkod 11

Heres the part of the code That i have:

``````#include <iostream>
using namespace std;

const int NUM_DIV = 6;			//Number of divisions
const int NUM_QTRS = 4;			//Number of quarters

void divisionSales(double [][NUM_QTRS], int);
void divisionChange(double [][NUM_QTRS], int);
void quarterSales(double [][NUM_QTRS], int);
void quarterChange(double [][NUM_QTRS], int);

int main()
{
double sales[NUM_DIV][NUM_QTRS];	//Array for the sales.
int div, qtrs;						//loop counters

//Loop to get sales values
for (div = 0; div < NUM_DIV; div++)
{
for (qtrs = 0; qtrs < NUM_QTRS; qtrs++)
{
cout << "Division " << (div + 1);
cout << ", Quarter " << (qtrs + 1) << ": \$";
cin >> sales[div][qtrs];
while (sales[div][qtrs] < 0)
{
cout << "ERROR: Please enter a possitive number for sales figures.";
cin >> sales[div][qtrs];
}
}
cout << endl;		//print blank line
}

divisionSales(sales, NUM_DIV);
divisionChange(sales, NUM_DIV);
quarterSales(sales, NUM_DIV);

return 0;
}

//loop to print the sales numbers by division
void divisionSales(double sales[][NUM_QTRS], int div)
{
double divSales = 0;
for (int x = 0; x < div; x++)
{
for (int y = 0; y < NUM_QTRS; y++)
divSales += sales[x][y];
cout << "Division " << (x + 1) << " Total Sales: \$"
<< divSales << endl;
divSales = 0;
}
cout << endl;
}

void divisionChange(double sales[][NUM_QTRS], int div)
{
for (int x = 0; x < div; x++)
{
for (int y = 1; y < NUM_QTRS; y++)
{
cout << "For division " << (x+1) << " there was a "
<< (sales[x][y+1] - sales[x][y]) << " difference between "
<< "quarters " << (y +1) << " and " << y << endl;
}
cout <<endl;
}

}``````

here is the outcome of my work:
Division 1, Quarter 1: \$14
Division 1, Quarter 2: \$52
Division 1, Quarter 3: \$41
Division 1, Quarter 4: \$52

Division 2, Quarter 1: \$41
Division 2, Quarter 2: \$52
Division 2, Quarter 3: \$41
Division 2, Quarter 4: \$52

Division 3, Quarter 1: \$41
Division 3, Quarter 2: \$52
Division 3, Quarter 3: \$41
Division 3, Quarter 4: \$52

Division 4, Quarter 1: \$41
Division 4, Quarter 2: \$52
Division 4, Quarter 3: \$41
Division 4, Quarter 4: \$52

Division 5, Quarter 1: \$41
Division 5, Quarter 2: \$52
Division 5, Quarter 3: \$41
Division 5, Quarter 4: \$52

Division 6, Quarter 1: \$41
Division 6, Quarter 2: \$52
Division 6, Quarter 3: \$41
Division 6, Quarter 4: \$52

Division 1 Total Sales: \$159
Division 2 Total Sales: \$186
Division 3 Total Sales: \$186
Division 4 Total Sales: \$186
Division 5 Total Sales: \$186
Division 6 Total Sales: \$186

For division 1 there was a -11 difference between quarters 2 and 1
For division 1 there was a 11 difference between quarters 3 and 2
For division 1 there was a -11 difference between quarters 4 and 3

For division 2 there was a -11 difference between quarters 2 and 1
For division 2 there was a 11 difference between quarters 3 and 2
For division 2 there was a -11 difference between quarters 4 and 3

For division 3 there was a -11 difference between quarters 2 and 1
For division 3 there was a 11 difference between quarters 3 and 2
For division 3 there was a -11 difference between quarters 4 and 3

For division 4 there was a -11 difference between quarters 2 and 1
For division 4 there was a 11 difference between quarters 3 and 2
For division 4 there was a -11 difference between quarters 4 and 3

For division 5 there was a -11 difference between quarters 2 and 1
For division 5 there was a 11 difference between quarters 3 and 2
For division 5 there was a -11 difference between quarters 4 and 3

For division 6 there was a -11 difference between quarters 2 and 1
For division 6 there was a 11 difference between quarters 3 and 2
For division 6 there was a -52 difference between quarters 4 and 3

The bold is what i don't understand why i'm getting that answer. Also i know between Quarter 2 and 1 there is not a -11 difference. I know its as simple as changing one number or something, i just can't see it. Can someone provide me with another set of eyes?

Lerner 582

it's at least an off by one error in divisionChange. When y goes from 1 to 3 then y + 1 goes from 2 to 4 but 4 as an index for quarters isn't legal. The legal indexes for quarters are 0 to 3.

helixkod 11

I changed two things it went from y+1 and y to y and y-1 with the counter set to 1 and now it works, thanks here the code

``````for (int y = 1; y < NUM_QTRS; y++)
{
cout << "For division " << (x+1) << " there was a "
<< (sales[x][y] - sales[x][y-1]) << " difference between "
<< "quarters " << (y +1) << " and " << y << endl;``````

helixkod 11

Thanks for the help again, Here another part which i don't understand:

``````double qtrsSales[NUM_QTRS];

for (int x = 0; x < div; x++)
{
for (int y = 0; y < NUM_QTRS; y++)
}

for (int y = 0; y < NUM_QTRS; y++)
cout << "Total sales for quarter " << (y+1)<< ": \$"
cout << endl;``````

I'm suppose to be have an array that takes in 4 quarters fo 6 divisions and add all the quaters up. im using relatively low numbers to test this out but i keep getting this:
Total sales for quarter 1: \$-9.25596e+061
Total sales for quarter 2: \$-9.25596e+061
Total sales for quarter 3: \$-9.25596e+061
Total sales for quarter 4: \$-9.25596e+061

Lerner 582

Problem 1:
The reason you get the funny number is that each element of qtrsSales, qtrsSales, is an object of type double. If you try to add something to a double without initializing it you're going to funny numbers. So initialize all the elements of qtrsSales to some value before you use them.

Problem 2:
In your earlier code you held the divisions value constant while you changed the qaurter you wanted to enter data for by using a nested loop where the outer loop remained constant while the inner loop changed. And it worked. Now you want to keep the quarter constant and change the cycle through the divisions so you add all 6 division sales together for a given quarter to derive a total sales value for each quarter. Hopefully you recognize some similarity between the previous, successfully completed, task and can figure out what to do on your own. You've almost got it as it is.

helixkod 11

lol i know im close =p

<The reason you get the funny number is that each element of qtrsSales, qtrsSales, is an object of type double. If you try to add something to a double without initializing it you're going to funny numbers. So initialize all the elements of qtrsSales to some value before you use them.

as for that let me take a swing with that in mind and ill see what i get.

helixkod 11

Woot:

That fixed it. Ty.

Ancient Dragon 5,243

you can reduce that to just one line like this: `double qtrsSales[NUM_QTRS] = {0.0};` That will initialize all elments of the array to 0.