ArraySum Method to transform a number of an array Assume that we receive two numbers with the
Question:
ArraySum
Method to transform a number of an array
Assume that we receive two numbers with the same number of digits. We should transform each number into an array of integers that represent the given number such that each element is a digit in the array. example: given number:
1234->[1,2,3,4]
InArraySum.javaimplementthisfunctionalityinsidethetransformmethod.
int[]transform(intnum)
1b)Methodtocomputethesumoftwoarraysthatrepresentaverybignumber.
Assumeyouhavetwoarraysofintegersthatrepresenttwoverybignumbersasexplainedaboveand shown as follows:
Atthisstep,weshouldtransformthetwobignumbersintotwoarrays. First:
transform(1234)->awhichis[1,2,3,4]
transform(1211)->bwhichis[1,2,1,1]
Weknowthataandbrepresent1234and1211respectively.
Now,completethefollowingmethodinArraySumclassthatcomputesthesumoftwogivenarrays.
Note:Youarenotallowedtotransformthearraysintointegersandthencomputethesumsincethe numbers will be very large numbers and you cannot store them into simple primitive data types.
Hint:Inmath,wealwaysstartfromthelastdigitintheright(indexi).Inthecaseofhavingacarry,we should include it in the sum calculation of the next digits (index i - 1).int[] sum(int[] a, int[] b)
Testcase 1:
a->[1,2,3,4]
b->[1,2,1,1]
sum(a,b)->sum([1,2,3,4],[1,2,1,1])
Theresultis:[2,4,4,5]
Testcase 2:
a->[1,2,3,4]
b->[9,2,9,5]
sum(a,b)->sum([1,2,3,4],[9,2,9,5])
Theresultis:[1,0,5,2,9]
Note:You are not allowed to useArrayList or other data structures that we have not covered in the class in order to solve this question.
Question2:TMax (40points)
2a)Generatearandomarray
InTMax.java,completethegetRandomArray()methodthatisresponsibletogenerateaonedimensional random array with the size of a random number between 2 and 10. Then populate the Array with some random real values from [0, 1].
Note:youcansettheseedbyusingtheargsinthemainmethod.
Outputexamples:
Theoutputresultsmightbevarieddependsontheseedthatyouareusing.
getRandomArray() -> [0.123, 0.235, 0.3573, 0.657]
getRandomArray() -> [0.067,0.235,0.657]
2b)Findthethirdbiggestnumberinthearray
Then, complete thirdBiggest(int[] array). This method should accept an array that we generated above and return the third biggest number in this array. In the case of having less than three numbers that are bigger than the rest of the elements, you should return the maximum.
Note:Youarenotallowedtousethepredefinedsortmethodinjavaorothersorting algorithms.
Testcase 1:
a -> [0.123, 0.235, 0.3573, 0.657]
thirdBiggest(a)->0.235
Testcase 2:
a->[0.067,0.235]
AsexplainedaboveinthiscaseweshouldreturntheMaximumnumber.Therefore:
thirdBiggest(a)->0.235
Question3:Rotatea2DArray (30points)
InRotate.java,completethemethodofrotate180.Themethodreceivesa2Darrayofintegersandrotatesthegiven 2D array in 1800 degrees (clockwise).
Assumematrix"a"representa2Darrayasfollows:
1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 |
Afterrunningtherotate180(a)thematrixshouldbechanged(in-place)suchthatithas180degreesrotationof elements.
Therefore,afterrunningrotate180(a)thematrix"a"willchangetothefollowingmatrix:
25 | 24 | 23 | 22 | 21 |
20 | 19 | 18 | 17 | 16 |
15 | 14 | 13 | 12 | 11 |
10 | 9 | 8 | 7 | 6 |
5 | 4 | 3 | 2 | 1 |
WhatToSubmit
Please put all your files in a folder called Assignment3. Zip the folder (DO NOT RAR it) and submit it through MyCourses.
Insideyourzippedfolder,theremustbeonlythefollowingfiles.Donotsubmitanyotherfiles,especially
.classfiles.Anydeviationfromtheserequirementsmayleadtolostmarks.
ArraySum.java TMax.java Rotate.java
Confession.txt(optional)Inthisfile,youcantelltheTAaboutanyissuesyouranintodoingthis assignment.
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest