Hi the code to answer exercise 61 is below However it is inc

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\");
}

Solution

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\");
}

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site