Question

In: Computer Science

Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85....

Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85.

                                                                                                                                                                  

Q2.Write a Java program to find element 54 in array 45,41,65,53,76,90 using Binary Search. (Hint: Binary search is applied to sorted array elements)

Q4.   Write a java program to create array list subject

       - add English, Maths, Science to the list

       - add Computers at index 2

       - display first occurrence index of Maths

       - traverse the list using for each loop.

              

Q5. Write a java program to create an ArrayList of user-defined class forobjects (Employee): for id,name,age. The employee data are as follows for creating class objects.

1101    Aamna 25

1111    Amjad   30

Solutions

Expert Solution

public class SeqSearch {
        public static void main(String[] args) {
                int arr[]= { 10,20,35,45,55,65,75,85};
                int key=55;
                int res=linerSearch(arr,key);
                if(res==-1)
                        System.out.println(key+" does not exist in the array");
                else
                        System.out.println(key+" found at index "+res);
        }
        private static int linerSearch(int[] arr, int key) {
                // iterating through the array and checking if found returning the index
                for(int i=0;i<arr.length;i++)
                        if(arr[i]==key)
                                return i;
                return -1;
        }
}

As per policy we can answer 1 question per post. Please post the remianing questions as separate post.Thanks


Related Solutions

Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
Write a program that allows the user to search the array to find the number entered
Write a program that allows the user to search the array to find the number entered
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new element to an array.​ Change an element with the new one.​ Search for a particular element in the array.​ ​The code must have four separate methods for each task stated above.​ Do not use any pre-defined Java functions.​ You are free to use int or String data-type for the array.​
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Java Program Sequential Search You are given a sequence of n integers S and a sequence...
Java Program Sequential Search You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S. Do not use any import sort packages. Input: In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given. Output:...
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a program that will search through an array and check to see if the first...
Write a program that will search through an array and check to see if the first number in the array occurs more than once. If the first number occurs more than once, return true. Otherwise, return false. If the array is empty, return false? You will need to use a variable, loop, and if statement.
Write a method that displays every other element of an array. Write a program that generates...
Write a method that displays every other element of an array. Write a program that generates 100 random integers between 0 and 9 and displays the count for each number. (Hint: Use an array of ten integers, say counts, to store the counts for the number of 0s, 1s, . . . , 9s.) Write two overloaded methods that return the average of an array with the following headers:      public static int average(int[] intArray)        public static double average(double[] dArray) Also,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT