• 8
• Locked
Every classical algorithm has been programmed for a long time. Still, tasks about algorithms are very valuable. At the very least, they make the programmer think. There's nothing complicated about the algorithm to find the maximum. So let's not dilly-dally: write a program that takes a positive integer n, takes n numbers, and then finds the maximum of them.
You can't complete this task, because you're not signed in.
ImDevin
Level 9, Old Town, United States
9 April, 04:06
12 tries!!! Initially, didn't understand the instruction correctly! the first N stands for a number, which represents how many times it will read the number from the keyboard.
Sinisa
Level 11, Banja Luka, Bosnia and Herzegovina
24 February, 10:54
The key question here is until when the program should ask for a user input? As it is stated it would run forever. There must be some condition when the loop would break, either to define the number of inputs or to define some keyword when the loop exits. The proposed solution does not properly calculate the output. Enter this: 5 6 7 71 9 19 235435 You'll get an output of 71.
Level 12, Iasi, Romania
5 December 2020, 07:53
i took a look in the solution they say is correct and i made 4 other versions, all worked perfectly but still it said is not correct. so you haft to make the code exactly as they did. who made this task is an "amazing" boss somewhere....
kar-fai chow
Level 8, Hong Kong, Hong Kong
18 November 2020, 22:43
will the input set have enough numbers for the initial N? e.g. if the first number is 999, will there be another 999 numbers to satisfy the number set? or do i have to handle that as well? My codes are running fine: handing the first N being negative, reading N more numbers, returning Max, handling all negative N numbers etc But apparently it is still not returning the maximum number CodeGym needs to be more specific on why I am failing This is getting to be a waste of my time
ъуъ
Level 9, Vilnius, Lithuania
29 September 2020, 20:16
29 tries.... although the code worked like a charm, couldn't pass the 5th requirement. Using MIN_VALUE fixed that, idk why... In case You take this approach don't forget to "import static java.lang.Integer.MIN_VALUE;"
Karas
Level 19, Tampa, United States
1 September 2020, 17:52
A little too confusing. Hint: use a for loop. make sure you maximum to Integer.MIN_VALUE and two if statements.
Chloe Dinh
Level 9, United States
31 August 2020, 05:41
1) read the first number ( N) from input. This number is just like when you ask a user how many number they want to have in the program. The program then takes the first number as the limit of the input numbers. For example, the first number is 5 then the program can read 5 more numbers (after the first number). 2) if N is greater than 0, then start the for loop to read the rest of numbers in input (don't forget the limit of number) and decided the maximum between those numbers. close the for loop and print the maximum. 3) maximum should be initialized by a lowest number so you can compare the maximum number with the number that your program read from the input and be sure it always less than that number and it can take the value of that number and assign to its.
Gil Allen
Level 8, Johnstown, United States
5 August 2020, 15:11
i tried using a for loop instead of the while - same code, but it didn't work. Instead of the while I had : for (int i = n; i < 2; i--) { same code
Brandon Leirer
Level 7, Keller, United States
8 June 2020, 21:16
The instructions were very convoluted.
Liew
Level 10
18 April 2020, 12:38
I am a retard, this question supposed to be easy :'( just set maximum to Integer.MIN_VALUE
andy 6473
Level 9, Bangalore, India
18 June 2020, 17:40
why?
Kent
Level 9, Salt Lake City, United States
1 July 2020, 21:08
Let's say you set the int maximum = 0; and then you put in three negative numbers? The program is supposed to output what the maximum value is of those three negative numbers that were input. However because the int maximum is set to 0, and negative numbers are not greater the program will incorrectly output 0 instead of one of the negative numbers. However if you set it to the MIN_VALUE it is impossible for the program to have an number smaller than that entered. I hope that makes sense?
George
Level 15, United Kingdom
29 August 2020, 12:48
that makes sense. That's why they had the maximum value blank for you to fill ("int maximum = "). I just assigned the first number in the loop to the maximum variable
``if(i == 0) maximum = num``
then had another check to compare against the input number for each iteration.
Tyler Evans
Level 6, Wallasey, United Kingdom
4 September 2020, 17:41
Thank you bro i was stuck on that validation i set maximum to 0 myself and coulnt figure it out makes alot of sence but dont think i would have came to that concliton myself many thanks