Maximum in an array

  • 4
  • Locked
  • Exercise
Whoever gets the best result in today's competition receives the key to New York City! He or she will need to automate 25 processes faster than 19 other classmates. Let's write a program that can help determine the winner of such competitions: create an array of 20 elements, fill it with numbers, and find the maximum.
You can't complete this task, because you're not signed in.
Comments(6)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
VrhLevel 9 , Zagreb
26 June, 21:14
I used .sort method and that seemed like too simple. I would like to solve it with a more complex algorithm but I don't know how. It would be cool if I knew to do this: declare a max variable -> loop through array and check if the array[i] > than max, if so set it to max The problem I encounter ( and this has happened in a few assignments so far) is this: 1) if i declare a variable in for loop, then it declares it on every single iteration and it doesn't make sense 2) if I declare it outside of for loop I have to give it initial value (or 0) which won't be good if there are negative numbers Any help with this?
VrhLevel 9 , Zagreb
26 June, 21:25
Actually I managed to solve this. 1) I declare Integer x = null; 2) In for loop, I create an if statement which will check if x is null -> set the x to array[i] (which will happen on first iteration every time 3) then i have else if statement that checks whether next numbers in array are bigger than x This seems to work but I would like to know if this is a good way or I can do it without using Integer object.
RomanLevel 41
27 June, 07:29
int max = Integer.MIN_VALUE;
VrhLevel 9 , Zagreb
28 June, 19:27
Thanks mate, this is really useful. I remember I used that before but didn't understand its importance.
Ishan GuptaLevel 9 , Aligarh
27 August 2018, 16:36
I am getting error at step 2 , where it says to input the numbers in array and return the array.what can be done?
RomanLevel 41
28 August 2018, 05:25
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.