1. Member
Join Date
Nov 2009
Posts
11
Rep Power
0

## Problem solved...thanks Jos!!!

Hi all,

I've decided to give it another go and here is my updated code...i think i have method m being called right, but my math within method m is wrong, i can't figure out how to make it right...any help would be greatly appreciated...

/** This program uses a method
* that calculates 1/2 + 2/3 + ... + i/(i+1)
*/

public class Assignment4 {
/* main method */
public static void main(String[] args) {
double mi; //used to catch the return value from method m(i)
System.out.println("i\t\tm(i)\n");

//Print the table
for (int i = 1; i <= 20;i++) {
mi = m(i); //Fill in blank here. Call the method
System.out.println(i+"\t\t"+ mi);
//System.out.println(mi);
} //end for

} //end main

/* define non-void method m(i) here */
public static double m(double key){
double sum;
double i=1;
key = (i/(i+1));
sum =+ key + (i/(i+1));
i++;
//System.out.println(key);
return key;
}
}

//end class
Last edited by mnki23; 11-04-2009 at 04:54 PM. Reason: Change my wording...

2. What's the output you're getting? Try putting a print statement inside your m method to test if it's actually being called but maybe not calculating something right.

3. Member
Join Date
Nov 2009
Posts
11
Rep Power
0

## Here is the output i am getting

i m(i)

1 0.5
2 0.6666666666666666
3 0.75
4 0.8
5 0.8333333333333334
6 0.8571428571428571
7 0.875
8 0.8888888888888888
9 0.9
10 0.9090909090909091
11 0.9166666666666666
12 0.9230769230769231
13 0.9285714285714286
14 0.9333333333333333
15 0.9375
16 0.9411764705882353
17 0.9444444444444444
18 0.9473684210526315
19 0.95
20 0.9523809523809523

but the output i'm looking for is:

i m(i)
1 0.5
2 1.1667

19 16.4023
20 17.3546

4. Originally Posted by mnki23
i m(i)

1 0.5
2 0.6666666666666666
3 0.75
4 0.8
5 0.8333333333333334
6 0.8571428571428571
7 0.875
8 0.8888888888888888
9 0.9
10 0.9090909090909091
11 0.9166666666666666
12 0.9230769230769231
13 0.9285714285714286
14 0.9333333333333333
15 0.9375
16 0.9411764705882353
17 0.9444444444444444
18 0.9473684210526315
19 0.95
20 0.9523809523809523

but the output i'm looking for is:

i m(i)
1 0.5
2 1.1667

19 16.4023
20 17.3546
I can understand the output you're getting (i/(i+1)), but why do you expect otherwise? I can't see a regularity in your wanted output.

kind regards,

Jos

5. Member
Join Date
Nov 2009
Posts
11
Rep Power
0
but the output i'm looking for is:

i m(i)
1 0.5
2 1.1667

19 16.4023
20 17.3546

According to the book, the correct output should be what i have posted above, can't figure out where i am wrong.

6. Originally Posted by mnki23
but the output i'm looking for is:

i m(i)
1 0.5
2 1.1667

19 16.4023
20 17.3546

According to the book, the correct output should be what i have posted above, can't figure out where i am wrong.
Does you book mention a formula? I'm sure it's not i/(i+1) for several values of i.

kind regards,

Jos

7. you're dividing a double by a double without any sort of decimal format attached to it. It's going to print out to the furthest decimal it can.

Also your math may be wrong. If you put in 1 as your starting you get key = 1/(1+1) = 1/2. Your algorithm is decreasing your input so the things you add together get progressively smaller. Picture it as a limit function, your answer will go on to infinity but the each time you're adding something smaller and smaller so you never get to one, you just become infinitely close to it.

8. Member
Join Date
Nov 2009
Posts
11
Rep Power
0
Thanks for the quick responses....here is the formula the book has

m(i)=(1/2)+(2/3)+...+i/(i+1)

regards

9. Originally Posted by mnki23
Thanks for the quick responses....here is the formula the book has

