some help needed.

This is a discussion on some help needed. within the C++ Programming forums, part of the General Programming Boards category; Kay , i am remaking ALL excersices of my c++ book. ( c++ How to program, fifth edition) and i ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    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 :
    CLASS HEADER
    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.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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.

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    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 );
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by g4j31a5
    Why don't you use
    Code:
    using namespace std;
    ???
    Maybe because it is not a good practice?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    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;
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    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.
    I'm just trying to be a better person - My Name Is Earl

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    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,

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    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

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Did you read my response? What problems are you having implementing the solution I suggested?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You probably don't want the
    Code:
    cout << endl;
    in earningslog::Setearnings().

    Looking at your code here
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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.

    [edit] 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]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    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)

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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).

    The problem is that your calculation for the frequency index was this:
    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.

  14. #14
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by Daved
    The problem is that your calculation for the frequency index was this:
    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
    (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...
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 10:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 12:55 PM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 08:22 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-01-2004, 12:24 AM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21