Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
I get the error, "The left-hand side of an assignment must be a variable" for this line, "(addition + n) = answer." I am trying to prompt for n and add 1 to it, then two, then three, and so on until all numbers up to n are added.

Code:
// A program to prompt for n, and then
// add all the numbers from 1 to n.
// The program should also calculate
// n(n+1)/2 and print both values. 

package loops;
import java.util.Scanner;
public class Addition
{
	static double slow(double n){
		
		double addition = 1; // number to add to n and increases by one each time until above n
		double answer; // answer
				
		//loop condition
		while (addition <= n) {
			return ((addition + n) = answer);
		}
	}
	
	static double fast(double n){
		return (n * (n + 1) / 2);
	}


	public static void main(String [] args) {
		
		Scanner sc = new Scanner (System.in);

		int n;

		//user input
		System.out.print("n = ");
		n = sc.nextInt();

		System.out.println("Slow way answer is: " + slow(n));
		System.out.println("Fast way answer is: " + fast(n));
	}

}
 

Cromulent

macrumors 604
Oct 2, 2006
6,816
1,101
The Land of Hope and Glory
Just turn it around. You need to have a variable on the left hand side so for instance if you want to add 10 to the variable number and put the result in the variable result you would do:

Code:
result = number + 10;
 
Also, something for you to think about soccersquirt82....

Why do you have both the slow and fast methods accepting a double as a parameter, and also returning a double?
Is that really required?

UPDATE: Also, you need to examine what your code is trying to do in 'slow'

Code snipped from there...
Code:
		//loop condition
		while (addition <= n) {
			return ((addition + n) = answer);
		}

So, if n was 5, you are trying to get 5+4+3+2+1 aren't you.
Given that addition is 1, and answer is undefined.... run through that loop and what do you think is going to happen?
The first thing that is wrong (and immediately tells you the code is not right), is that the loop is only ever going to run once.
You've defined a answer variable, which is presumably going to keep a 'running total' which should be used to return ONLY AFTER the while loop has finished.

Right, hopefully that's not given too much away for you to start to work on it.
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
Thanks! A new error pops up saying, "This method must return a result of type double." Doesn't already return the type double?
 

Cromulent

macrumors 604
Oct 2, 2006
6,816
1,101
The Land of Hope and Glory
Thanks! A new error pops up saying, "This method must return a result of type double." Doesn't already return the type double?

Seeing as this for homework I'm not going to give a precise answer, but what is a double? Is the number you are returning a double? Or is it another type of number? Could you actually declare answer and addition as different types and still achieve the same result?
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
I have no errors, but I can't make it loop again and again. I tried debugging, but it's not working (the debugger will go into another window when I step into the slow method). Help!!

Code:
package loops;
import java.util.Scanner;
public class Addition
{
	static double slow (double n){
		
		double addition = 1; // number to add to n and increases by one each time until above n
		double answer = 0; //
				
		//loop condition
		while (addition <= n) {
			return (answer = n + addition);	
		}
		addition = addition + 1;
		return answer;
		}	
	
	static double fast (double n){
		return (n * (n + 1) / 2);
	}


	public static void main(String [] args) {
		
		Scanner sc = new Scanner (System.in);

		int n;

		//user input
		System.out.print("n = ");
		n = sc.nextInt();

		System.out.println("Slow way answer is: " + slow(n));
		System.out.println("Fast way answer is: " + fast(n));
	}

}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
return immediately returns from the function you're in, even if you're in a loop. What your fast function does is:
initialize addition to 1
initialize answer to 0
If the parameter n that is passed in is greater than 0,
--the function returns the value of n passed in + 1 (the value of addition).
else
--set addition to addition + 1, so addition equals 2
--return answer (which is 0)

So what you want to do is accumulate your answer in... answer. With each iteration of your while loop you need to do 2 things. Add the appropriate value to answer, and increment something else. You seem to know what to increment, you're just doing it in the wrong place.

Every time you write a loop, ask yourself this:
Is there something in this loop that will eventually change the condition to false. If not, you probably missed something. The variables involved in the condition are n and addition. The only thing you modify in your loop is answer, setting it to n + 1(initial value of addition) and immediately return it. Rethink what you need to do in your loop, and how you want to modify answer with each iteration.

In this case, you want to add all of the integers from 1 to n. You are unlikely to want to add n itself at any point.

Another thing: why doubles? Can anything ever be a decimal value? In fast you multiply n and n+1, then divide by 2. Since either n or n+1 must be even, this means that the product of the two will be even, so when you divide by 2 the result will always be a whole number. Seems like an int would do the job.

-Lee
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
I tried doing what you told me, but some of the stuff really doesn't make sense. The if n > 0 doesn't make sense because I do everything in the else statement even though n should be greater that 0. I also don't know how to store the next answer and then provide the real answer. Do I need another variable?


Code:
package loops;
import java.util.Scanner;
public class Addition
{
	static int slow (int n){
		
		int addition = 1; // number to add to n and increases by one each time until above n
		int answer = 0; //
						
		//loop condition
		while (addition < n) {
			if (n > 0) {
				return n + 1; }
			else {
				addition = addition + 1;
				return answer; }
		}
		return answer;
	}
	
	static int fast (int n){
		return (n * (n + 1) / 2);
	}


	public static void main(String [] args) {
		
		Scanner sc = new Scanner (System.in);

		int n;

		//user input
		System.out.print("n = ");
		n = sc.nextInt();

		System.out.println("Slow way answer is: " + slow(n));
		System.out.println("Fast way answer is: " + fast(n));
	}

}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I tried doing what you told me, but some of the stuff really doesn't make sense. The if n > 0 doesn't make sense because I do everything in the else statement even though n should be greater that 0. I also don't know how to store the next answer and then provide the real answer. Do I need another variable?


Code:
package loops;
import java.util.Scanner;
public class Addition
{
	static int slow (int n){
		
		int addition = 1; // number to add to n and increases by one each time until above n
		int answer = 0; //
						
		//loop condition
		while (addition < n) {
			if (n > 0) {
				return n + 1; }
			else {
				addition = addition + 1;
				return answer; }
		}
		return answer;
	}
	
	static int fast (int n){
		return (n * (n + 1) / 2);
	}


	public static void main(String [] args) {
		
		Scanner sc = new Scanner (System.in);

		int n;

		//user input
		System.out.print("n = ");
		n = sc.nextInt();

		System.out.println("Slow way answer is: " + slow(n));
		System.out.println("Fast way answer is: " + fast(n));
	}

}

Hm. You don't need that if you added in. Get rid of return in your while loop. Return will return immediately. This means the FIRST time it is hit in your while loop, the while ends and the function returns immediately. This is not what you want.

To accumulate in answer you can do something like:
Code:
answer = answer + somethingelse;
or
Code:
answer += somethingelse;

These are functionally equivalent. You are already doing this with addition. You need to figure out what you want to add to answer. Your
Code:
return answer;

outside of your while is good, and you want to make sure that's where your function ends.

-Lee
 
Lee pretty much explains what your remaining issues are, and what should be done... but if you are having problems understanding, then maybe(?) this will help.

Don't bother with the debugger, just manually walk through the code with a piece of paper.

Code:
static int slow (int n){
		
		int addition = 1; // number to add to n and increases by one each time until above n
		int answer = 0; //
						
		//loop condition
		while (addition < n) {
			if (n > 0) {
				return n + 1; }
			else {
				addition = addition + 1;
				return answer; }
		}
		return answer;
	}

OK, on your piece of paper, make a little table, with each of your variable's as column headings.
Code:
n    | addition      | answer
------------------------------
     |               |
