0% found this document useful (0 votes)
41 views3 pages

Lab 6 Dsa

The document describes two lab tasks and a home task related to searching arrays in Java. The first task initializes an array with account balances and prints low balances. The second task searches an array using Arrays.binarySearch(). The home task prints the distinct values in an array along with their occurrence counts.

Uploaded by

Huzaifa Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views3 pages

Lab 6 Dsa

The document describes two lab tasks and a home task related to searching arrays in Java. The first task initializes an array with account balances and prints low balances. The second task searches an array using Arrays.binarySearch(). The home task prints the distinct values in an array along with their occurrence counts.

Uploaded by

Huzaifa Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

LAB # 06

Searching in a Linear Array

Object:
To find an element in linear array using Linear Search and Binary Search

Lab Tasks:
1. Declare an array of size 10 to store account balances. Initialize with values 0 to
1000000. Check all array if any value is less than 10000. Show message:
Account No .. Low Balance
Account No .. Low Balance

Code:
package Lab5;
public class Task1 {
public static void main(String[] args) {
int[] balances = new int[10];
balances[0] = 200;
balances[1] = 1000;
balances[2] = 3000;
balances[3] = 240000;
balances[4] = 15000;
balances[5] = 10;
balances[6] = 123456;
balances[7] = 0;
balances[8] = 400000;
balances[9] = 345678;

for(int i=0; i<balances.length; i++){


if(balances[i]<10000){
System.out.printf("Account No: %d Low Balance%d\n",i+1,balances[i]);
}
}
}
}

OUTPUT:
2. Write a program to search in array using Array built-in class.

Code:

package Lab6;
import java.util.*;

public class Task2 {


public static void main(String[] args) {
int[] arr = {8,1,4,2,6,9};
Scanner sc = new Scanner(System.in);
System.out.print("Enter no to search in array: ");
int search = sc.nextInt();
Arrays.sort(arr);
int index = Arrays.binarySearch(arr, search);
System.out.printf("%d found at %d",search, index);
}
}

OUTPUT:
Home Task:
1. Write a function called occurrences that, given an array of numbers A, prints all
the distinct values in A each followed by its number of occurrences. For example,
if A = (28, 1, 0, 1, 0, 3, 4, 0, 0, 3), the function should output the following five lines
(here separated by a semicolon) “28 1; 1 2; 0 4; 3 2; 4 1”.

Code:
package Lab6;
import java.util.*;

public class HomeTask1 {


public static void main(String[] args) {
int[] arr = {1,41,12,12,1,12,0,4,4,4};
System.out.println(Arrays.toString(arr));
getOccurrences(arr);

}
public static void getOccurrences(int[] numbers){
Arrays.sort (numbers);

int count = 0;
int start = 0;
int move = 0;

while(start < numbers.length){


for (int j = 0; j < numbers.length; j++){
int currentInt = numbers[start];;
if (currentInt == numbers[j])
{
count++; move++;
}
}
if(count == 1){
System.out.println(numbers[start] + " occurs " + count + " time ");
} else {
System.out.println(numbers[start] + " occurs " + count + " times ");
}
count = 0;
start = start + move; move = 0;
}
}
}

OUTPUT:

You might also like