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