Archived

This topic is now archived and is closed to further replies.

MrPoopypants

Formulating a perfect square algorithm

Recommended Posts

public class FunLoops {
	//blah, these wont be used

        private long myint;
	private double myintsquared;
	private long mysum;

        //...

	FunLoops(){
		myint = 1;
		myintsquared = 1;
		mysum = 0;
	}

	public void magicSquare(int x) {
		int increment = 0;
		int sum = 0;
		for ( int i = 1; i <= x; i++ ){
			if ( i * i == sum ){
				System.out.println(i);
				continue;
			}else if(sum > 1){
				i++;
			}else{
				increment++;
				sum = sum + increment;
			}
		}
	}
}
The compiler bitches and complains if i put a return statement out of the if ( i * i == sum ) block. Changing this function to void doesnt help to much ... If anyone could give me a hand that would be great, thanks.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you dont need to return anything , you could just pass x by reference, void(int &x), and max x = to what ever you want and it will alter the variable instead of making a copy of the variable.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
you dont need to return anything , you could just pass x by reference, void(int &x), and max x = to what ever you want and it will alter the variable instead of making a copy of the variable.


Thanks, but i was looking for something which had to deal more with my algorithm itself

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Anonymous Poster
you dont need to return anything , you could just pass x by reference, void(int &x), and max x = to what ever you want and it will alter the variable instead of making a copy of the variable.


The poster''s language is Java. Java only has call-by-value.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by MrPoopypants

public class FunLoops {
//blah, these wont be used

private long myint;
private double myintsquared;
private long mysum;

//...

FunLoops(){
myint = 1;
myintsquared = 1;
mysum = 0;
}

public void magicSquare(int x) {
int increment = 0;
int sum = 0;
for ( int i = 1; i <= x; i++ ){
if ( i * i == sum ){
System.out.println(i);
continue;
}else if(sum > 1){
i++;
}else{
increment++;
sum = sum + increment;
}
}
}
}


The compiler bitches and complains if i put a return statement out of the if ( i * i == sum ) block. Changing this function to void doesnt help to much ...

If anyone could give me a hand that would be great, thanks.


The compiler probably bitches because not all execution paths terminate with a return statement in the case you mentioned. You can either throw a dummy return statement at the end of the function, or have a value that holds the return value and mutate it''s value where you would have placed a return in the original version. Then, after that assignment, place a break statement. The added return statement will return the assigned value.

Why does the compiler do this? Consider the case when x is 0. The comparison i <= x will always be false and the for-loop will never be executed. What is the return in this case? Your code doesn''t provide any options.

Share this post


Link to post
Share on other sites