COMP 250 Introduction to Computer Science Assignment1

Document Sample
COMP 250 Introduction to Computer Science Assignment1 Powered By Docstoc
					               COMP 250: Introduction to Computer Science
                             Assignment 1
                           Posted Wednesday, January 16, 2008
                                Friday, January 25, 2008
    This homework explores the notions of algorithm design and time complexity which we started dis-
cussing in class. The homework is worth 5% of your final grade for the course. The total number of
points on it is 50. Please submit the code through WebCT. You can submit the rest of the assignment
either through WebCT or in hard copy in class.

  1. [5 points] More on list intersections
     In the lecture notes, we discussed the problem of determining the number of common elements in
     two arrays containing no duplicates. Suppose now that we also want to have as output an array
     which contains the values of the elements that are in common (i.e. the names if the students).
     Change the pseudocode version of your choice to achieve this task.

  2. [45 points] Finding the most frequent element in an array

       (a) [30 points] Suppose that you are given an array of integers. Write an algorithm (in pseu-
           docode) which outputs the value occuring most frequently in the array, and the number of
           times it occurs. For example, if your input is the array:
           1 4 4 3 4 3 5 2 10 1
           your algorithm should output (4, 3). If there are ties, report the most frequent value which
           occurs first in the array.
       (b) [5 points] What is the time complexity of your algorithm? Justify your answer.
       (c) [10 points] Provide a Java implementation of your algorithm. You should call your class
           MostFrequentElement. Comment your code clearly. Here is a brief skeleton for your
           code:
           public MostFrequentElement {
               public static void main (String[] args) {
               // Here you will declare the array and initialize it
               //Now write your algorithm
               }
           }




                                                  1