Hi. Sorry, if this is a really easy to answer.

I need to find the smallest int result in a calculation between a fixed value and a variable. Then I need to return one of those variable. However, if there exists multiple smallest results, then I return a fixed value.

Here's sort of a pseudo-code that doesn't work because sometimes it returns the "fixed_value" even though it's not the smallest result.

It also doesn't return "fixed_value" when it should.

String[] variable = { num0 , num1 , num2 , num3 };

int multiple_smallest = 0;

for (int i = 0 ; i < s.length ; i++) {
	int result = calculation( fixed_value, variable[i] );
	if ( result < smallest ) {
		smallest = result;
		String variable = s[i];
	}
	else if ( dist == smallest ) multiple_smallest++;
}

if ( variable == s[0] ) variable = "some_value0";
else if ( variable == s[1] ) variable = "some_value1"; 
else if ( variable == s[2] ) variable = "some_value2"; 
else if ( variable == s[3] ) variable = "some_value3"; 
		
if ( multiple > 0 ) variable = "fixed_value";
		
return variable;

Recommended Answers

All 4 Replies

if ( variable == s[0] ) variable = "some_value0";
else if ( variable == s[1] ) variable = "some_value1"; 
else if ( variable == s[2] ) variable = "some_value2"; 
else if ( variable == s[3] ) variable = "some_value3";

I can't seem to figure out what you are doing here. If you are assigning the value that produced the smallest result to variable earlier then you just need to return the variable.

Also

if ( multiple > 0 ) variable = "fixed_value";

Shouldn't the multiple be multiple_smallest or is it fine because it is just a pseudo-code. But then how do you know it isn't working if this is just a pseudo-code.

So basically this should change to

if (multiple_smallest > 0)
    return fixed_variable;
else
    return variable;

You're right, it should be multiple_smallest.

I didn't want to post my full code because it's too long and confusing to explain so I made a pseudo-version of it.

For your first question, the variable is assign as any String value that I may want to change them to.

I didn't want to post my full code because it's too long and confusing to explain so I made a pseudo-version of it.

Writing code that only you can read is a bad habit to get into. However, you made the right decision to not post it because of the length.

I think it's mostly because his explanation doesn't make sense. Therefore the code doesn't make sense either. Lol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.