# Comparing values problems

• 11-13-2010, 05:06 PM
mari.beka
Comparing values problems
Hello everybody,

I'm trying to write a program that calculates the year salary of a person in two variants A and B for a period of 25 years.
The program should display for each year the raise and the total sum of the money. And in the end the program should display a comparing sentence,
saying which variant is better A or B and from which year.

This is the program code that i've done so far, but sth is wrong with the sentence cuz it shows always the last year as a reference....please help.....

Code:

```public class Lohnerhohung {     public static void main(String[] args){         double kapitalA=1200,kapitalB=1200;         double [] sumA = new double[100];         double []sumB= new double[100];         double varianteB = 0;         int i=0;         double jahreA,sumA_1 = 0,sumB_1=0;         System.out.println("Year | Year A | Sum A | Year B | Sum B");         do{             kapitalA = (kapitalA * 0.100) * 11;             jahreA=kapitalA+100;             kapitalB=kapitalB*Math.pow((1+0.1/11), 11);             varianteB+=kapitalB;             sumA_1+=jahreA;             sumA[i]=sumA_1;             sumB_1+=varianteB;             sumB[i]=sumB_1;             System.out.print("\n"+i);             System.out.print("\t"+jahreA);             System.out.print("\t\t"+sumA[i]);             System.out.print("\t\t"+varianteB);             System.out.print("\t\t"+sumB[i]);             i++;         }         while(i<25);           if(sumA[i]>kapitalB){         System.out.println("A is greater from year"+i);}  else if (sumB[i]>sumA[i]){  System.out.println("B is greater from year"+i);}     }     }```
• 11-13-2010, 05:41 PM
venerik
After the while loop i equals 25 and both sumA[i] and sumB[i] are zero... that's why
• 11-14-2010, 03:00 AM
Eranga
Keep track of the summation separately for validation. Or else you've traverse the same again to find the total.
• 11-14-2010, 11:36 AM
mari.beka
Thank you guys, i appreciate your words....I have tried a lot to make things better, but still having problems to find a way to publish that sentence in the end....here is the changed code
Code:

```public class Lohnerhohung {     public static void main(String[] args){         double kapitalA=1200,kapitalB=1200;         double [] sumA = new double[100];         double [] sumB = new double[100];         double varianteB = 0;         int i=1;         double jahreA,sumA_1 = 0,sumB_1=0;         double max = 0;         System.out.println("Year | Year A | Sum A | Year B | Sum B");                 for(i=0;i<25;i++){             kapitalA = (kapitalA * 0.100) * 11;             jahreA=kapitalA+100;             kapitalB=kapitalB*Math.pow((1+0.1/11), 11);             varianteB+=kapitalB;             sumA_1+=jahreA;             sumA[i]=sumA_1;             sumB_1+=varianteB;             sumB[i]=sumB_1;             System.out.print("\n"+i);             System.out.print("\t"+jahreA);             System.out.print("\t\t"+sumA[i]);             System.out.print("\t\t"+varianteB);             System.out.print("\t\t"+sumB[i]);}             max = Double.NEGATIVE_INFINITY;             for(i=1;i< 25;i++){                 if(sumB[i]>sumA[i]) {                 max = sumB[i];             }             }             System.out.println("\n"+max+"\t"+i);         }     }```
The problem now is that it gets the highest number of all of them, but i want the first max number. Meaning when one of the sums gets higher, then it should print that number when the sum got higher, and the year when it got higher..

example
Code:

```Year | Year A | Sum A | Year B | Sum B 0        1420.0                1420.0                1325.6556809295562                1325.6556809295562 1        1552.0                2972.0                2790.1248345802273                4115.780515509783 2        1697.2000000000003                4669.200000000001                4407.94304548282                8523.723560992603```
In this case i want the program to publish just the sumB and year 1....but i cant...with the code that i mentioned i can just get the maximum one....
• 11-14-2010, 03:43 PM
Eranga
You've terminate the process at i = 25. At that time in both arrays the value of element 26 (or the index 25) holds the value of zero. So none of the criteria is not met, and nothing is print to the console.

Could you explain a bit what you really want to do? What's the logic behind to print those statements.
• 11-14-2010, 05:45 PM
mari.beka
The logic of this is that the program should print a statement which shows which alternative is better and from which year.

In my case the program is calculating the salary in two ways so the program should print the a statement that says
alternative A or B (comparing the sumA and sumB) is better from year ?.
• 11-15-2010, 02:47 AM
Eranga
But the thing is sumA and sumB holds zero in your case. Did you try to debug the code?
• 11-15-2010, 10:18 AM
mari.beka
holds zero.....i don't understand...
yeah i also debugged the program...it's ok....
just the statement is not.......
• 11-15-2010, 10:43 AM
RamyaSivakanth
U have to put comments above each line of code for understanding your logic.

As per the above code ecev max should also be an array.and i u are printing which will be always 25 .
• 11-15-2010, 06:43 PM
mari.beka
guys....if i knew how to do it,
i wouldn't ask for help..........so please if you have a real answer for it, tell it to me....
i cannot explain more then this....i think i have made all the my questions clear...
• 11-15-2010, 09:57 PM
venerik
Okay, I'll give it a try then...

You want to find an answer to the question: in what year does the sum of variant B exceed the sum of variant A. Right?

What you need to do is check sumA and sumB inside the while statement and remember i when sum B exceeds sum A. After the loop you can print your sentence.

Something like:

Code:

```        int j = -1;         do{                 kapitalA = (kapitalA * 0.100) * 11;                 jahreA=kapitalA+100;                 sumA_1+=jahreA;                 sumA[i]=sumA_1;                                 kapitalB=kapitalB*Math.pow((1+0.1/11), 11);                 varianteB+=kapitalB;                 sumB_1+=varianteB;                 sumB[i]=sumB_1;                                 if (sumB[i] > sumA[i] && j == -1) {                         j = i;                 }                 System.out.printf("%2d",i);                 System.out.printf("\t%10.2f",jahreA);                 System.out.printf("\t\t%10.2f",sumA[i]);                 System.out.printf("\t\t%10.2f",varianteB);                 System.out.printf("\t\t%10.2f\n",sumB[i]);                 i++;         } while(i<25);         System.out.println("B is greater from year "+j);     }```
This assumes that variant A starts as the 'better' option which is the case in your code. If you want to try other variants and you don't know which one of the to starts as the better option you should first check which one starts as the best one like this:

Code:

```                if (i == 0) {                         // in the first year determine best starting position                         bestStartingPosition = sumA[i] > sumB[i] ? 'A' : 'B';                 }                                 switch (bestStartingPosition) {                 case 'A':                         // if A has the best start, check whether B exceeds A this year                         if (sumB[i] > sumA[i] && j == -1) {                                 j = i;                         }                         break;                 case 'B':                         // if B has the best start, check whether A exceeds B this year                         if (sumA[i] > sumB[i] && j == -1) {                                 j = i;                         }                 }```

Code:

```        if(bestStartingPosition == 'A' && j != -1) {                 System.out.println("B is greater from year "+j);     }         if(bestStartingPosition == 'B' && j != -1) {                 System.out.println("A is greater from year "+j);     }```
after the while loop
I hope this helps to get your problem solved.

Erik
• 11-16-2010, 08:11 PM
mari.beka
Thank u so much....this is what i was trying to do all day....
i just forgot to put that variable to save i.....

Thanks again
• 11-16-2010, 10:15 PM
venerik
You are welcome.