Answer to Question #174296 in Algorithms for Kwame Hay

Question #174296

Question 4

A. Consider a problem to find the student who had the highest GPA for the 2020/2021 academic year.

i. Explain your choice of either a search or a sorting algorithm to solve this problem. [7.5 marks]

ii. Write the algorithm for your implementation. [7.5 marks]

B. Consider the analogy of a pile of books on a table. Using a diagram, explain the basic operations involved in adding a book to the pile and removing a book from the pile. [5 marks]

C. Use the code below to answer the questions that follow: [5 marks] public class CS204 { public static int linearSearch(int[] data, int target) { for (int i = 0; i < data; i++) { if (target == data[i]){ return i;

5

} } return -1; } public void main(String[] args) { int[] data = {3, 14, 7, 22, 45, 12, 19, 42, 6}; System.out.println("Search for 7: " + linearSearch(7)); } }

i. Why is method linearSearch declared static?

ii. Identify and resolve the errors in the code.


1
Expert's answer
2021-03-29T05:07:51-0400

A)

i) We will use searching technique to find the highest scoring student, the searching technique will perform better with respect to the sorting in the linear search with respect to time.

ii)

1.start
2.initiate sort =0 index = -1
3. for loop i=0 to total record
4.if sort>total record
5. sort=total record
6. repeat till total record
7. end.

B) For the pile of the table, there will two basic operation-

i) push operation ( to add into the pile)

ii) pop operation (to take out from the pile)



c) package quizTest;
public class CS204{
    public static int linearSearch(int[] data, int target) {
        for (int i = 0; i<data.length;i++) {
        if (target == data[i]){
            return i;
        }
    }
        
    
 return -1;
    }
    public static void main(String[] args) {
        int[] data = {3, 14, 7, 22, 45, 12, 19, 42, 6};
        System.out.println("Search for 7: " + linearSearch(data, 7));
        }
    }


i) It is declared as the static search because, if the data is constant during the search 
then this linear search operation can be perfomed.
ii) Here main function was not declared as static, so program compilation was not able to
iinitiate.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS