Question

hi I need to understand how the out put become -4 in first question and out...

hi I need to understand how the out put become -4 in first question and out put 310 for the second question please can you explain to me thanks a lot


1.(ch12. Sc16. P. 820. loop) Consider the following code:

import java.util.*;
public class MyLoopOneSGTest{
   public static void main(String[] args){
     int w = 3;
     System.out.println(myLoop(w));
   }
   public static int myLoop(int n){
      int x = 1;
      for (int i = 1; i < n; i++){
         x = x - i;
      }
      x-=2;
      return x;
   }
}

What is the output of the above code?

Answer: -4

  1. (ch. 13. binary search)

Consider the following class:

public class MyBinarySGDemo {
   public static void main(String[] args){
      int[] a = {2, 4, 5, 6, 9, 10, 12};
      int target = 2;
      mystery(a, target);
   }
   public static int mystery(int[] a, int target){                              
      int min = 0;
      int max = a.length - 1;    
      while (min <= max) {
         int mid = (max + min) / 2;
         System.out.print(mid);
         if (a[mid] == target) {
            return mid;     
         } else if (target > a[mid]) {
            min = mid + 1;  
         } else {   
            max = mid - 1;  
         }
      }       
      return -1;   
   }
}
What is the output of the above code?

Answer:  310

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

1)

Initially x = 1;

for i=1; 1<3 true

x = 1 - 1 = 0

for i=2; 2<3 true

x = 0 - 2 = -2

for i=3; 3<3 false

x = -2-2 = -4

2)

It is a binary search

Initially min = 0, max = 6

while 0<6: true

mid = (6+0)/2 = 3

prints 3

a[3] == 2: false

max = 3-1 = 2

while 0<2: true

mid = (2+0)/2 = 1

prints 1

a[1]==2: false

max = 1-1 = 0

while 0<=0: true

mid = (0+0)/2 = 0

pirnts 0

a[0] ==2: true

return 0

So, finally prints 310

Add a comment
Know the answer?
Add Answer to:
hi I need to understand how the out put become -4 in first question and out...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Teacher Instructions: Create the binarySearch() method for String arrays from the downloaded version for ints so...

    Teacher Instructions: Create the binarySearch() method for String arrays from the downloaded version for ints so the program can find String. Wrap a main around it, of course, so that we can see how it works. Provided Code: // Returns the index of an occurrence of target in a, // or a negative number if the target is not found. // Precondition: elements of a are in sorted order public static int binarySearch(int[] a, int target) { int min =...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

  • Write merge method for mergeSort method, it takes the array of data, starting index of first...

    Write merge method for mergeSort method, it takes the array of data, starting index of first half, starting index of second half and end index of second half. please use the code below to test it public class A4Sort{ public static void mergeSort(int[] a, int l, int h){ if (l >= h) return; int mid = (h + l) / 2; mergeSort(a, l, mid); mergeSort(a, mid + 1, h); merge(a, l, mid + 1, h); } public static void main(String[]...

  • USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...

    USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...

  • 6. (4 points) The following code for insertion sort has been modified to print out the...

    6. (4 points) The following code for insertion sort has been modified to print out the sorted component of the array during the sort. What will be the output of running the main method of the following code? Try to trace by hand and then verify by executing the code public class Insertion ( public static void sort (String[] a) f int n- a.length; for (int i-1; i < n; i++) f for (int j i; j 0; j--) if...

  • plz solve Modify binary search so that it always returns the element with the smallest index...

    plz solve Modify binary search so that it always returns the element with the smallest index * that matches the search element (and still guarantees logarithmic running time). import java.util.Arrays; public class BinarySearch2 { public static int rank(int key, int[] a) { // Array must be sorted. int lo = 0; int hi = a.length - 1; while (lo <= hi) { // Key is in a[lo..hi] or not present. int mid = lo + (hi - lo) / 2;...

  • What does this program print? package javaapplication210; public class JavaApplication210 { public static void main(String[]args){ System.out.printf("Result...

    What does this program print? package javaapplication210; public class JavaApplication210 { public static void main(String[]args){ System.out.printf("Result is: % d/n", mystery (-5, -9));//System.out.printf("Result is: % d/n", mystery (-4, -8));//System.out.printf("Result is: % d/n", mystery (-6, -7));//} public static int mystery(int a, int b) { if(b - 1) returns a; else return a + mystery(a, b + 1); } }

  • Can someone explain me parts of this code I do not fully understand what its doing...

    Can someone explain me parts of this code I do not fully understand what its doing or what its purpose is. I have highlighted the parts I'm confused over. Please explain thoroughly. import java.util.Scanner; class A1Stats { public static void main(String args[]) { double min, max, sum, mean; int n; Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); String[] numbers = input.split(" "); double value[] = new double[numbers.length]; if(numbers.length > 0){ for (int i = 0; i < numbers.length;...

  • Java i know that result will be 415. Can you explain how it works. step by...

    Java i know that result will be 415. Can you explain how it works. step by step please. thanks public static void whatsPrinted(int A[]) { for (int i=1; i<A.length; i++) { A[i]=A[i-1]*2+1; } } public static void main(String args[]) { int A[] = {12,3,8,9,7,11}; whatsPrinted(A); System.out.println(A[A.length-1]); }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT