Thread: array of counters

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    61

    array of counters

    Here is what the problem is:

    Use a single-subscripted array to solve the following problem. A company pays its salespeople on a commission basis. The sales person recieves $200 a 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):
    a)$200-299
    b)$300-399
    c)$400-499
    d)$500-599
    e)$600-699
    f)$700-799
    g)$800-899
    h)$900-999
    i)$1000 and over.

    Here is the code I have so far but I dont know if im on the right track or not. I suck at c++. If anyone could look at it and tell me if im on the right track or not, I would greatly appreciate it, thanks.

    #include <iostream>
    using namespace std;


    const int counterSize=10;
    const int rangeSize=27;

    int counter[counterSize]={0};
    int ranges[rangeSize]={1,2,3,4,5,6,7,8,9,10,
    2,5,7,9,4,5,7,9,3,7,1,4,
    5,6,7,8,3};


    for(int salary=0; salary<rangeSize; salary++)
    ++counter[ranges[salary]];


    //outputting frequencies

    cout<<"Range"<<"/t/t/t/t/t/t"<<"Frequency"<<endl;

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Steps:
    1. Create an array of ints of size 9 (for 9 salary brackets). Initialize it to zero.

    2. Take user input to determine how many employees we have.

    3. Create a 'for' loop, which runs once for each employee. In the loop ask for their gross sales. Calculate 9%, add that to 200, and increment the appropriate element of the counter array.

    4. Loop through each bracket of salaries and display the value of the appropriate counter.

    Simple.

    Note: When trying to determine what bracket a particular salary falls under, you could do it the simple way and add a bunch of 'if' statements. Even simpler though, would be to divide the salary by 100 (keeping everything as integers), and switching on the result. Look at how the brackets are set up:
    a)$200-299
    b)$300-399
    c)$400-499
    d)$500-599
    e)$600-699
    f)$700-799
    g)$800-899
    h)$900-999
    i)$1000 and over.
    If you earnt anything from $800 to $899, dividing that number by 100, and taking the result as an integer (no decimals), gives you 8. Thus we can use the following code:
    Code:
    switch (Salary/100)
    {
    case 2:
       Counters[0]++; //Increment the counter for that bracket
       break;
    case 3:
       Counters[1]++; //Increment the counter
       break;
    //etc
    As you can see, the Counters[n]++; statement will simply be repeated over and over. Also observe that the index used is always 2 less than the value of the 'case' statement. Thus we can shorten that code like so:
    Code:
    Counters[((int)Salary/100)-2]++;
    The '(int)' is needed to remove the decimals resulting from the division.

    I hope this has helped.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Talking A step in a right direction

    Hello

    Just to let you know.. that I am not going to do your homework for you.. but I do remember my days as a struggling CS student.. I would have loved for someone to point me in the right direction

    Here are a couple of tips that I would recommend:

    First.. initialize your array of counters

    Code:
    int pay_scale[8] = {200, 299};     // initialize the [0] and [1] element
    
         for(int i = 2; i < 8; i++)    // populate the rest of the pay_scale array
         {
                int j = i;
                pay_scale[i] = (pay_scale[--j] + 100);
         }
    So.. now you have an array that holds values ranging from 200 (in element [0]) and 999 (in element [8])

    Your remaining objectives now are:

    1.) Prompt the user to enter in a predetermined number of employee weekly salaries

    2.) Use boolean logic to compare each user entry to the pay_scale array.

    3.) Use binary logic (possibly in the form of IF/ELSE IF or Switch Case) to store the result of each comparison... (Possibly into another array called, int answer[predefined number of entries] for example) then the user can 'cout' the answer array and see how many employees fall within each catagory of pay ranges


    Hope this helps get you started in a right direction
    Last edited by The Brain; 07-04-2004 at 05:03 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I think my method is a better solution for this particular problem. However your one is good because you could easily change it to cover different salary brackets.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    thanks.. but i forgot to mention my method would require answer[] to be a dymanic array.. with a predetermined size declared by the user... not sure if dantestwin is up on using dynamic arrays..
    Last edited by The Brain; 07-04-2004 at 04:41 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Thank you both for your kind help, I really appreciate it. I haven't gotten to dynamic arrays yet, but nonetheless I appreciate the tips. When I do get to it, I'm sure this information will help. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM