Computers and Technology

The method countSorted is intended to find the length of the longest consecutive block of sorted values in an array, where sorted means that the next element in the array is higher than or equal to the previous element in the array. For example, if the array arr contains the values [25, 7, 7, 14, 14, 14, 21, 3, 3, 3, 5, 12, 12, 13, 13], the call countSorted(arr) should return 8, the length of the longest consecutive block of sorted numbers: 3, 3, 3, 5, 12, 12, 13, 13.

However, the method countSorted that you can see below does not work as intended.

Line 1: int countSorted(int[] array){
Line 2: int count = 1;
Line 3: int max = 1;
Line 4: for (int k = 1; k < array. length; k++) {
Line 5: if (array[k-1] <= array[k]) {
Line 6: count++;
Line 7: } else {
Line 8: if (count > max) {
Line 9: max = count;
Line 10: }
Line 11: count = 1;
Line 12: }
Line 13: }
Line 14: return max;
Line 15: }
Line 16: int [] arr = {25, 7, 7, 14, 14, 14, 21, 3, 3, 3, 5, 12, 12, 13, 13};
Line 17: System. out. println(countSorted(arr));

Enter the value printed on screen by this code segment (Line 17) ??

Which of the following changes should be made in the code so that the method works as intended?

a. It should return count instead of max
b. Before line 14, it should check if count is greater than max and, in that case, do max = count;
c. Before line 14, it should check if count is greater than max and, in that case, do max = count+1;
d. It should return max +1

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 06:40, sardarp1irc5
What are the three uses of a screw?
Answers: 2
image
Computers and Technology, 23.06.2019 12:40, Emilyvite6251
According to the video what are some tasks petroleum engineers perform check all that apply
Answers: 2
image
Computers and Technology, 23.06.2019 21:30, mariah10455
Write a fragment of code that reads in strings from standard input, until end-of-file and prints to standard output the largest value. you may assume there is at least one value. (cascading/streaming logic, basic string processing)
Answers: 3
image
Computers and Technology, 23.06.2019 22:30, meijorjay94p2u2zy
Apart from confidential information, what other information does nda to outline? ndas not only outline confidential information, but they also enable you to outline .
Answers: 1
Do you know the correct answer?
The method countSorted is intended to find the length of the longest consecutive block of sorted val...

Questions in other subjects:

Konu
Biology, 17.10.2019 04:00