second array to hold the count.

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1436 Views - Last Post: 06 March 2012 - 02:30 PM Rate Topic: -----

#1 CarlosAAlvarez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 01-February 12

second array to hold the count.

Posted 05 March 2012 - 01:26 PM

I've been trying to figure this out all morning. I go the first part down, which is to calculate the salesmen's salary. now what I need to do is build a second array that holds the count that determines how many of the salespeople earned salaries in each of the following ranges: $200-$299, $300-$399, $400-$499, $500-%599, 600-699, 700-799, 800-899, 900-999, $1000 and over.
Here is my code so far:

public class SalesCommission
{
	public static void main ( String args [] )
	{
		int salesData[] = { 750, 1200, 5000, 7575, 10000, 9250, 12500, 3560, 4800 };
		int salaryCount[] = { 200-299, 300-399, 400-499, 500-599, 600-699, 700-799, 800-899, 900-999, 1000 };
		double salary = 0;
		int count = 0;
		

		for ( int counter = 0; counter < salesData.length; counter++ )
		{
		salary = salesData[counter] * 0.09 + 200;

		System.out.println (salary);

		}

		for (int counter = 0; counter < salaryCount.length; counter++ )
		{
			count = salaryCount[counter]++;

			System.out.println (count);
		}

		
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: second array to hold the count.

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: second array to hold the count.

Posted 05 March 2012 - 01:30 PM

:)
This statement:
int salaryCount[] = { 200-299, 300-399, 400-499, 500-599, 600-699, 700-799, 800-899, 900-999, 1000 };
is equivalent to
int salaryCount[] = { -99, -99, -99, -99, -99, -99, -99, -99, 1000 };

you simply store 200-299 = -99, 300-399 = -99,.....
Was This Post Helpful? 0
  • +
  • -

#3 CarlosAAlvarez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 01-February 12

Re: second array to hold the count.

Posted 05 March 2012 - 01:35 PM

View Postpbl, on 05 March 2012 - 01:30 PM, said:

:)
This statement:
int salaryCount[] = { 200-299, 300-399, 400-499, 500-599, 600-699, 700-799, 800-899, 900-999, 1000 };
is equivalent to
int salaryCount[] = { -99, -99, -99, -99, -99, -99, -99, -99, 1000 };

you simply store 200-299 = -99, 300-399 = -99,.....


Yes, that is what shows after i execute the program, you are right. I knew for sure that was incorrect, i just left it as is when i posted my problem. I am just lost on how i can make it into a count for the salary ranges.
Was This Post Helpful? 0
  • +
  • -

#4 CarlosAAlvarez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 01-February 12

Re: second array to hold the count.

Posted 05 March 2012 - 03:39 PM

In regards to my question, is a second array even necessary to hold the count? Or am I in the right direction?
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: second array to hold the count.

Posted 05 March 2012 - 04:15 PM

You can always:

int range[] = {200, 300, 400, 500, 600, 700, 800, 900};
int nbEach[] = new int[range.lenght + 1];

int i = 0;
for(; i < range.length; ++i) {
    if(salary >= range[i] && salary <= (range[i] + 99))
        ++nbEach[i];
        break;
    } 
}
// not found so the 1000+
if(i == range.length)
   ++nbEach[range.length];


Was This Post Helpful? 0
  • +
  • -

#6 CarlosAAlvarez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 01-February 12

Re: second array to hold the count.

Posted 05 March 2012 - 05:56 PM

View Postpbl, on 05 March 2012 - 04:15 PM, said:

You can always:

int range[] = {200, 300, 400, 500, 600, 700, 800, 900};
int nbEach[] = new int[range.lenght + 1];

int i = 0;
for(; i < range.length; ++i) {
    if(salary >= range[i] && salary <= (range[i] + 99))
        ++nbEach[i];
        break;
    } 
}
// not found so the 1000+
if(i == range.length)
   ++nbEach[range.length];



Unfortunately that did not work for me, the only thing it executed was the salary of the salespeople. The professor is also not allowing us to use break; unless the code requires a switch statement.
The output should look like everything vertically align. Starting from the salespeople's salaries to the number of occurrences that each salesman ranges from accordingly. I attached a picture of how the output should look like. I am really confused as of how to do this.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: second array to hold the count.

Posted 05 March 2012 - 06:02 PM

You can always replace the break by a boolean
boolean found = false;
for(int i = 0; i < range.length; ++i) {
    if(!found) {
        if(salary >= range[i] && salary <= (range[i] + 99))
           ++nbEach[i];
           found = true;
        } 
    }
}
// not found so the 1000+
if(!found)
   ++nbEach[range.length];



If you start to add restriction on the fly, as soon as we provide you with a solution, we are not out of the woods :)
Also the last lines of your posted image seems to be the frequencies
Was This Post Helpful? 0
  • +
  • -

#8 jdavi134   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: second array to hold the count.

Posted 05 March 2012 - 06:02 PM

So what you are saying is that you want the out to look like this:

salary count
salary count
salary count
salary count
etc....
Was This Post Helpful? 0
  • +
  • -

#9 CarlosAAlvarez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 01-February 12

Re: second array to hold the count.

Posted 05 March 2012 - 06:10 PM

View Postpbl, on 05 March 2012 - 06:02 PM, said:

You can always replace the break by a boolean
boolean found = false;
for(int i = 0; i < range.length; ++i) {
    if(!found) {
        if(salary >= range[i] && salary <= (range[i] + 99))
           ++nbEach[i];
           found = true;
        } 
    }
}
// not found so the 1000+
if(!found)
   ++nbEach[range.length];