Now, just run through the code in your head, as if it was being run on the computer.
Let's start with slow(n) being invoked with n=3.
So, on your piece of paper, write 3 in the 'n' column.
Now, when
Code:
int addition = 1; 
int answer = 0;
runs, stick 1 in the addition column, and 0 in the answer column.
Now, 'run' the next line in your head....
Code:
		while (addition < n)
Well, looking at your table on the piece of paper, looks like this is true, so we know to get inside the loop.

... OK, will leave the rest to you. But, remember what Lee said about what 'return' does.



Don't know if this will help, sometimes used to help me when I started programming years ago.
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
Hmmm. . . I tried not initializing the answer variable to answer = 0 because that messed things up. But now I get errors for "The local variable answer may not have been initialized" three times for answer. What do I do?

[/code]
package loops;
import java.util.Scanner;
public class Addition
{
static int slow (int n){

int addition = 1; // number to add to n and increases by one each time until above n
int answer; // number that adds to addition and will be the answer

//loop condition
while (n > 0) {
if (addition < n) {
answer = addition + answer;
addition = addition + 1;
}
return answer;
}
return answer;
}

static int fast (int n){
return (n * (n + 1) / 2);
}


public static void main(String [] args) {

Scanner sc = new Scanner (System.in);

int n;

//user input
System.out.print("n = ");
n = sc.nextInt();

System.out.println("Slow way answer is: " + slow(n));
System.out.println("Fast way answer is: " + fast(n));
}

}
Code:
 
Code:
//loop condition
while (n > 0) {
  if (addition < n) {
    answer = addition + answer;
    addition = addition + 1;
  }
  return answer;
}
return answer;
Why did you change the while loop conditional logic?
What you had before was correct (the while loop declaration at least).
The return answer inside the while loop is wrong, but it's currently what stops that while loop becoming an infinite loop.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Hmmm. . . I tried not initializing the answer variable to answer = 0 because that messed things up. But now I get errors for "The local variable answer may not have been initialized" three times for answer. What do I do?

[/code]
package loops;
import java.util.Scanner;
public class Addition
{
static int slow (int n){

int addition = 1; // number to add to n and increases by one each time until above n
int answer; // number that adds to addition and will be the answer

//loop condition
while (n > 0) {
if (addition < n) {
answer = addition + answer;
addition = addition + 1;
}
return answer;
}
return answer;
}

static int fast (int n){
return (n * (n + 1) / 2);
}


public static void main(String [] args) {

Scanner sc = new Scanner (System.in);

int n;

//user input
System.out.print("n = ");
n = sc.nextInt();

System.out.println("Slow way answer is: " + slow(n));
System.out.println("Fast way answer is: " + fast(n));
}

}
Code:
[/QUOTE]

Initializing answer is not the problem and is crucial. Put that back. [B]GavinT[/B] already said you need to change your loop condition back. After that, get rid of the if in your while. The statements in the if are correct, leave them. Get rid of the return in your while, it will return immediately as explained, preventing the loop from iterating.

It's close.

-Lee
Edit: [B]GavinT's[/B] advice on charting program flow on paper is very good advice.
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
The slow way answer is now 14 when n is 5. It should be 15. When n is 6, the slow way is 17 and it should be 21. Help!

Code:
package loops;
import java.util.Scanner;
public class Addition
{
	static int slow (int n){
		
		int addition = 1; // number to add to n and increases by one each time until above n
		int answer = 0; // number that is being returned
		
		//loop condition
		while (addition < n) {
			answer = n + addition;
			addition = addition + 1;
			}
		return answer + n;
		}
		
	static int fast (int n){
		return (n * (n + 1) / 2);
	}


	public static void main(String [] args) {
		
		Scanner sc = new Scanner (System.in);

		int n;

		//user input
		System.out.print("n = ");
		n = sc.nextInt();

		System.out.println("Slow way answer is: " + slow(n));
		System.out.println("Fast way answer is: " + fast(n));
	}

}
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
The slow way answer is now 14 when n is 5. It should be 15. When n is 6, the slow way is 17 and it should be 21. Help!

