subject

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

ansver
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 21:30, elsauceomotho
Im doing this last minute and literally none of my neighbors or people that my dad works with use excel so if anyone could me make up an example
Answers: 1
image
Computers and Technology, 23.06.2019 00:40, QueenKy6050
Consider the following statements: struct nametype{string first; string last; }; struct coursetype{string name; int callnum; int credits; char grade; }; struct studenttype{nametype name; double gpa; coursetype course; }; studenttype student; studenttype classlist[100]; coursetype course; nametype name; mark the following statements as valid or invalid. if a statement is invalid, explain why. a.) student. course. callnum = "csc230"; b.) cin > > student. name; c.) classlist[0] = name; d.) classlist[1].gpa = 3.45; e.) name = classlist[15].name; f.) student. name = name; g.) cout < < classlist[10] < < endl; h.) for (int j = 0; j < 100; j++)classlist[j].name = name; i.) classlist. course. credits = 3; j.) course = studenttype. course;
Answers: 1
image
Computers and Technology, 23.06.2019 01:40, hanjonez
Writing a modular program in visual c++. i am new to this and not sure what i am missing. i am getting the following error: baddate. cpp: in function ‘int main()’: baddate. cpp: 50: 3: error: ‘else’ without a previous ‘if’elsehere are the instructions and code: writing a modular program in c++in this lab, you add the input and output statements to a partially completed c++ program. when completed, the user should be able to enter a year, a month, and a day. the program then determines if the date is valid. valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31.notice that variables have been declared for you. write the simulated housekeeping() function that contains the prompts and input statements to retrieve a year, a month, and a day from the user. include the output statements in the simulated endofjob() function. the format of the output is as follows: month/day/year is a valid date. ormonth/day/year is an invalid date. execute the program entering the following date: month = 5, day = 32, year = 2014. record the output of this program. execute the program entering the following date: month = 9, day = 21, year = 2002. record the output of this /* program name: baddate. cppfunction: this program determines if a date entered by the user is valid. input: interactiveoutput: valid date is printed or user is alerted that an invalid date was entered*/#include bool validatedate(int, int, int); using namespace std; int main(){// declare variablesint year; int month; int day; const int min_year = 0, min_month = 1, max_month = 12, min_day = 1, max_day = 31; bool validdate = true; // this is the work of the housekeeping() method// get the year, then the month, then the daycout< < "enter the year"< > year; cout< < "enter the month"< > month; cout< < "enter the day"< > day; // this is the work of the detailloop() method// check to be sure date is validif(year < = min_year) // invalid yearvaliddate = false; else if (month < min_month || month > max_month) // invalid monthvaliddate = false; else if (day < min_day || day > max_day) // invalid dayvaliddate = false; // this is the work of the endofjob() method// test to see if date is valid and output date and whether it is valid or notif(validdate == true); {// output statementcout<
Answers: 1
image
Computers and Technology, 23.06.2019 05:20, reeeeeee32
What did creator markus “notch" persson initially call his game
Answers: 1
You know the right answer?
The method countSorted is intended to find the length of the longest consecutive block of sorted val...

Questions in other subjects:

Konu
Mathematics, 28.01.2020 03:31
Konu
Mathematics, 28.01.2020 03:31