#how do I make this simple method work?

125 messages · Page 1 of 1 (latest)

slow sentinel
#

I'm trying to make it so the length is within the range given and itll add only those in the range

rugged templeBOT
#

This post has been reserved for your question.

Hey @slow sentinel! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

thorn cliff
#

You may also want to add an if that checks if the indexEnd is greater than index start and return 0 for that case as well.

#

and you are overriding your indexStart and indexEnd with the corresponding values in the array so the length is always calculated wrong.

thorn cliff
#

Your average variable is unnecessary you can go ahead and remove that. And define the variable sum as a double, currently you are doing integer division since both, sum and newLength are integers, you want to do floating point arithmetic.

#

Your for-loop is also incredibly confusing to reqd. You want to start at indexStart and end at indexEnd - 1 if the last index is not inclusive otherwise iterate until indexEnd.

slow sentinel
#

ok fixed the ints to doubles

slow sentinel
thorn cliff
thorn cliff
slow sentinel
#

but the return needs to be in the end?

#

sorry i dont understand java too well 😭

thorn cliff
#

No, if indexEnd > indexStart terminate early and return 0.

slow sentinel
#

so like this?

thorn cliff
#

Yeah now the first part of the method looks good. On to the second part. First, get rid of the reassignment of startIndex and endIndex

slow sentinel
#

oop

#

wrong messge reply

#

oh i uess it wasnt

#

the line numbers

#

wasnt there

thorn cliff
#

I cant see the line numbers

slow sentinel
#

indexStart = array something

#

thats not necessaru?

thorn cliff
#

Its not it will lead to wrong results.

slow sentinel
#

oh

#

ok got rid of that

thorn cliff
#

Now onto the for loop. In what range do you want to calculate the average ? Inclusive or exclusive endIndex ?

slow sentinel
#

System.out.println(averageInRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }, 2, 5));

#

should return 4.5

#

i think thats inclusive

#

but im not 100 percent

thorn cliff
#

Yeah that is inclusive. So we need to first fix how you calculate the newLength

#

You are currently calculating it as if endIndex is exclusive. You will need to add + 1 to be the correct length.

slow sentinel
#

ohh

thorn cliff
#

And for your for-loop inclusive endIndex means <= endaindex not < endIndex

slow sentinel
#

yo sorry can i dm u later im in a math class rn

#

i cant have my laptop right now LMAO

#

or ill ping u here in like an hour and half if thats ok

#

sorry BRBB

thorn cliff
#

Pinging me is ok id prefer continuing here.

slow sentinel
#

will do! thank yo so much for ur help so far!

rugged templeBOT
slow sentinel
#

ok back @thorn cliff

thorn cliff
#

alright so we are currently working on the for-loop correct ?

slow sentinel
#

i believe so

thorn cliff
#

so your for-loop should start at startIndex and iterate to and including endIndex

slow sentinel
#

so instead of array length its startIndex

#

or

thorn cliff
#

that's your condition. Your condition should be up to and including endIndex

slow sentinel
#

so was that wrong?

#

changing the lenth*

thorn cliff
#

where your for-loop starts and where it ends.

slow sentinel
#

so it starts at the condition?

#

and ends there?

#

im so dumb bruh sorry its hard for me ot understand

thorn cliff
#

it starts at indexStart and ends at (including) endIndex

slow sentinel
#

can u give me an example

thorn cliff
#

you can translate that sentence into code.
for(start;run Conditon;increment)

slow sentinel
#

oH

#

for(indexStart = 0;indexEnd > indexStart; indexStart++) {

thorn cliff
#

yeah sorry i just realised I worded it wrong

slow sentinel
#

o

thorn cliff
#

your logic was correct you just need to turn it around since it's the run condition not the end condition

#

your loop runs while the middle part is true

slow sentinel
#

so is it still wrong

#

idk i thought logically it makes sense

#

since it starts at 0 indexend will be greater

#

then itll count the range

#

of numbers

thorn cliff
#

yeah but indexStart also needs to be equal to indexEnd since indexEnd is inclusive:

slow sentinel
#

so what should the condition be?

#

do i need to do like if they equal each other

#

smth

thorn cliff
#

indexEnd >= indexStart

slow sentinel
#

oh

#

thats it?

thorn cliff
#

that's the condition

slow sentinel
#

damb i was cloes

thorn cliff
#

now we nee to look at how you defined the iterator:
You defined it as indexStart = 0. That's wrong, indexStart is already given, you shouldn't reassign it

slow sentinel
#

but it says invalid syntax

#

indexStart; index....

thorn cliff
#

just remove indexStart altogether

#

for(;...;..) is valid syntax.

slow sentinel
#

oh

#

okay got it

thorn cliff
#

what are you incrementing ?

slow sentinel
#

1?

#

or indexStart

#

++

thorn cliff
#

what variable ? Are you incrementing indexStart or decrementing indexEnd ?

slow sentinel
#

increasing

#

indexStart

thorn cliff
#

My preference is to name the incrementing/decrementing value first in the condition, that way you always now what variable is changing. So instead of
for(;endIndex >= startIndex; startIndex++) I would write for(;startIndex <= endIndex; startIndex++) or alternatively for(;endIndex >= startIndex; endIndex--) although I find it more natural for the startIndex to aproach the endIndex

#

but that's personal preference

#

and another thing I'd like to mention. Normally when working with a startIndex and an endIndex the endIndex is always exclusive.
For example in IntStream.range() or Arrays.copyOfRange. When the endIndex is included you would normally name the method something like averageOfRangeClosed that way the caller knows that the endIndex is included in the calculation e.g. IntStream.rangeClosed()

slow sentinel
#

😭 idek

#

like that?

thorn cliff
#

yeah that looks correct.

slow sentinel
#

now the sum part

thorn cliff
#

that was the sum part, the for-loop is correct

slow sentinel
#

then why dont it work 😭

thorn cliff
#

now just calculate the average with the sum and the newLength

#

what are you returning ?

slow sentinel
#

sum/newlength

thorn cliff
#

could you show me the entire method as of now ?

slow sentinel
#

its returning 0.0 instead of 4.5

thorn cliff
#

are you sure the input is correct ?

slow sentinel
#

wait

#

i had the signs flipped

#

on indexEnd < indexStart

#

oops

thorn cliff
#

after that small correction it should work

slow sentinel
#

yea!!!

#

alright thank u so much man

rugged templeBOT
# slow sentinel alright thank u so much man

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

thorn cliff
#

you're welcome. Please close the post if you are done.

slow sentinel
#

kk TY