# Ass1 - Name: Choucri Philippe Bechir ID #: 260345209...

This preview shows pages 1–3. Sign up to view the full content.

Name: Choucri Philippe Bechir ID #: 260345209 Course: COMP 250 Assignment: 1 1- I am not assuming that the search algorithm has already been written for us. Algorithm : listIntersection(a, m, b, n) Input: An array a of m strings and an array b of n strings. The elements of a are assumed to be distinct. The same is true for b. Output: The number of elements present in both a and b and an array c which contains the values of the elements that are in common. string [] c = new string [m ] /* I don’t know which of a or b is the smallest array but any ways we can’t have more than m or n common elements. So I initialized the array c to m elements (it could have been n too) */ int h 1 // h is the index of the array c int i 1 int j int intersect 0 for i 1 to m do j 1 for j 1 to n do if a[i]==b[j] then // if not, the j is incremented intersect intersect +1 c[h] a[i] h h+1 break return intersect, c ; 2- Algorithm : listIntersectionDup(a,m,b,n) Input: An array a of m elements and an array b of n elements. The elements of a might not be distinct. The same is true for b. Output: An array c containing triplets. Each triplet refers to a common element with the numbers of times it occurs in each of the two arrays and has the following form (e,f,g) what i'm gonna do is create triplets that will store in them the common value, with the number of repetitions for each array. The triplet will be of this form : (e,f,g). First we will look in the first array. We pick up the element that's in the first index and see if the same exists in the second array (we search, until we find the same one). If found, we first check if it's not already in the array c. If it's not, we put it in the first part of the triplet. (part e) Then we start to count how much time it appears in the first array. we store this int in the second part of the triplet. After that, we see how much time it appears in the second array and store the value in the third part of the triplet.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Then we put the triplet inside the array c which is in charge of storing all the triplets. After that we go to the second index of the first array and we repeat the same steps. (we have to make sure that the next common element is not an element which has been accounted for previously ie. we have to make sure that it's not the same as another element which is already in a triplet.) And we do thing until we hit the last index of the first array. triplet [] c = new triplet [m] /* I am creating an array which can contains triplets. Each triplet will store in it the common value and how many times it appears in each array. Like before, I don’t know which of a or b is the smaller array but any ways we can’t have more than m or n common elements. So I initialized the array c to m elements (it could have been n too) */ int q 1 // this is the int that I will use for the indexes of c string intersect Boolean stored true for i1 1 to m do // i1 is the index variable for a for
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/02/2010 for the course COMP 250 taught by Professor Blanchette during the Spring '08 term at McGill.

### Page1 / 8

Ass1 - Name: Choucri Philippe Bechir ID #: 260345209...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online