Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
C++
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int[] costsarr = {210, 375, 181, 600, 750, 360, 13125, 58, 7553, 5509, 2708, 2934};
System.out.println("Cost per day");
        for (int y = 0; y < costsarr.length; y++) {

            int sum = 0;

            if (y == 0) {

                sum = sum + costsarr[0];

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }

            if (y == 1) {

                sum = sum + costsarr[0] + costsarr[1] * 2;

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }

            if (y == 2) {

                sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3;

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }

            if (y == 3) {

                sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4;

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }

            if (y == 4) {

                sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5;

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }
            if (y == 5) {

                sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6;

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }
            if (y == 6) {

                sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7;

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }
            if (y == 7) {

                sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8;

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }
            if (y == 8) {

                sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9;

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }
            if (y == 9) {

                sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9 + costsarr[9] * 10;

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }
            if (y == 10) {

                sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9 + costsarr[9] * 10 + costsarr[10] * 11;

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }
            if (y == 11) {

                sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9 + costsarr[9] * 10 + costsarr[10] * 11 + costsarr[11] * 12;

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }
            if (y == 12) {

                sum = sum + costsarr[0] + costsarr[1] * 2 + costsarr[2] * 3 + costsarr[3] * 4 + costsarr[4] * 5 + costsarr[5] * 6 + costsarr[6] * 7 + costsarr[7] * 8 + costsarr[8] * 9 + costsarr[9] * 10 + costsarr[10] * 11 + costsarr[11] * 12 + costsarr[12] * 13;

                System.out.println("Day " + (y + 1) + ": $" + sum);
            }

        } 

    }
}


What I have tried:

This is to calculate the amount spent each day for the 12 days of christmas. This is as best as i could shorten it and dont know where else to go from here.
Posted
Updated 7-Dec-16 16:43pm
v3
Comments
CHill60 7-Dec-16 18:25pm    
Shorten what? The number of lines of code? The length of time it takes to execute? The amount of memory it consumes?

Hint - try "switch" and try introducing a variable for the output
PIEBALDconsult 7-Dec-16 19:01pm    
Replace each newline with a SPACE. Done.
Dave Kreskowiak 7-Dec-16 19:04pm    
Look through that pile of if statements and notice a pattern in it. After a quick glance, you can remove all of those if statements and replace the entire stack with another loop inside the first one.
Member 12892727 7-Dec-16 20:38pm    
Thank you so much! i think i got it :)

1 solution

The main culprit is that you have put this
int sum=0;

inside the for loop, the effect of which is this sum is being reset to zero on each iteration, that results in having to repeat the previous day's addition in subsequent iteration again and again. So place it before the start of the loop. Once you do that, the sum will retain its value from the preceding iteration, that will get rid of the redundant addition in each if block.
Next, each of the if condition is based on the index number of the array, which you can derive from the iteration count which is y in your code, so why re-invent the wheel. You only need one line of code like this:
sum = sum + costsarr[y] * (y+1);

as the iteration count will take care of the array index.
Putting together, the final code should look like:
C#
int[] costsarr = {210, 375, 181, 600, 750, 360, 13125, 58, 7553, 5509, 2708, 2934};
int sum = 0;
for(int i=0;i<costsarr.length;i++)
{
	int day = i+1;
	sum += costsarr[i] * day;
	System.out.println("Day " + day + ": $" + sum);
}

Enjoy your learning...
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900