# Using an array instead of switch

• (2 Pages)
• 1
• 2

## 15 Replies - 21994 Views - Last Post: 11 February 2008 - 06:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=42870&amp;s=be129c6dda5fdd58f22ad2f7dae8e317&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Purity86

Reputation: 0
• Posts: 36
• Joined: 28-January 08

# Using an array instead of switch

Posted 09 February 2008 - 10:33 PM

Hi, i recently just started learning about arrays and i feel like i have the hang of single dimension arrays, but i cant for the life of me figure out this question. Im not looking for the answer/code to my homework, but some advice or tips on ways of figuring out the algorithm.

Here is a copy of the problem
Use a single-subscripted array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive \$200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses \$5000 in sales in a week receives \$200 plus 9% of \$5000 or a total of \$650. Write an application (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

\$300-\$399

c) \$400-\$499

d) \$500-\$599

e) \$600-\$699

f) \$700-\$799

g) \$800-\$899

h) \$900-\$999

i) \$1000 and over

You should write a method that returns the salary with the gross sales as the function argument.
You should write a method that takes the salary and the array as the function arguments and increase the appropriate array elements.

Im looking for some help to get started.

Im thinking since i know the ranges which the values will fall, i can declare my array as [ 9 ]. Should i declare the price ranges as the array information?

Anyone have any tips on how i can come up with the algorithm for it? (questions to ask myself, things to consider etc..) im new at java and programming in general

This post has been edited by Purity86: 09 February 2008 - 11:21 PM

Is This A Good Question/Topic? 0

## Replies To: Using an array instead of switch

### #2 Martyr2

• Programming Theoretician

Reputation: 5186
• Posts: 13,916
• Joined: 18-April 07

## Re: Using an array instead of switch

Posted 09 February 2008 - 11:30 PM

Well your array is going to be your counters. So you will need to setup the array with the 10 slots (corresponding to the ten ranges.. aka subscript 9) and set them to 0.

In your function where you accept the salary and the array of ranges, you will simply need to evaluate the salary and which range it fits in. You can do this with a series of if statements. If salary is greater than or equal to 1000, increment value at subscript 9, if it is greater than or equal to 900, increment subscript 8 etc etc.

That should put you on the right path to solve this.

### #3 Purity86

Reputation: 0
• Posts: 36
• Joined: 28-January 08

## Re: Using an array instead of switch

Posted 10 February 2008 - 08:24 AM

Martyr2, on 9 Feb, 2008 - 11:30 PM, said:

Well your array is going to be your counters. So you will need to setup the array with the 10 slots (corresponding to the ten ranges.. aka subscript 9) and set them to 0.

In your function where you accept the salary and the array of ranges, you will simply need to evaluate the salary and which range it fits in. You can do this with a series of if statements. If salary is greater than or equal to 1000, increment value at subscript 9, if it is greater than or equal to 900, increment subscript 8 etc etc.

That should put you on the right path to solve this.

