B. For larger datasets, a linear search may be inadequate or perhaps inefficient. What other search method can be employed to return the maximum number from a set of elements in an array. Explain your answer. [5 marks]
C. As the lead system engineer at AIT, you have been given raw data for hundred (100) students who sat for an entrance exam and you have been asked to write the pseudocode to summarize the results. For each student grade, it is written a “P” for pass and “F” for fail. Use indentation and line numbers to highlight the flow of the program:
Your pseudocode should address the following:
i. Prompt the user to enter each result one after the other
ii. Provide a count of the results of each type
iii. Summarize the results indicating the number that passed or failed
[5 marks]
// some changes
b) For larger datasets, a linear search can be uneficcient, so we can use the binary search to return the maximum number from a set of elements in an array. It can obtain the better performing time then the linear search due to dividing the task into the smaller subtasks.
A binary search usually uses a sorted list. In this case we don't need the search of the biggest element. But if the array is not ordered, we can use the next algorithm.
This algorithm uses a divide and surrender technique.
Since the problem of finding the largest number in an array is complex, this algorithm breaks down the problem into smaller, simpler subproblems.
In the case where the length of the array is two, we simply compare the elements and return the largest.
But if the length of the array is greater than two, we create n subarrays of length n - 1, where we remove each element in turn from the old array to create n new arrays, and then we recursively run the algorithm on these new arrays.
c)
// pseudocode
START
INIT passed = 0, failed = 0, i = 0, entered = 0, mark ="", name ="";
WHILE entered != "s"
INPUT name;
INPUT entered;
IF entered >= 60
THEN
mark = 'T';
INCREMENT passed;
ELSE
mark = 'F';
INCREMENT failed;
ENDIF
DISPLAY entered+" "+mark;
ENDWHILE ;
DISPLAY "For the student "+name;
DISPLAY "passed exams (T) "+ passed ;
DISPLAY "failed exams (F) "+ failed ;
// some implementation on Java
public void summarize(Student s) {
try (Scanner scanner = new Scanner(System.in)) {
int passed = 0, failed = 0, i = 0;
while (scanner.hasNextDouble())
{if (scanner.nextDouble() >= 60) {
s.getExams().get(i).setMark("T");
passed++;
} else {
s.getExams().get(i).setMark("T");
failed++;
}
}
s.setPassedExams(passed);
s.setFailedExams(failed);
System.out.println("Passed exams (T): " +passed);
System.out.println("Failed exams (F): " +failed);
}}
Comments
Leave a comment