If you start to add restriction on the fly, as soon as we provide you with a solution, we are not out of the woods :)
Also the last lines of your posted image seems to be the frequencies


Yes, sorry I could not provide beter wording, but yes those are frequencies. for example ( "How many of the salaries were between $200-$299. The frequency would output "1" just like it shows in the picture posted.

View Postjdavi134, on 05 March 2012 - 06:02 PM, said:

So what you are saying is that you want the out to look like this:

salary count
salary count
salary count
salary count
etc....


yes you are right. and then after the salary count, it would follow up with the frequencies of how many times that salary fell into a range (ex. $200-$299, $300-$399, etc.)
Was This Post Helpful? 0
  • +
  • -

#10 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: second array to hold the count.

Posted 05 March 2012 - 06:38 PM

View PostCarlosAAlvarez, on 05 March 2012 - 08:10 PM, said:

yes you are right. and then after the salary count, it would follow up with the frequencies of how many times that salary fell into a range (ex. $200-$299, $300-$399, etc.)

Seems that this is what your program does
Was This Post Helpful? 0
  • +
  • -

#11 CarlosAAlvarez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 01-February 12

Re: second array to hold the count.

Posted 05 March 2012 - 06:41 PM

View Postpbl, on 05 March 2012 - 06:38 PM, said:

View PostCarlosAAlvarez, on 05 March 2012 - 08:10 PM, said:

yes you are right. and then after the salary count, it would follow up with the frequencies of how many times that salary fell into a range (ex. $200-$299, $300-$399, etc.)

Seems that this is what your program does


The posted picture is showing how the output should look like, but my code is not outputting that. the only thing that outputs is the salary of the salespeople, I am trying to get the frequencies on to my code so it can output the same thing as the posted picture.
Was This Post Helpful? 0
  • +
  • -

#12 jdavi134   User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: second array to hold the count.

Posted 05 March 2012 - 06:50 PM

Ok I see what you are saying. But think about something here. You want to print out the number of people that have a salary within a certain range. You are setting your salaryCount array equal to the range that you are counting for each. So instead of setting the array values to the range, try setting each value to 0, and then you increment when a certain salary is found.

Hope this helps!

Jack
Was This Post Helpful? 0
  • +
  • -

#13 CarlosAAlvarez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 01-February 12

Re: second array to hold the count.

Posted 05 March 2012 - 07:26 PM

Well, everything outputs in the correct order now, but I just cant seem to get the frequencies correct, it only outputs "0" and it is suppose to output the number of time a salary falls within a "salary range" Here is a picture of my output

this is my current code

public class SalesCommission
{
	public static void main ( String args [] )
	{
		int salesData[] = { 750, 1200, 5000, 7575, 10000, 9250, 12500, 3560, 4800 };
		double salary = 0;
		int range = 0;

		for (int counter = 0; counter < salesData.length; counter++ )
		{
			salary = salesData[counter] * 0.09 + 200;
	
			System.out.println (salary);
	
		}	
		
		for (int counter =0; counter < salesData.length; counter++)
		{

			if (range > 10)
				range = 10;

			salesData[range] = salesData[range] + 1;
		
			System.out.println (range);
		}

		
		
		
	
		
	}
}

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#14 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: second array to hold the count.

Posted 06 March 2012 - 05:18 AM

int salesData[] = { 750, 1200, 5000, 7575, 10000, 9250, 12500, 3560, 4800 };
// ranges
// 0: 200-299
// 1: 300-399
// 2: 400-499
// 3: 500-599
// 4: 600-699
// 5: 700-799
// 6: 800-899
// 7: 900-999
// 8: 1000
int salaryCount[] = { 0,0,0,0,0,0,0,0,0 };

for (int counter = 0; counter < salesData.length; counter++ ) {
	double salary = salesData[counter] * 0.09 + 200;
	System.out.println(salary);
	// right, time to add to that count
	// you need to find what range it lands in
	int range = ???
	
	// what you're doing with that second array
	// in keeping track of the number of entries for each
	// slot
	// you increment the slot for each salary in a given range
	salaryCount[range]++;
}	

// NOW, print out the count data
// but it's not really salesData length, is it?
for (int i=0; i<salaryCount.length; i++) {
	System.out.println(salaryCount[i]);
}



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#15 CarlosAAlvarez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 01-February 12

Re: second array to hold the count.

Posted 06 March 2012 - 01:32 PM

Seems like I am having a hard time understanding the logic of how to keep count. Here is the code that I have been working on, am I even in the right direction? Or do I need to make big fixes?

public class SalesCommission
{
	public static void main ( String args [] )
	{
		int salesData[] = { 750, 1200, 5000, 7575, 10000, 9250, 12500, 3560, 4800 };
		int salaryCount[] = new int [9];
		double salary = 0;
		int range = 0;

		for (int counter = 0; counter < salesData.length; counter++ )
		{
			salary = salesData[counter] * 0.09 + 200;
	
			System.out.println (salary);
	
			range = salary / 100 - 2;
			
	
			salaryCount[range]++;
		
		}
		
		for (int counter = 0; counter<salaryCount.length; counter++)
		{
		System.out.println (salaryCount[counter]);
		}

		
	}
}





I know for a fact that the following formula is incorrect: range = salary /100 -2
What am i missing?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2