m(i)=(1/2)+(2/3)+...+i/(i+1)

regards

kind regards,

Jos

10. Member
Join Date
Nov 2009
Posts
11
Rep Power
0
I can't figure out how to add them all up....i know that i need to add up (1/2)+(2/3)+(3/4) and so on until you get to (20/21); but i can't figure it out.

11. Originally Posted by mnki23
I can't figure out how to add them all up....i know that i need to add up (1/2)+(2/3)+(3/4) and so on until you get to (20/21); but i can't figure it out.
In the body of your loop add the term m(i) to a variable, say, 'sum'; before you start the loop don't forget to initialize it to 0.

kind regards,

Jos

12. Member
Join Date
Nov 2009
Posts
11
Rep Power
0
Gents,

If one of you could give me a solution it would be greatly appreciated, i've wasted approx. 8 hours on this problem and haven't been able to come up with a solution, i'm so ready to throw in the towel, i'm sure i'm overlooking something simple

13. Member
Join Date
Nov 2009
Posts
11
Rep Power
0
i've updated my code and am trying again any help is greatly appreciated

14. Originally Posted by mnki23
i've updated my code and am trying again any help is greatly appreciated
Show your latest version of the code; you are calling method m(i) in your loop and you want to add its return value to a variable over and over again in that loop; when you've initialized that variable to 0.0 you end up with all the values m(1)+m(2)+m(3) ... m(20) in that variable when your loop has finished.

It's your turn now, use your imagination; if you want to learn how to program you have to use your imagination in a rational way otherwise programming is not for you ...

kind regards,

Jos

15. Member
Join Date
Nov 2009
Posts
11
Rep Power
0
Jos,

Here is the latest code

public class Assignment4 {
/* main method */
public static void main(String[] args) {
double mi; //used to catch the return value from method m(i)
System.out.println("i\t\tm(i)\n");

//Print the table
for (int i = 1; i <= 20;i++) {
mi = m(i); //Fill in blank here. Call the method
System.out.println(i+"\t\t"+ mi);
//System.out.println(mi);
} //end for

} //end main

/* define non-void method m(i) here */
public static double m(double key){
double sum;
double i=1;
key = (i/(i+1));
sum =+ key + (i/(i+1));
i++;
//System.out.println(key);
return key;
}

I've been using my imagination and i'm out of ideas...

16. That mi() method is just rubbish now: you create local variables i and sum at every method call and assign them the same values over and over again. Those local variables are lost when the method termintas.

Here is the (spoonfed) solution because this thread has been going on for too long already:

Java Code:
double sum= 0.0; // contains m(1)+m(2)+ ... m(i)
for (int i= 1; i <= 20; i++) {
sum+= m(i); // add the next term to the sum
System.out.println(i+": "+sum)); // print partial result
}
Use the following version of m(int i) for this:

Java Code:
double m(int i) {
return i/(i+1.0);
}
kind regards,

Jos
Last edited by JosAH; 11-04-2009 at 04:51 PM.

17. Member
Join Date
Nov 2009
Posts
11
Rep Power
0
Jos,

Thank you for being so patient with me, i greatly appreciate it.

Regards

18. Originally Posted by mnki23
Jos,

Thank you for being so patient with me, i greatly appreciate it.

Regards
You're welcome of course; you really should show a bit more rational creativity if you want to continue programming; compare my code and your code for an example and try to understand my code and reuse the ideas in it for your further code.

kind regards,

Jos

19. Member
Join Date
Nov 2009
Posts
11
Rep Power
0
Yes i've been playing around with your code to see how i can make it work but with different variables and such...I really made it harder then it needed to be.

20. Originally Posted by mnki23
Yes i've been playing around with your code to see how i can make it work but with different variables and such...I really made it harder then it needed to be.
Yep, I am extremely lazy: do just what has been asked for, no more and no less. b.t.w. try to change the '1.0' to '1' in the m() method (see above) and see what happens and try to understand it; it never hurts to play with the code and temporarily ruin it a bit occasionally ;-)

kind regards,

Jos

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•