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");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.