English

Write a program to accept the year of graduation from school as an integer value from the user. Using the binary search technique on the sorted array of integers given below, output the message - Computer Applications

Advertisements
Advertisements

Question

Write a program to accept the year of graduation from school as an integer value from the user. Using the binary search technique on the sorted array of integers given below, output the message "Record exists" if the value input is located in the array. If not, output the message "Record does not exist".

Sample Input:

n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
1982 1987 1993 1996 1999 2003 2006 2007 2009 2010
Answer in Brief

Solution

import java.util.Scanner;
public class Q13
{public static void main(String[] args)
{
Scanner sn = new Scanner(System.in);
int n[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
int year, flag = 0, mid,1b = 0, ub = n.length - 1;

System.out.println("Please enter the year of graduation to be searched");
year = sn.nextInt();

    do
    {
    mid = (lb + ub) / 2;

    if (year < n[mid])
ub = mid − 1;

    else if (year > n[mid])
    Ib = mid + 1;

    else
    {flag = 1;
System.out.printIn("Record exists");
    break;
}} while (lb <= ub);
if (flag == 0)
System.out.printIn("Record does not exist");
}}
shaalaa.com
  Is there an error in this question or solution?
Chapter 3: Arrays (Single Dimensional and Double Dimensional) - EXERCISES [Page 242]

APPEARS IN

Avichal Computer Applications [English] Class 10 ICSE
Chapter 3 Arrays (Single Dimensional and Double Dimensional)
EXERCISES | Q VII. 13. | Page 242
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×