# some help needed.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-07-2007
epidemic
some help needed.
Kay , i am remaking ALL excersices of my c++ book. ( c++ How to program, fifth edition)

and i just entered the stage about arrays and vectors.... , this is my assignment:

Code:

```Use a one-dimensional array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople each receive \$200 per week plus 9 percent of their gross sales for that week. For example, a salesperson who grosses \$5000 in sales in a week receives \$200 plus 9 percent of \$5000, or a total of \$650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson's salary is truncated to an integer amount): \$200\$299 \$300\$399 \$400\$499 \$500\$599 \$600\$699 \$700\$799 \$800\$899 \$900\$999 \$1000 and over```
this is what i made :
Code:

```#ifndef classB_0 header.h #define classB_0 header.h #include <iostream> using std::cout; using std::endl; using std::cin; using std::getline; class earningslog {   public:   static const int employees = 20; // 20 employee's   earningslog (float []);   void Setearnings (float []);   void  outputbar  (int []);   private:   int totalearnings[employees]; }; #endif```
CLASS IMPLEMENTATION
Code:

```#include "ClassB_0 header.h"  // 20 employee's earningslog::earningslog (float procentless[]) {   Setearnings (procentless); } void earningslog::Setearnings (float procentless[]) {   const int frequencysize = 9;   int frequency[frequencysize] = {0};   for (int x = 0; x < employees ; x++)   {     totalearnings[x] = ( (procentless[x]*0.09) + 200 );     if ( (totalearnings[x]  ) > 1000)     {       frequency[8]++;     }     else     {       frequency[ (totalearnings[x] / 100 ) ]++;  // problem:  cast to int makes some       // values turn to 0     }     cout << endl;   }   outputbar (frequency); } void earningslog::outputbar  (int frequency []) {   int l = 0;   for (int x = 200; x < 1000; x+=100 ,l++)   {     cout <<x<<"-"<<x+99<<": ";     cout << frequency[l] << endl;   }   cout <<"1000 and over:"<< frequency[8] << endl; }```
DRIVER PROGRAM (main)
Code:

```#include "ClassB_0 header.h" int main () {   float lol[earningslog::employees] =   {     1000,1500,2000,2500,3000,3500,4000,4000,5000,5500,     7000,8000,3000,6000,9999,8888,7777,6666,5555,4444   };   earningslog zor (lol); }```

output:
Code:

```200-299: 0 300-399: 0 400-499: 1 500-599: 2 600-699: 2 700-799: 4 800-899: 3 900-999: 2 1000 and over:2```
=> problem is that there are 20 employees and , and when i count my output it is only 16
i already know where the problem might be , and i marked that section bold, i just dont know how to solve the problem.
• 01-07-2007
Daved
That marked section is a problem. What if the salesperson has \$0 in sales (the lowest possible). Then the totalearnings will be \$200. That divided by 100 is 2, so you are incrementing the counter at index 2. What happened to indexes 0 and 1? You need to take that index calculation and account for the fact that \$200-\$299 should be at index 0, and with the rest following after it.
• 01-07-2007
g4j31a5
Code:

```using std::cout; using std::endl; using std::cin; using std::getline;```
Why don't you use
Code:

`using namespace std;`
???

Also, in your code, the array's index will start at the index "2" because you always add it with 200. And because the index is started at "2", the supposed index of 7 & 8 won't be processed.

Just remove the 200 in:
Code:

`totalearnings[x] = ( (procentless[x]*0.09) + 200 );`
• 01-07-2007
vart
Quote:

Originally Posted by g4j31a5
Why don't you use
Code:

`using namespace std;`
???

Maybe because it is not a good practice?
• 01-07-2007
g4j31a5
Quote:

Originally Posted by vart
Maybe because it is not a good practice?

So does
Code:

```using std::cout; using std::endl; using std::cin; using std::getline;```
:D
• 01-08-2007
swgh
A lot of text books show this:

Code:

```using std::cout; using std::endl;```
and have you belive it is the correct way to do this. Yes, it is better than just having

Code:

