• Post Reply Bookmark Topic Watch Topic
  • New Topic

indexOfMaxInRange Method  RSS feed

 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a method called indexOfMaxInRange that takes an array of 10 random integers, and two parameters that define the range (low and high. This method should find the largest element in the given range of array indices and returns its index.

This is what I have done for it:


What I don't understand is where I would put something in for it to take an array of 10 random integers. How would I do that?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is pretty easy to create a random array like that, with an instance of the Random class (Java8+). Never mind all the things you are told about loops: use the methods of the Random object (here called rand):-Every Random object has an ints() method; that particular overriding will give you ten (10L) nearly random numbers between 1 and 99 fed to an IntStream, whose toArray() method gives you the requisite int[]. So far, so good. Now, let us make a bigger array than 10 elements, maybe 100. Let us go from element i to j inclusive. Now work out what the starting index will be (hint: it isn't low + 1) and the finishing index, which you will mention in the middle part of the for header.
Note you can use two conditions joined together with the && operator in the middle part of the for loop header. You don't appear to be using high in your loop at all at present.
Note that line 4 will not find the highest number in the array. I shall leave you to work out what it will actually find.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am required to call it from main. Although I did read on what you were saying above, I understood that but am not allowed to use it.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not clear on what you are asking. Are you having trouble producing an array of 10 random integers to pass to that method? Or do you have the array, but you're having trouble passing it to the method? Or something else?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with this? Are you really not allowed to us this to create your array?Have you corrected the problems in the max in range method?
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure how to make this that I was given to take an array of 10 random integers.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you're reading too much into your requirements. You could also interpret "take an array of random integers" as "take an array of integers that are not arranged in any particular order or have any particular values". Maybe the requirements are simply stating what you can expect, not what it's asking you to do. IMO, your code already fulfills the requirement by declaring an int[] as the first parameter to the method. I think you're just worrying over nothing.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as testing your code is concerned, I don't see anything in the requirement statement that you posted that prevents you from following Campbell's suggestion.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:As far as testing your code is concerned, I don't see anything in the requirement statement that you posted that prevents you from following Campbell's suggestion.

Oh I see what you mean, but When I put that code in my main, what is supposed to replace the portion of it? Is that where my class name goes?
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind I found my problem and figured out how to use it with an idea from Campbell Ritchie.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if you had been given an array already, isn't it simpler to use it?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are finding the index, I think you shou‍ld print the array so you can verify that the index you have actually is the highest (in range). Probably simplest way to print an array:-
System.out.println(Arrays.toString(myArray));
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!