I'm suppose to follow this algorithm:

1. Declare all ordinary Variables
2. Prepare and initialize all arrays.
3. Prepare array to hold all item cost.
4. Initialize Array cost with 0.(using for()statement)
5. Compute cost of each item then store results to array cost.(using for()statement)
6. compute average cost(using for()statement)
7. print all items(item no., desc, price, qty, cost).
8. print average cost.

with this I came up with this code

public class ItemCost {


    public static void main(String[] args) {
        //Variable Declaration
        int ctr;
        float aveCost = 0;
        
        int[] itemNo = {111, 222, 333, 444};
        int[] qty = {5, 2, 6, 3};
        String[] desc = {"T.V.", "Printer", "LCD", "Electric Fan"};
        float[] price = {5000, 1300, 5500, 1000};
        float[] cost = new float[4];
        
        System.out.println("Item No\t\t Description \t Price\t\tQty\t  Cost\n");
        
        //loop statement for array cost and average cost
        for (ctr = 0; ctr<cost.length; ctr++) { 
           cost[ctr] = price[ctr] * qty[ctr];
           aveCost = cost[ctr] / ctr; }
        
        //loop statement for printing the elements
        for (ctr = 0; ctr < itemNo.length; ctr++)
        {
            
            System.out.printf("%5d\t\t%12s\t%8.2f\t%2d\t%8.2f\n", itemNo[ctr], 
                    desc[ctr], price[ctr], qty[ctr], cost[ctr]);
             
        }//end of for loop
            
            System.out.println();
            System.out.printf("Average Cost: %.2f\n", aveCost);           
    }
}

problem is, everything is correct except for the average cost which only output 1000, when its suppose to output 15,900.. can anyone pls help me

the problem is you are overwriting the value you have for aveCost, instead of correctly calculating it and later on calculating the average.

do you have any suggestion on how to not overwrite the value? >.<

for (ctr = 0; ctr<cost.length; ctr++) { 
           cost[ctr] = price[ctr] * qty[ctr];
           totalCost = totalCost + cost[ctr];
           aveCost = totalCost / ctr; }

i tried doing this but now it outputs 21200 haha

for (ctr = 0; ctr<cost.length; ctr++) { 
           cost[ctr] = price[ctr] * qty[ctr];
           totalCost = totalCost + cost[ctr];
           aveCost = totalCost / cost.length; }

:D yey i think this is the right code

Edited 4 Years Ago by Ashenvale: n/a

one last question.. what if I wanted the output to look like this?

15,900.00

how do I put comma(,) ? this was not been taught to us yet, I just want it to look more readable.

for (ctr = 0; ctr<cost.length; ctr++) { 
           cost[ctr] = price[ctr] * qty[ctr];
           totalCost = totalCost + cost[ctr];
           aveCost = totalCost / cost.length; }

:D yey i think this is the right code

yes and no.
move the line

aveCost = totalCost / cost.length;

outside of the for loop. you're still overwriting it. actually, you are calculating it every time you run your loop, but only the last time will remain saved, so it will be the same if you do it after running the iteration, it 'll just be a bit more efficiƫnt (especially if you have more elements in your arrays)

I'll post another thread for my last question since it is not related :) thank you for answering my questions. Its nice to have an active community

This question has already been answered. Start a new discussion instead.