0

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++) {
        System.out.println("Total sale for category " + i + ": $" + 
    }
    return totalSale;
}
1
  • You can take the sum of an int[] with IntStream.of(mac).sum(). Commented Oct 8, 2016 at 15:30

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, @Chris! That I completely understand and made the fix. It was redundant of me to include the if statements in that context. But I guess my main problem is the part with "totalSale[0] = totalSale[0] + mac[i];" Since I define the index position for each statement, this statement is rendered useless if I have more than four inputs for that array. So I tried to create some kind sum += equation statement, but it didn't work in the loop. Or at least I am pretty sure it is rendered useless.
@SSW this is true, my suggestion is to make the method take in a 2-dimensional array instead of an array for each individual category. Have you learned 2D arrays or should I provide an example?
@SSW I updated my answer to include a better approach of tackling your problem, try it out for yourself!
thanks for the explanation! One quick question, what would happen to the old arrays? Like mac, iphone, ipad, ipod? Since the value input is written in terms of those names, would I have to change that too?
@SSW true again, you would have to replace them all with a 2D array. I can add an example to my answer.
|
0

Here, i can see that you are assuming mac, iphone, ipad and ipod arrays will always have the same number of elements (length). Your code can break in the case one of them have a different length.

I would rather suggest this approach:

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];
    }
    for (int i = 0; i < iphone.length ; i++) {
        totalSale[1] = totalSale[1] + iphone[i];
    }
    for (int i = 0; i < ipad.length ; i++) {
        totalSale[2] = totalSale[2] + ipad[i];
    }
    for (int i = 0; i < ipod.length ; i++) {
        totalSale[3] = totalSale[3] + ipod[i];
    }

    System.out.println("Total sale for category mac: $" + totalSale[0]);
    System.out.println("Total sale for category iphone: $" + totalSale[1]);
    System.out.println("Total sale for category ipad: $" + totalSale[2]);
    System.out.println("Total sale for category ipod: $" + totalSale[3]);

    return totalSale;
}

First, calculate the total sale for each category by its own, using a for loop for each of them. Then writing the result back to the console without the for loop, since it has no reason to be used.

Comments

0

Varargs will help you: https://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

public static int[] totalSale(int[]... categories) {
    int[] totalSale = new int[categories.length];

    for (int i = 0; i < totalSale.length; i++) {

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

So, in this case you can either to pass any amount of parameters:

int[] totalSales = totalSale(mac, iphone, ipad, ipod, ipod2, iphone3, iphone4);

int[] totalSales2 = totalSale(ipod, ipod2, iphone3, iphone4);

Or make an array to pass only one parameter using the same method:

int[][] categories = {mac, iphone, ipad, ipod, ipod2, ipod3, iphone100500};
int[] totalSales2 = totalSale(categories);

And if you want use only the second way, then you can just define the method as:

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

Comments

0

I suppose you requirement is SUM all array data as below and store the result into the array too?

int[] mac = {200,9000,13000,900};
int[] iphone = {500,5000,200,0};
int[] ipad = {900,4300,0,800};
int[] ipod = {0,300,120,500};
int[] result equal to {macSUM, iphoneSUM, ipadSUM, ipodSUM}

So we can use two loops one if for each device, and other one is for each element in the array the code show below as example

 public static int totalSale(int[] devices){
        int sum;
        for (int i = 0; i < devices.length ; i++) {
            sum += devices[i];
        }

        return totalSale;
    }

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 = new int[4];
    for(int i = 0; i < 4; i++) {
            totalSales[i] = totalSale(mac);
        }
}

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.