So let me get this straight, i have pretty much made the program with out an array. I have 9 variables (one for each range) all named count_range1, count_range2 etc.. I have a switch statement that increments the right counter after the calculation. (so for example if the range is 300-399 i will increment the "count_range1 counter) At the end i have a chart with the range and the corresponding variable.

The only thing the array allows me to do, is not declare every variable separately in the start, and at the end of the program only have one statement (instead of printing every variable) that will print all the data from the array?

### #4 Martyr2

• Programming Theoretician

Reputation: 5186
• Posts: 13,916
• Joined: 18-April 07

## Re: Using an array instead of switch

Posted 10 February 2008 - 10:06 AM

Well yeah, the array's power comes in the fact that it is a sequential line of memory slots rather than having to declare each individual variable. It cuts down all the typing, repetitions, allows for expansion and also easier to get at and manipulate using more automated structures (like loops).

Whenever you find yourself using 10 vars representing similar data, this should be a classic sign you need an array.

### #5 Purity86

Reputation: 0
• Posts: 36
• Joined: 28-January 08

## Re: Using an array instead of switch

Posted 10 February 2008 - 04:53 PM

Am i on the right track? (this code is pseudocode just trying to set up the logic for the question before i start doing the code) Sorry for asking alot of questions, this is an online course with a list of problems due at the end of the week and my only reference is the java book i bought.

```int counter_array [] = [9]

Sales // method to ask user sales amount \ pass that information to get salary \ pass that to increase array
system.out.printf( "Enter sales amount (Negative to end) ");
gross_sales = nextInt()

getSalary(gross_sales) // method call to get salary method that will return the salary based on calculations. Uses gross_sales as an arguement
increaseArray( salary ) // method call to increase array that sends salary to increaseArray method and increases count

After loop
displaymessage( counter_array [] )

Method increase array( int salary, int counter_array [] )
if salary >= 200 & <=299
++counter[1]
else
if salary >= 300 & <=399
++counter[2]
else
if salary >= 400 & <=499
++counter[3]
else
if salary >= 500 & <=599
++counter[4]
else
if salary >= 600 & <=699
++counter[5]
else
if salary >= 700 & <=799
++counter[6]
else
if salary >= 800 & <=899
++counter[7]
else
if salary >= 900 & <=999
++counter[8]
else
if salary >= 1000
++counter[9]
else

method getSalary (int gross_sales)
return (gross_sales * 0.09 + 200)

method displaymessage( int counter_array [])
for displaycount = 0; displaycount <= 9 displaycount++
system.out.print("%4d%4d%4d", displaycount + 2 * 100, displaycount + 2 * 100 + 99, counter_array[ displaycount ]
```

This post has been edited by Purity86: 10 February 2008 - 04:56 PM

### #6 Martyr2

• Programming Theoretician

Reputation: 5186
• Posts: 13,916
• Joined: 18-April 07

## Re: Using an array instead of switch

Posted 10 February 2008 - 06:12 PM

It appears you are on the right track. If you setup your if statements to scale downwards, you can simply your range checking. For instance... instead of 900 to 999 you can check if salary is greater than or equal to 900 because if it was 1000 or over it would have triggered the previous if statement.

```if (salary >= 1000) { do code }
else if (salary >= 900) { do other code }
else if (salary >= 800) { do more code }

```

Get it fleshed out and then we can see what it does for you and fix any problems that come along.

### #7 Purity86

Reputation: 0
• Posts: 36
• Joined: 28-January 08

## Re: Using an array instead of switch

Posted 10 February 2008 - 06:19 PM

Martyr2, on 10 Feb, 2008 - 06:12 PM, said:

It appears you are on the right track. If you setup your if statements to scale downwards, you can simply your range checking. For instance... instead of 900 to 999 you can check if salary is greater than or equal to 900 because if it was 1000 or over it would have triggered the previous if statement.

```if (salary >= 1000) { do code }
else if (salary >= 900) { do other code }
else if (salary >= 800) { do more code }

```

Get it fleshed out and then we can see what it does for you and fix any problems that come along.

awesome thanks for the help man im going to start working on it

### #8 Purity86

Reputation: 0
• Posts: 36
• Joined: 28-January 08

## Re: Using an array instead of switch

Posted 11 February 2008 - 02:15 AM

Well i finished the program and compiled it and i have 2 errors
i put comments in my code to help describe what the plan for each line of code is

2 errors
line49 - illegal start of expression *public void salesMethod*
line71 - .class expected *Displaymessage( counter_array [] );*

```import java.util.Scanner; // importing java scanner

public class SalesComm
{

public int getSalary( int gross_sales ) // method to calculate salary, uses gross sales as arguement and returns gross sales
{
salary = (gross_sales * 0.09 + 200);
return salary;
}

public void displayMessage( int counter_array []) // method to display the results
{
for ( displaycount = 0; displaycount <= 9; displaycount++ )
System.out.print( "%4d%4d%4d", displaycount + 2 * 100, displaycount + 2 * 100 + 99, counter_array[ displaycount ] );

}

public void increaseArray( int salary, int counter_array [] ) // method to find out which counter to increase based on salary
{

if (salary >= 1000) {
counter_array[8]++;
} else if (salary >= 900) {
counter_array[7]++;
} else if (salary >= 800) {
counter_array[6]++;
} else if (salary >= 700) {
counter_array[5]++;
} else if (salary >= 600) {
counter_array[4]++;
} else if (salary >= 500) {
counter_array[3]++;
} else if (salary >= 400) {
counter_array[2]++;
} else if (salary >= 300) {
counter_array[1]++;
} else if (salary >= 200) {
counter_array[0]++;

}

public void salesMethod(); // method to get input, get salary, increase array, and display message
{
int counter_array[] = new int [ 9 ]; // intilizing an array
int gross_sales; // initilizing the variable for input
int salary;

Scanner input = new Scanner( System.in ); // creating scanner to read input on sales amount

System.out.printf( "Enter sales amount (Negative to end) "); // Asks the user to input the sales amount or sentinel value
gross_sales = input.nextInt(); // assigns input to variable gross_sales

while ( gross_sales >= 0 ) //loop
{
getSalary( gross_sales ); // method call with gross_sales as arguement, should return the salary
increaseArray( salary, counter_array ); //method call with salary as arguement

system.out.printf( "Enter sales amount (Negative to end) "); //asks the user to input the sales amount
gross_sales = input.nextInt(); // assigns input to variable gross_sales

} // ending while method

displayMessage( counter_array [] );// method call to display results using the counter_array as an arguement to the function

}

}

```

This post has been edited by Purity86: 11 February 2008 - 02:16 AM

### #9 letthecolorsrumble

• Student of The Sun

Reputation: 27
• Posts: 555
• Joined: 07-November 07

## Re: Using an array instead of switch

Posted 11 February 2008 - 05:12 AM

Purity86, on 11 Feb, 2008 - 02:15 AM, said:

Well i finished the program and compiled it and i have 2 errors
i put comments in my code to help describe what the plan for each line of code is

2 errors
line49 - illegal start of expression *public void salesMethod*
line71 - .class expected *Displaymessage( counter_array [] );*

```import java.util.Scanner; // importing java scanner

public class SalesComm
{

public int getSalary( int gross_sales ) // method to calculate salary, uses gross sales as arguement and returns gross sales
{
double salary = (gross_sales * 0.09 + 200); /*you failed to declare*/
return (int) salary; /*down-casting*/
}

public void displayMessage( int counter_array []) // method to display the results
{ /*declare variable in for loop*/
for ( int displaycount = 0; displaycount <= 9; displaycount++ )
System.out.printf( "%4d%4d%4d", displaycount + 2 * 100, displaycount + 2 * 100 + 99, counter_array[ displaycount ] );
/*printf instead of print while using formatted output*/
}

public void increaseArray( int salary, int counter_array [] ) // method to find out which counter to increase based on salary
{

if (salary >= 1000) {
counter_array[8]++;
}
else if (salary >= 900) {
counter_array[7]++;
}
else if (salary >= 800) {
counter_array[6]++;
}
else if (salary >= 700) {
counter_array[5]++;
}
else if (salary >= 600) {
counter_array[4]++;
}
else if (salary >= 500) {
counter_array[3]++;
}
else if (salary >= 400) {
counter_array[2]++;
}
else if (salary >= 300) {
counter_array[1]++;
}
else if (salary >= 200) {
counter_array[0]++;
}
}

public void salesMethod()/*removed semicolon*/ // method to get input, get salary, increase array, and display message
{
int counter_array[] = new int [ 9 ]; // intilizing an array
int gross_sales; // initilizing the variable for input
int salary;

Scanner input = new Scanner( System.in ); // creating scanner to read input on sales amount

System.out.printf( "Enter sales amount (Negative to end) "); // Asks the user to input the sales amount or sentinel value
gross_sales = input.nextInt(); // assigns input to variable gross_sales
salary = getSalary(gross_sales);/*computing salary*/
while ( gross_sales >= 0 ) //loop
{
getSalary( gross_sales ); // method call with gross_sales as arguement, should return the salary
increaseArray( salary, counter_array ); //method call with salary as arguement

System.out.printf( "Enter sales amount (Negative to end) "); //asks the user to input the sales amount
gross_sales = input.nextInt(); // assigns input to variable gross_sales

} // ending while method

displayMessage( counter_array );// method call to display results using the counter_array as an arguement to the function

}

}

```

Missing main method couldnt check if it works but the errors are away... the single line comments will help to idenity the corrections.

### #10 Purity86

Reputation: 0
• Posts: 36
• Joined: 28-January 08

## Re: Using an array instead of switch

Posted 11 February 2008 - 11:44 AM

I am still getting the same errors let me show you guys the entire program.

```import java.util.Scanner; // importing java scanner

public class SalesComm
{
private double salary; // declaring variable salary of type double

public int getSalary( int gross_sales ) // method to calculate salary, uses gross sales as arguement and returns gross sales
{
double salary = (gross_sales * 0.09 + 200);
return salary;
}

public void displayMessage( int counter_array []) // method to display the results
{
for ( displaycount = 0; displaycount <= 9; displaycount++ )
System.out.printf( "%4d%4d%4d", displaycount + 2 * 100, displaycount + 2 * 100 + 99, counter_array[ displaycount ] );

}

public void increaseArray( int salary, int counter_array [] ) // method to find out which counter to increase based on salary
{

if (salary >= 1000) {
counter_array[8]++;
} else if (salary >= 900) {
counter_array[7]++;
} else if (salary >= 800) {
counter_array[6]++;
} else if (salary >= 700) {
counter_array[5]++;
} else if (salary >= 600) {
counter_array[4]++;
} else if (salary >= 500) {
counter_array[3]++;
} else if (salary >= 400) {
counter_array[2]++;
} else if (salary >= 300) {
counter_array[1]++;
} else if (salary >= 200) {
counter_array[0]++;

}

public void salesMethod(); // method to get input, get salary, increase array, and display message
{
int counter_array[] = new int [ 9 ]; // intilizing an array
int gross_sales; // initilizing the variable for input
double salary;

Scanner input = new Scanner( System.in ); // creating scanner to read input on sales amount

System.out.printf( "Enter sales amount (Negative to end) "); // Asks the user to input the sales amount or sentinel value
gross_sales = input.nextInt(); // assigns input to variable gross_sales

while ( gross_sales >= 0 ) //loop
{
getSalary( gross_sales ); // method call with gross_sales as arguement, should return the salary
increaseArray( salary, counter_array ); //method call with salary as arguement

system.out.printf( "Enter sales amount (Negative to end) "); //asks the user to input the sales amount
gross_sales = input.nextInt(); // assigns input to variable gross_sales

} // ending while method

displayMessage( counter_array [] );// method call to display results using the counter_array as an arguement to the function

}

}
```

The following program is my application program

```public class SalesCommTest
{
public static void main( String args[] )
{

SalesComm mysales = new SalesComm();

mysales.salesMethod();
}
}

```

can anyone help me figure out these 2 errors along with any others you might see while you look at the code.
Any help would be much appreciated .

### #11 Martyr2

• Programming Theoretician

Reputation: 5186
• Posts: 13,916
• Joined: 18-April 07

## Re: Using an array instead of switch

Posted 11 February 2008 - 12:04 PM

well the first error is because you forgot an extra closing brace before your "public void SalesMethod" function. This closing brace will be to close off your increaseArray() function. You also have a semicolon after "salesMethod" that should not be there.

``` } else if (salary >= 200) {
counter_array[0]++;

}

} // <---- Error1... curly brace to close the function here

// Error2...See the semicolon here? Needs to be removed!
public void salesMethod(); // method to get input, get salary, increase array, and display message
{

```

Fix these two problems and recompile.... see what else comes up.

This post has been edited by Martyr2: 11 February 2008 - 12:04 PM

### #12 Purity86

Reputation: 0
• Posts: 36
• Joined: 28-January 08

## Re: Using an array instead of switch

Posted 11 February 2008 - 12:17 PM

Martyr2, on 11 Feb, 2008 - 12:04 PM, said:

well the first error is because you forgot an extra closing brace before your "public void SalesMethod" function. This closing brace will be to close off your increaseArray() function. You also have a semicolon after "salesMethod" that should not be there.

``` } else if (salary >= 200) {
counter_array[0]++;

}

} // <---- Error1... curly brace to close the function here

// Error2...See the semicolon here? Needs to be removed!
public void salesMethod(); // method to get input, get salary, increase array, and display message
{

```

Fix these two problems and recompile.... see what else comes up.

Man thank you so much for the help man and the quick responses. Well the only error left now is that the compiler expects .class for my displayMessage method call at the very end of the program

Im also concerned about my return function (to get salary do calculations/ and use increaseArray method) i dont know if i set that up properly

### #13 Martyr2

• Programming Theoretician

Reputation: 5186
• Posts: 13,916
• Joined: 18-April 07

## Re: Using an array instead of switch

Posted 11 February 2008 - 01:05 PM

You had a bit of problems else where but your logic is functional. I have gone ahead and fixed it to start working for you. I have outlined the changes I have made below...

1) Your getSalary function will need to return a double, because salaries can be of decimal values after the calculation
2) Added some extra parenthesis in yoru getSalary function just to make it clear of order of operations
3) Commented out your printf line in displayMessage. I am not too sure what the calculations in there are for, but I have replaced it with a simple statement to show you your count_array values and show your counts.
4) Your loop in displayMessage should be 0-8 not 0-9 so you need only "<" and not less than or equal to. You would overrun your array boundaries otherwise.
5) Your increaseArray function needs to take a double for the salary, not int. Again salaries can have decimal values after the calculation.
6) Added decimal places to your salary tests just to make sure you are comparing similar types... doubles against values with decimal values. Notice they did not change value.
7) Added that closing curly brace and removed the semicolon.
8) In salesMethod initialized your salary variable. Initialize before attempting to use.
9) In your while loop your gross_sales function returns a double, you need to store that in the salary variable before passing it to increaseArray() function. The variable salary defined in getSalary is a local variable and your other salary variable defined at the top of the program is a class level. Different scopes, so the program level variable needs to be set to the value returned by the function. Otherwise it doesn't get set.
10) When passing an array to a function that takes an array, don't include the square brackets. At the end, notice that there are no square brackets on the parameter passed to displayMessage(). Only square brackets on the incoming parameter in the displayMessage() header.

