SSW SSW - 2 months ago 6
Java Question

Adding elements in an array in a loop

I'm only a beginner, so I know data structure and code logic isn't very good at all.

So this is a portion of a project, but I am having difficulty formatting the for loop. I am pretty sure I have to create some kind of for each loop. I wasn't sure how to do it, so I created a very simplified and broken down version (I know most of the code is in practice, but I wanted to visualize it. I need help printing out the total sales for each category of product. Right now, I only have four values, but if I were to go up to 30 values, this code would not work obviously. So any tips on how to create the for loop?

public static int[] totalSale( int[] mac, int[] iphone, int[] ipad, int[] ipod ){
int[] totalSale = {0,0,0,0};

for (int i = 0; i < mac.length ; i++) {
totalSale[0] = totalSale[0] + mac[i];
totalSale[1] = totalSale[1] + iphone[i];
totalSale[2] = totalSale[2] + ipad[i];
totalSale[3] = totalSale[3] + ipod[i];
}

for (int i = 0; i < totalSale.length; i++) {
if (i == 0) {
System.out.println("Total sale for category " + i + ": $" + totalSale[0]);
}
if (i == 1) {
System.out.println("Total sale for category " + i + ": $" + totalSale[1]);
}
if (i == 2) {
System.out.println("Total sale for category " + i + ": $" + totalSale[2]);
}
if (i == 3) {
System.out.println("Total sale for category " + i + ": $" + totalSale[3]);
}
}
return totalSale;
}


Here are the example input values if necessary:

public static void main(String[] arguments) {
int[] days = {1,2,3,4};
int[] mac = {200,9000,13000,900};
int[] iphone = {500,5000,200,0};
int[] ipad = {900,4300,0,800};
int[] ipod = {0,300,120,500};
numberOfDays(days);
int[]totalSales = totalSale(mac,iphone,ipad,ipod);
popularProduct(totalSales);
popularDay(days,mac,iphone,ipad,ipod);
}

Answer

Change

for (int i = 0; i < totalSale.length; i++) {
      if (i == 0) {
       System.out.println("Total sale for category " + i + ": $" + totalSale[0]);
      }
      if (i == 1) {
       System.out.println("Total sale for category " + i + ": $" + totalSale[1]);
      }
      if (i == 2) {
       System.out.println("Total sale for category " + i + ": $" + totalSale[2]);
      }
      if (i == 3) {
       System.out.println("Total sale for category " + i + ": $" + totalSale[3]);
      }
}

to

for (int i = 0; i < totalSale.length; i++) {
  System.out.println("Total sale for category " + i + ": $ + totalSale[i]);
}

There's no need to create an if-statement for every single element in the array totalSale. Think about general cases for the array when dealing with loop statements involving arrays. Since you're printing out every single element in the array with the same line "Total sale for category", you can toy with it and think about what's changing and what isn't.

Clearly, the only things changing are i and the element in the array that's being printed out. What's changing can be represented by variables (i, totalSale[i]) and what's not changing can be represented by constants ("Total sale for category", "$"). Therefore, you only need one line in your for-loop to express this when printing out elements of an array.

EDIT: A way to deal with the problem of accepting a variable number of sales categories in your method is to instead make your method take in a 2D array, in which the number of rows is the number of categories and the number of columns ins the number of sales within each category. For example, int sales[][] = new int[30][20] represents an array of 30 categories with 20 sales in each category. So modify the header,

public static int[] totalSale( int[] mac, int[] iphone, int[] ipad, int[] ipod )

to

public static int[] totalSale(int[][] sales)

and change this,

int[] totalSale = {0,0,0,0};

for (int i = 0; i < mac.length ; i++) {
    totalSale[0] = totalSale[0] + mac[i]; 
    totalSale[1] = totalSale[1] + iphone[i]; 
    totalSale[2] = totalSale[2] + ipad[i];
    totalSale[3] = totalSale[3] + ipod[i];
}

to

int[] totalSale = new int[sales.length];//sales.length is the number of         
                                        //rows/categories in sales

for (int i = 0; i < sales.length ; i++) {
    for (int j = 0; j < sales[i].length; j++) { //sales[i].length is the number
                                                //of sales/columns per category
        totalSale[i] = totalSale[i] + sales[i][j];//sales[i][j] is the jth
                                                  //sale in the ith category
    }
}

And lastly in the main, those 4 arrays can now be replaced with a 2D array.

Replace,

int[] mac = {200,9000,13000,900};
int[] iphone = {500,5000,200,0};
int[] ipad = {900,4300,0,800};
int[] ipod = {0,300,120,500};

with

int[][] sales = { {200,9000,13000,900},
                  {500,5000,200,0},
                  {900,4300,0,800},
                  {0,300,120,500}}; //how you define a 2D array

Above is a 4x4 array but it can be flexible. You can make it 10x10, 20x20, 30x30, etc. Lastly, don't forget to change the method call to take in only one parameter now, the 2D array.

Change

popularDay(days,mac,iphone,ipad,ipod);

to

popularDay(sales);