`using namespace std;`
Although the majority on this board I have seen use it. Prefixing each object with
std:: is seen as the most 'proffesional' way to do this. In my view, either choice is down to the programmer, as the compiler does not really care as long as it is correct. The only querk will come from other programmers who view or modify the code later on.
• 01-08-2007
epidemic
Quote:

Originally Posted by g4j31a5
Code:

```using std::cout; using std::endl; using std::cin; using std::getline;```
Why don't you use
Code:

`using namespace std;`
???

Also, in your code, the array's index will start at the index "2" because you always add it with 200. And because the index is started at "2", the supposed index of 7 & 8 won't be processed.

Just remove the 200 in:
Code:

`totalearnings[x] = ( (procentless[x]*0.09) + 200 );`

ehm sorry to say but, this does not solve the problem, im not even going to try that cause that line is just to CALCULATE the totalearnings and has nothing to do with the array frequency, and thats where the fault is ...

just for a quicky , this stands for : "The salespeople each receive \$200 per week plus 9 percent" as noted in the assignment.

but thanks for trying tough,
• 01-08-2007
epidemic
i give up , i think there is no possible solution for me with the knowledge i should have from that chapter+all the ones before it. (which isnt alot) also i think that the question is not clear enough ... well it is but i need some tips :(
• 01-08-2007
Daved
Did you read my response? What problems are you having implementing the solution I suggested?
• 01-08-2007
dwks
You probably don't want the
Code:

`cout << endl;`
in earningslog::Setearnings().

Code:

```  for (int x = 0; x < employees ; x++)   {     totalearnings[x] = ( (procentless[x]*0.09) + 200 );     if ( (totalearnings[x]  ) > 1000)     {       frequency[8]++;     }     else     {       frequency[ (totalearnings[x] / 100 ) ]++;  // problem:  cast to int makes some       // values turn to 0     }     cout << endl;   }```
You increment an index of frequency employees (that is, 20) times. So you must be either incrementing an invalid index (like -1 or 9), or there's something wrong with your printing code. I suggest printing what (totalearnings[x] / 100) is for each loop of the loop.
• 01-08-2007
dwks
Quote:

Originally Posted by Daved
That marked section is a problem. What if the salesperson has \$0 in sales (the lowest possible). Then the totalearnings will be \$200. That divided by 100 is 2, so you are incrementing the counter at index 2. What happened to indexes 0 and 1? You need to take that index calculation and account for the fact that \$200-\$299 should be at index 0, and with the rest following after it.

That still doesn't explain why the totals printed add up to 16, not 20.

 Wait.

Code:

```  for (int x = 0; x < employees ; x++)   {     totalearnings[x] = ( (procentless[x]*0.09) + 200 );     if ( (totalearnings[x]  ) > 1000)     {       frequency[8]++;     }     else     {       frequency[ (totalearnings[x] / 100 ) ]++;  // problem:  cast to int makes some       // values turn to 0     }     cout << endl;   }```
What if totalearnings[x] is 900? There's your problem. You're right, Daved. Sorry. [/edit]
• 01-08-2007
epidemic
yes ehm sorry david , i kande missed your post, but i was replying to the guy i quoted :P

ok this is what im trying now :

Code:

```void earningslog::Setearnings (float procentless[]) {   const int frequencysize = 9;   int frequency[frequencysize] = {0};   for (int x = 0; x < employees ; x++)   {     float sub = procentless[x]*0.09;     totalearnings[x] = ( sub + 200 );     if ( (totalearnings[x]  ) > 1000)     {       frequency[8]++;     }     else     {       const float test = 0.50;       float  sub  = totalearnings[x]/100;       cout << sub <<"\t"<< (int)sub << endl;       frequency[(int)sub]++;     }     }   outputbar (frequency); } void earningslog::outputbar  (int frequency []) {   int x = 0;   int y = 200;   cout <<y <<"-"<<y+99 <<": " << frequency[x] << endl;   x++,y+= 100;   cout <<y <<"-"<<y+99 <<": " << frequency[x] << endl;   x++,y+=100;   cout <<y <<"-"<<y+99 <<": " << frequency[x] << endl;   x++,y+=100;   cout <<y <<"-"<<y+99 <<": " << frequency[x] << endl;   x++,y+=100;   cout <<y <<"-"<<y+99 <<": " << frequency[x] << endl;   x++,y+=100;   cout <<y <<"-"<<y+99 <<": " << frequency[x] << endl;   x++,y+=100;   cout <<y <<"-"<<y+99 <<": " << frequency[x] << endl;   x++,y+=100;   cout <<y <<"-"<<y+99 <<": " << frequency[x] << endl;   x++,y+=100;   cout <<"1000 and over:"<< frequency[x] << endl; }```
output:

Code:

```E:\PROJECTMAP\test\tests\windows\Debug_Build\tests.exe 2.9        2 3.35        3 3.8        3 4.25        4 4.7        4 5.15        5 5.6        5 5.6        5 6.5        6 6.95        6 8.3        8 9.2        9 4.7        4 7.4        7 9.9992        9 8.9993        8 7.9994        7 6.9995        6 5.9996        5 200-299: 0 300-399: 0 400-499: 1 500-599: 2 600-699: 3 700-799: 4 800-899: 3 900-999: 2 1000 and over:3```

im sorry but i got to go to bed now i tought that i might use const float test = 0.50 and compare the sub numbers if their digets after the . are higher than zero, but well ...

i made a loop in it printing sub and the integer cast to it

ill look further tomorrow got to sleep now
tnx guys

( i changed totalearnings to the type as float)
• 01-08-2007
Daved
I'm thinking you only need to make one small change to the originally posted code. In fact, the change I am thinking of is only a few characters total.

The problem is not with float versus int. You were correct in the way you used int before (since the problem stated that you should truncate all numbers to int).

Code:

`(totalearnings[x] / 100 )`
That means that total earnings of \$200 maps to index 2, but you want it to map to index 0. All you have to do is adjust that line of code. I think you should go back to your original code from the first post and make that small change.
• 01-09-2007
g4j31a5
Quote:

Originally Posted by Daved
Code:

`(totalearnings[x] / 100 )`
That means that total earnings of \$200 maps to index 2, but you want it to map to index 0. All you have to do is adjust that line of code. I think you should go back to your original code from the first post and make that small change.

I agree with you. That's what I'm telling him before. Sorry if I can't make myself clear. The problem is
Code:

```  for (int x = 0; x < employees ; x++)   {     totalearnings[x] = ( (procentless[x]*0.09) + 200 );     if ( (totalearnings[x]  ) > 1000)     {       frequency[8]++;     }     else     {       frequency[ (totalearnings[x] / 100 ) ]++;  // problem:  cast to int makes some       // values turn to 0     }     cout << endl;   }```
In the bolded line, the total earnings was added by \$200 so the index will become
Quote:

(200+x)/100
There's 2 ways to overcome this:
Code:

```  for (int x = 0; x < employees ; x++)   {     totalearnings[x] = ( (procentless[x]*0.09) + 200 );     if ( (totalearnings[x]  ) > 1000)     {       frequency[8]++;     }     else     {       frequency[ ( (totalearnings[x] - 200 )/ 100 ) ]++;  // problem:  cast to int makes some       // values turn to 0     }     cout << endl;   }```
And because the value 200 is a constant, why don't we remove it in the first place (because the actual variable that matter is the procentless*0.09) to make it simpler. So the code would be like:
Code:

```  for (int x = 0; x < employees ; x++)   {     totalearnings[x] = ( (procentless[x]*0.09) ); <-- no need to add 200     if ( (totalearnings[x]  ) > 800) <-- but now the earnings will be at (0 to 800)+200     {       frequency[8]++;     }     else     {       frequency[ (totalearnings[x] / 100 ) ]++;  // problem:  cast to int makes some       // values turn to 0     }     cout << endl;   }```
Basically, I just changed from 200 to 1000 to (0 to 800)+200. Cheers...
• 01-09-2007
epidemic
hm ok, that prutty smart , ill try it later on got some homework now . thanks

if i dont post annymore you'll know this problem is solved :)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last