Here is the final product....

```import java.util.Scanner; // importing java scanner

public class SalesComm
{
private double salary; // declaring variable salary of type double

public double getSalary( int gross_sales ) // method to calculate salary, uses gross sales as arguement and returns gross sales
{
double salary = ((gross_sales * 0.09) + 200);
return salary;
}

public void displayMessage( int counter_array[]) // method to display the results
{
for (int displaycount = 0; displaycount < 9; displaycount++ )
//System.out.printf( "%4d%4d%4d", displaycount + 2 * 100, displaycount + 2 * 100 + 99, counter_array[ displaycount ] );
System.out.println(displaycount + ": " + counter_array[displaycount]);

}

public void increaseArray( double salary, int counter_array [] ) // method to find out which counter to increase based on salary
{

if (salary >= 1000.00) {
counter_array[8]++;
} else if (salary >= 900.00) {
counter_array[7]++;
} else if (salary >= 800.00) {
counter_array[6]++;
} else if (salary >= 700.00) {
counter_array[5]++;
} else if (salary >= 600.00) {
counter_array[4]++;
} else if (salary >= 500.00) {
counter_array[3]++;
} else if (salary >= 400.00) {
counter_array[2]++;
} else if (salary >= 300.00) {
counter_array[1]++;
} else if (salary >= 200.00) {
counter_array[0]++;

}
}

public void salesMethod() // method to get input, get salary, increase array, and display message
{
int counter_array[] = new int [ 9 ]; // intilizing an array
int gross_sales; // initilizing the variable for input
double salary = 0.0;

Scanner input = new Scanner( System.in ); // creating scanner to read input on sales amount

System.out.printf( "Enter sales amount (Negative to end) "); // Asks the user to input the sales amount or sentinel value
gross_sales = input.nextInt(); // assigns input to variable gross_sales

while ( gross_sales >= 0 ) //loop
{
salary = getSalary( gross_sales ); // method call with gross_sales as arguement, should return the salary
increaseArray( salary, counter_array ); //method call with salary as arguement

System.out.printf( "Enter sales amount (Negative to end) "); //asks the user to input the sales amount
gross_sales = input.nextInt(); // assigns input to variable gross_sales

} // ending while method

displayMessage(counter_array);

}

}

```

Read through the changes and find them in the code. You will see how they match up and why things were done this way.

Enjoy!

"At DIC we be array fixing, programming hacking code ninjas!"

### #14 Purity86

Reputation: 0
• Posts: 36
• Joined: 28-January 08

## Re: Using an array instead of switch

Posted 11 February 2008 - 03:14 PM

Well the program works perfectly thank you so much for the tips, i see the mistakes i made. i have one last question for you, since i am bored at work now. Is it possible to condense my increaseArray method with a loop and another array for the ranges (100 900 800 700). Dont want to know the anserw if there is one, just want to know if it is possible (it will give me something to try and figure out)

### #15 Martyr2

• Programming Theoretician

Reputation: 5186
• Posts: 13,916
• Joined: 18-April 07

## Re: Using an array instead of switch

Posted 11 February 2008 - 05:37 PM

You could boil it down to an algorithm that will compare the salary for you.