1

this is a simple program that takes calculates gross sales and determines how many people have earned salary in ranges ( 200 to 299, 300 to 399, so on..) i have stored the values in an array ( counter) how do i use an enhanced for loop to print my array ( not counter controlled, only enhanced)

this is my program

public class Sales 
{

    public static void main(String args[])
    {
        //declare
        double salesData[] = {750, 1200, 5000, 7575, 10000, 9250, 12500, 3560, 4800};
        double gross = 0;//[] = new double[9];
        int counter[]= new int[9];
        int i=0;


        //walk through sales data
        for(i=0; i<salesData.length;i++)
        {
            //calculate gross
            gross = 200 + salesData[i] * .09;

            //determine range and increment count array
            if((gross >200) && (gross  <= 299))
                counter[0] ++;

            else if((gross >300) && (gross  <= 399))
                counter[1] ++;

            else if((gross >400) && (gross  <= 499))
                counter[2] ++;

            else if((gross >500) && (gross  <= 599))
                counter[3] ++;

            else if((gross >600) && (gross  <= 699))
                counter[4] ++;

            else if((gross >700) && (gross  <= 799))
                counter[5] ++;

            else if((gross >800) && (gross  <= 899))
                counter[6] ++;

            else if((gross >900) && (gross  <= 999))
                counter[7] ++; 

            else if (gross >1000)
                counter[8]++;
            //print gross
            System.out.println(gross);

        }
2
  • Note that you could greatly simplify the actually complicated part of this loop by calculating the position to update, rather than using a series of ifs: int indexToUpdate = (int)((gross - 200) / 100); Also, you currently skip over anything that lands on the hundred exactly. Is that intentional? Commented Mar 16, 2012 at 23:01
  • the sales array does not have anything that lands on 1000 so i skip over it. how do you print the counter(the array currently storing the ranges) ?? Commented Mar 19, 2012 at 4:04

4 Answers 4

2
double salesData[] = {750, 1200, 5000, 7575, 10000, 9250, 12500, 3560, 4800};

for(double data : salesData){
gross = 200 + data * .09;
//your logic
}
Sign up to request clarification or add additional context in comments.

1 Comment

but i need to print the counter array with a for loop
1

The same way you use it on any other collection:

for( double d : salesData) {
     ...
}

Comments

1

Enhanced-for loop:

for(double b : salesData) System.out.println(b);

Arrays.toString(double[] array):

System.out.println(Arrays.toString(salesData));

1 Comment

ohh thank you! i used for(double b : salesData) System.out.println(b);
0
    double[] salesData = {750, 1200, 5000, 7575, 10000, 9250, 12500, 3560, 4800};
    int[] counter = new int[9];

    //walk through sales data
    for (double salesDatum : salesData)
    {
        //calculate gross
        double gross = 200 + salesDatum * .09;
        int group = Math.min(8, (int)gross / 100 - 200);

        counter[group]++;
        //print gross
        System.out.println(gross);

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.