



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Code is written in java to perform 1D traversal
Typology: Lecture notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Input: import java.util.Scanner; public class arrayTraversal { // code to implement method Largest public static int Largest(int[] a) { int max = 0; for (int i = 0; i < a.length; i++) { if (a[i] > max) { max = a[i]; } } return max; } // code to implement method Difference public static void Difference(int[] a) { int max = Largest(a); for (int i = 0; i < a.length; i++) { System.out.println(max - a[i]); } } // code to implement method searchKey public static void searchKey(int[] a) { Scanner keyboard = new Scanner(System.in); int key; boolean found = false; int location = -1; System.out.println("Enter the key: "); key = keyboard.nextInt(); for (int i = 0; i < a.length; i++) { if (a[i] == key) { found = true; location = i; } } if (found) {
System.out.println("key is in index location:" + location); } else { System.out.println("key is at index location:" + location); } keyboard.close(); } // code to implement method Update public static int[] Update(int[] a) { int[] douArray = new int[a.length]; for (int i = 0; i < a.length; i++) { douArray[i] = 2 * a[i]; System.out.println(douArray[i]); } return douArray; } public static int Menu() { Scanner keyboard = new Scanner(System.in); System.out.println(" \t 1. Finding the largest value (max) in an array \n"
4.Update data times two Make a selection or 0 to end processing 1 Max: 55