#how do I make this simple method work?
125 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @slow sentinel! Please use
/closeor theClose Postbutton 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.
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.
I just saw that you do that check at the very end. Why not add it right away then it's easier to read.
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.
cause doesnt it make it unreachable?
ok fixed the ints to doubles
this part i honestly dont understand how to write it properly
Moving the if up to the start of the method doesn't make the rest of it unreachable. The rest of the method will only be executed if endIndex >= startIndex.
You have the defined range. Your for loop should start at startIndex and end at endIndex-1 if endIndex is supposed to be exclusive.
like justthe if? indexstart? <= indexend?
but the return needs to be in the end?
sorry i dont understand java too well 😭
No, if indexEnd > indexStart terminate early and return 0.
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
57 and 58?
oop
wrong messge reply
oh i uess it wasnt
the line numbers
wasnt there
I cant see the line numbers
Its not it will lead to wrong results.
Now onto the for loop. In what range do you want to calculate the average ? Inclusive or exclusive endIndex ?
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
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.
ohh
And for your for-loop inclusive endIndex means <= endaindex not < endIndex
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
Pinging me is ok id prefer continuing here.
will do! thank yo so much for ur help so far!
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.
ok back @thorn cliff
alright so we are currently working on the for-loop correct ?
i believe so
so your for-loop should start at startIndex and iterate to and including endIndex
that's your condition. Your condition should be up to and including endIndex
where your for-loop starts and where it ends.
so it starts at the condition?
and ends there?
im so dumb bruh sorry its hard for me ot understand
it starts at indexStart and ends at (including) endIndex
can u give me an example
you can translate that sentence into code.
for(start;run Conditon;increment)
yeah sorry i just realised I worded it wrong
o
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
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
yeah but indexStart also needs to be equal to indexEnd since indexEnd is inclusive:
so what should the condition be?
do i need to do like if they equal each other
smth
indexEnd >= indexStart
that's the condition
damb i was cloes
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
what are you incrementing ?
what variable ? Are you incrementing indexStart or decrementing indexEnd ?
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()
yeah that looks correct.
now the sum part
that was the sum part, the for-loop is correct
then why dont it work 😭
now just calculate the average with the sum and the newLength
what are you returning ?
sum/newlength
could you show me the entire method as of now ?
are you sure the input is correct ?
after that small correction it should work
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.
you're welcome. Please close the post if you are done.
kk TY