0

Well, I am doing a practice problem (preparing for midterm) and I was able to get one of the outputs correct. However, I am having troublesome getting the average input. It ends up at 12.0 instead of 6.5

Here's the prompt question:5.

Complete the following Java program by filling in the bodies of functions sum(), avg(), and ord(). A call to sum(n) should return the sum of all the integers from 1 to n, while avg(n) returns the average of the same set of numbers. A call to the boolean function ord(x, y, z) returns true if x < y< z and false otherwise. The Function main() should produce the following output

Output:

6.5 true false

This is my code:

class Problem5 { // sum(): return 1+2+3+..+n static int sum(int n) { //this is given int sum = 0; for(int i=0; i<n; i++) { sum += n; } return n; } // avg(): return average of {1,2,..,n} static double avg(int n) { // given double sum = 0; for (int i=1; i<n; i++) { sum +=n; } return sum / n; } //ord(): return true if and only if x<y<z static boolean ord(double x, double y, double z){ //given if (x < y && y <z){ return true; } else { return false; } } public static void main (String[]args) { System.out.println(avg(12)); System.out.println(ord(1.2,3.4,5.6)); System.out.println(ord(3.4,1.2,5.6)); } } 

Overall I am having trouble coding/ filling in the code for static int sum(int) and static double avg(int).

7
  • this does not compile, as sum is not declared in avg. Commented Jan 29, 2015 at 20:22
  • There are a lot of things wrong with your indentation... Commented Jan 29, 2015 at 20:22
  • 1
    Fastest would be return n*(n+1)/2, but I think the goal is to understand coding, not maths... ;-) Commented Jan 29, 2015 at 20:24
  • as a side note, sum([1...n]) == n*(n+1)/2, avg([1...n]) == (n+1)/2 (no need for loop), (That is not related to programming, but rather to math) Commented Jan 29, 2015 at 20:24
  • "it gave me errors" What are the errors? Commented Jan 29, 2015 at 20:25

2 Answers 2

1

This:

for (int i=1; i<n; i++){ 

Will skip n. (it will loop on 1...n-1). For 12, the sum will be 11*12/2, which you then divide by 12, resulting in 11/2 = 6.5

Fix it like so:

for (int i = 1; i <= n; i++) { 

(or replace the whole loop by return (double) (n+1) / 2.0)

For your sum function, there is the same error, plus the return value is not good:

return n; 

Should be

return sum; 

And the increment should be sum += i;, not n (you want 1+2+3+4..., not 12+12+12+12...)

Again, you can replace the whole loop by return n * (n + 1) / 2

I assume your teacher would expect you to learn about re usability, and since your 2 loops in sum and in avg are identical, you could write:

public static double avg(int n) { return (double) sum(n) / n; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This made a lot sense to me. Thanks for pointing out my mistakes and explaining.
0

A sum is just the addition of all the numbers in a certain range:

static int sum(int n) { int total = 0; for(int i = 1; i <= n; i++) { total += i; } return total; } 

Average is just the sum of the range divided by the amount of numbers:

static double avg(int n) { return sum(n) / (double) n; } 

4 Comments

the total += 1; loop is priceless
I'm a Python programmer, by profession, just kidding I'm in high school. But why the downvotes, the code works fine?
it works, but my eyes are bleeding (you are right, though it does not mandate a downvote. sorry about that.)
I fixed it, how are your eyes?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.