Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, the code to answer exercise 6.1 is below. However, it is incorrect because \

ID: 3924602 • Letter: H

Question

Hi, the code to answer exercise 6.1 is below. However, it is incorrect because "The incorrect value 7 was found in variable loopvar (should be 8)." Please edit the code below so that the value 8 is found in variable "loopvar".

for (loopvar = 0; loopvar < arrayme.length; loopvar++) {
   if (arrayme[loopvar] == targetval) {
      slot = loopvar+1;
         break;
   }
}

if(loopvar == arrayme.length) {
   System.out.println("Search element was not found");
}   
else {
   System.out.println("else");
}

Explanation / Answer

Hi, Please find my code.

You shouls start from varloop = 1

Please let me know in case of any issue.


for(loopvar=1; loopvar <= arrayme.length; loopvar++){
  
   if(arrayme[loopvar-1] == targetval){
       break;
   }
}

if(loopvar > arrayme.length) {
System.out.println("Search element was not found");
}   
else {
System.out.println("Search elemnet found at "+loopvar+
                    ", and it took "+loopvar+" comparisons");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote