You are on page 1of 3

import java.util.

Scanner;

public class Main {

// (a) Declare the global 1D array, PRICE, with ten elements. Populate the array with the given data
above.

static int[] PRICE = {50, 100, 50, 120, 65, 120, 90, 24, 78, 200};

public static void main(String[] args) {

// (e) Edit the main program to:

// • call display procedure

DISPLAY();

// • allow the user to input an integer value

Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer value to search: ");

int searchValue = scanner.nextInt();

// • pass the value to linearSearch() as the parameter

boolean found = linearSearch(searchValue);

// • output an appropriate message to tell the user whether the search value was found or not

if (found) {

System.out.println("The value " + searchValue + " was found in the array.");

} else {

System.out.println("The value " + searchValue + " was not found in the array.");

// • call bubble sort

Bubblesort();

System.out.println("Array after bubble sort:");

DISPLAY();
}

// (b) Write a procedure called DISPLAY to output all the elements in the array.

static void DISPLAY() {

System.out.println("Array elements:");

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

System.out.print(PRICE[i] + " ");

System.out.println();

// (c) A function, linearSearch(), takes an integer as a parameter and performs a linear

// search on array PRICE to find the parameter value. It returns True if it was found and

// False if it was not found.

static boolean linearSearch(int value) {

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

if (PRICE[i] == value) {

return true;

return false;

// (d) Write a procedure Bubblesort() using the bubble sort algorithm.

static void Bubblesort() {

int n = PRICE.length;

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (PRICE[j] > PRICE[j + 1]) {

// Swap elements

int temp = PRICE[j];


PRICE[j] = PRICE[j + 1];

PRICE[j + 1] = temp;

You might also like