# array of counters

• 07-03-2004
dantestwin
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;
• 07-03-2004
bennyandthejets
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:
Quote:

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.
• 07-03-2004
The Brain
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])

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 :)
• 07-04-2004
bennyandthejets
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.
• 07-04-2004
The Brain
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.. :rolleyes:
• 07-04-2004
dantestwin
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. :)