Code:
package loops;
import java.util.Scanner;
public class Addition
{
	static int slow (int n){
		
		int addition = 1; // number to add to n and increases by one each time until above n
		int answer = 0; // number that is being returned
		
		//loop condition
		while (addition < n) {
			answer = n + addition;
			addition = addition + 1;
			}
		return answer + n;
		}
		
	static int fast (int n){
		return (n * (n + 1) / 2);
	}


	public static void main(String [] args) {
		
		Scanner sc = new Scanner (System.in);

		int n;

		//user input
		System.out.print("n = ");
		n = sc.nextInt();

		System.out.println("Slow way answer is: " + slow(n));
		System.out.println("Fast way answer is: " + fast(n));
	}

}



OK, let's look at slow:
Code:
static int slow (int n){
		
		int addition = 1; // number to add to n and increases by one each time until above n
		int answer = 0; // number that is being returned
		
		//loop condition
		while (addition < n) {
			answer = n + addition;
			addition = addition + 1;
			}
		return answer + n;
		}

As GavinT suggested before, let's chart the execution when the argument, n, is 5.

:Enter the function, slow.
n=5

Initialize addition to 1 and answer to 0.
n=5, addition=1, answer=0

:enter while loop, check condition 1 < 5, true, so iterate once
:set answer to n + addition
n=5,addition=1,answer=6
:addition = addition + 1
n=5,addition=2,answer=6

:check loop condition, 2 < 5, true, so iterate
:set answer to n + addition
n=5,addition=2,answer=7
:addition = addition + 1
n=5,addition=3,answer=7

:check loop condition, 3 < 5, true, so iterate
:set answer to n + addition
n=5,addition=3,answer=8
:addition = addition + 1
n=5, addition=4,answer=8

:check loop condition, 4 < 5, true, so iterate
:set answer to n + addition
n=5,addition=4,answer=9
:addition = addition + 1
n=5,addition=5,answer=9

:check loop condition, 5 < 5, false, end while loop
:return answer + n, 9+5
14 is returned

Obviously this is not what we want. We want to accumulate some values in answer, not set it to the sum of other variables each iteration of the loop. answer needs to be on the left and right side of the = to do this. What do you want to add to answer with each iteration? You can change only this line:
Code:
answer = n + addition;

and this will work. It's a bit unconventional, because you're adding n in your return statement, but it will work. The alternative is for your loop condition to change to:
Code:
addition <= n

that way n should be added to answer during the loop, and you can just return answer.

-Lee
 
It's also wrong when n is 2 and 3...

The structure for what you want to do is almost there there though.

Code:
//loop condition
[I]while (addition < n) {[/I]
  answer = n + addition;
  addition = addition + 1;
}
[I]return answer + n;[/I]

Now, there is a one line fix, which will make your code work.
However, I would probably rework it a little bit as well, just out of personal preference.

So, the one line fix? Have a look at how 'answer' is set inside the while loop.
Lee already has said...
Code:
answer = answer + somethingelse;
You don't do that.
First time the loop runs you say, answer = 'n' + 1
Second time the loop runs, you say answer = 'n' + 2
Third time the loop runs, you say answer = 'n' + 3
etc etc.


.....
The code in italic form is what I'd probably change just because of my personal preference on what the return call returns.
 

soccersquirt82

macrumors 6502
Original poster
Mar 11, 2008
296
0
THANKS SO MUCH TO BOTH OF YOU! IT WORKS!! YAYY!

Code:
static int slow (int n){
		
		int addition = 1; // number to add to n and increases by one each time until above n
		int answer = 0; // number that is being returned
		
		//loop condition
		while (addition <= n) {
		  answer = answer + addition;
		  addition = addition + 1;
		}
		return answer;
		}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.