Ask your own question, for FREE!
Computer Science 12 Online
OpenStudy (anonymous):

a) Design an algorithm that takes as input an array L1 of length n, where L1= a1, a2,…aN and an array L2 of length m, where L2 = b1, b2, … bM and returns a new array L3 of length n x m whose elements have the following values: L3= a1+b1, a1+b2…,a1+bM, a2+b1, a2+b2,…, aN+bM. For example, if the input to your algorithm is L1 = [1, 2, 4] and L2 = [1, 3, 5, 7], then the result should be [2, 4, 6, 8, 3, 5, 7, 9, 5, 7, 9, 11]. The number of givens is 4 (the 2 arrays and their lengths). You only have to show the algorithm that solves the problem. a) Design an algorithm that takes as input an array L1 of length n, where L1= a1, a2,…aN and an array L2 of length m, where L2 = b1, b2, … bM and returns a new array L3 of length n x m whose elements have the following values: L3= a1+b1, a1+b2…,a1+bM, a2+b1, a2+b2,…, aN+bM. For example, if the input to your algorithm is L1 = [1, 2, 4] and L2 = [1, 3, 5, 7], then the result should be [2, 4, 6, 8, 3, 5, 7, 9, 5, 7, 9, 11]. The number of givens is 4 (the 2 arrays and their lengths). You only have to show the algorithm that solves the problem. @Computer Science

OpenStudy (anonymous):

In python code: http://codepad.org/CTNs3Uvj One way to think about this problem is to use nested for loops: let L1[i] be the i-th element in L1, we fix the element, and iterate through k-th elements of L2, adding each of these elements to L1[i], then we increase i by 1 if and only if k = length_of_L2 (for other programming languages, you can use the given inputs for lengths here) with first_index <= i <= last_index of L1.

OpenStudy (anonymous):

If I were to draw a Visio diagram, how would it look though. And to convert it to java. That's my problem. I'm not all that strong with java.....

OpenStudy (anonymous):

I don't know about Java, but if there is no way to populate an array without explicitly looping over it, consider creating an empty array (called "temp" here, for later reference) and adding a third, outermost loop, for some j, with 0 <= j <= N*M, where N is the length of L1 and M is the length of L2. Then temp[0] = L1[first_index] + L2[first_index], temp[1] = L1[first_index] + L2[second_index], etc. I can code it in C/C++ if this would make it any easier.

OpenStudy (anonymous):

Ok. Thanks

OpenStudy (anonymous):

You know the size of the result array (L3), so create it. Loop over the elements in L1. For each element, loop over the elements in L2. Calculate the sum. Now you have to know where to put it in L3. Keep an index variable (created outside the loops, initialized to 0). In the innermost loop store the sum at that index, then increment the index.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!