Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

what you are about to read is a copy paste of one attempt to help... but it did not work i got a message...'return' statement

what you are about to read is a copy paste of one attempt to help... but it did not work i got a message...'return' statement outside of function....i think the key is: use the variable i and the .length property to control the loop but i cant work it out.......................................................................................................................................... home / study / engineering / computer science / computer science questions and answers / The Incorrect Value 99 Was Found In Variable I (should Be 30) Your Code ( With Color Highlights ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: The incorrect value 99 was found in variable i (should be 30) Your Code ( with color highlights a... the incorrect value 99 was found in variable i (should be 30) Your Code ( with color highlights added ): for (int j = 0; j < arraydata.lenght ; j++ ){ if (arraydata[j]==targetvar){ i = j + 1; } } The Instructions: Exercise 6.1 : O(N) Loop A classic O(N) algorithm is linear search, where every item is looked at until the target is found Write a 'for' loop to search the array of integers arraydata, use the variable i and the .length property to control the loop Use an 'if' statement to find a match for the value in the variable targetvar When the loop terminates i MUST contain the number of comparisons to find the value That number must be +1 of the slot the item was found in, if the item was found in slot zero, it took one comparison to find it Do not declare i or initialize arraydata or targetvar it will be done for you. Write only a loop with an if inside and remember when the value was found in slot 3 it took 3+1 or 4 comparisons to find it Expert Answer joker1234 answered this Was this answer helpful? 0 0 313 answers the incorrect value 99 was found in variable i (should be 30) Your Code ( with color highlights added ): for (int j = 0; j < arraydata.lenght ; j++ ){ if (arraydata[j]==targetvar){ i = j + 1; } } The problem with above code is that,when arraydata might contain multiple instance of targetvar above problem might occur.You want to terminate the loop when first match is found. for e.g arradata={1,4,3,6,4,8} assume targetvar=4 two 4's first at slot 1 second at slot 4 so if you execute your above code you will get i= j+1 = 4+1 =5 i.e comparisons of last match but we want to terminate at first match so use below code: Note: since questions only asks to write ONLY a loop and dont initialize and declare i,arraydata,targetvar ,i have answered as required. code: for (int j = 0; j < arraydata.lenght ; j++ ) { if (arraydata[j]==targetvar) { i=j+1; return i; //terminating the loop here since first match is found } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is heritability?

Answered: 1 week ago

Question

1. Identify three communication approaches to identity.

Answered: 1 week ago

Question

d. Who are important leaders and heroes of the group?

Answered: 1 week ago

Question

3. Describe phases of minority identity development.

Answered: 1 week ago