This week's book giveaways are in the Angular and TypeScript and Web Services forums.
We're giving away four copies each of Programming with Types and The Design of Web APIs and have the authors on-line!
See this thread and this one for details.
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Merge codes - Generate 500 RANDOM numbers, and find the Nth smallest number (range from 100 – 1000)

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Generate 500 Random number range from 100 – 1000



2) Nth smallest number



How can I merge both into one code so, that after generating 500 random number b/w 100-1000, the next code should print the Nth smallest number.
Can someone please help me with the right logic to solve this.

Thanks

 
Saloon Keeper
Posts: 6588
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code does not generate the correct random range. The range you have is 0-999 (inclusive).
 
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please use the code button; I might go back to your post and use the code button on it, and you can see how much better it looks

Don't declare loop variables outside the loop. The way you wrote a loop in the RandomNumbers class is how they had to write loops in C but the other loop in NthSmall line 10 shows the correct way to do it in Java®.
You have correctly found the largest and smallest elements in your 4‑element array but finding the nth element for an arbitrary value of n can be awkward. You might have to (copy and) sort the array and then count elements.

Even that might be mathematically incorrect. If you have an arrayand you seek the 4th smallest (index=3), you will get −99. If you write the numbers out, oou find that −99 is the 8th= largest element. So you add 8 and 4 and get 12 but that array has eleven elements. I suggest you pretend you don't know that!

As far as I am concerned, the correct way to fill an array with “randdom” numbers is to use the IntStream you can get from the Random object:-Details of ints() method, and of toArray()(←links).
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This morning, I wrote: . . So you add 8 and 4 and get 12 but that array has eleven elements. I suggest you pretend you don't know that! . . .

...and if you try the next‑smallest that is still equal to the 8th= largest, so you add 8 and 2 and you get 10.

Again, I recommend you pretend you don't know that
 
Sibi Varghese
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code I got worked out from someone online:

import java.util.Random;
public class RandomNumeberNth {

 public static void main(String[] args) {

 int startcounter=100;
 int n=0;
 int endCounter=500;
 int arraysize = endCounter-startcounter;
 int numbers[] = new int[arraysize+1];
 Random rnum = new Random();
 System.out.println("Random Numbers\n****************");
     for(int counter=startcounter;counter<=endCounter;counter++)
 {
 int c = n++;
 int num = rnum.nextInt(1000);
 numbers[c]= num;
 System.out.println(numbers[c]);

 }
 int smallest = numbers[0];
               int biggest = numbers[0];

               for(int i=1; i< numbers.length; i++)
               {
                       if(numbers[i] > biggest)
                               biggest = numbers[i];
                       else if (numbers[i] < smallest)
                               smallest = numbers[i];
               }
               System.out.println("Largest Number is : " + biggest);
               System.out.println("Smallest Number is : " + smallest);
}
}

so, I had few questions on some lines of code

1)  int endCounter=500; - Do I keep this 500 since, I'm generating 500 numbers

2)  this part of the code :
         int c = n++;
 int num = rnum.nextInt(1000);
 numbers[c]= num;

Some confusion with these lines of code : 1) basically you are declaring int variable 'c' and the value is incremented as n++ 2) Generates integer to be printed and return a number within > =1000 - int num = rnum.nextInt(1000); 3) numbers[c]= num; creating a array and assigning to num. Please correct my understanding.
 
Sheriff
Posts: 6551
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've already been told to UseCodeTags (that's a link) when you post code.

1)  int endCounter=500; - Do I keep this 500 since, I'm generating 500 numbers


Yes.  

2)  this part of the code :
        int c = n++;
int num = rnum.nextInt(1000);
numbers[c]= num;

Some confusion with these lines of code : 1) basically you are declaring int variable 'c' and the value is incremented as n++ 2) Generates integer to be printed and return a number within > =1000 - int num = rnum.nextInt(1000); 3) numbers[c]= num; creating a array and assigning to num. Please correct my understanding.


1) Not quite.  Since the ++ is after the variable name, you use the variable first, then increment it.  So c gets n than n get incremented.

2) Almost.  nextInt(1000) will create an integer from 0 to 999.

3) No.  The array numbers has already be defined.  numbers[c]= num; assigns num to numbers at index c.
 
Sibi Varghese
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome!!! Knute Snortum. Thanks a lot.

Knute Snortum wrote:You've already been told to UseCodeTags (that's a link) when you post code.

1)  int endCounter=500; - Do I keep this 500 since, I'm generating 500 numbers


Yes.  

2)  this part of the code :
        int c = n++;
int num = rnum.nextInt(1000);
numbers[c]= num;

Some confusion with these lines of code : 1) basically you are declaring int variable 'c' and the value is incremented as n++ 2) Generates integer to be printed and return a number within > =1000 - int num = rnum.nextInt(1000); 3) numbers[c]= num; creating a array and assigning to num. Please correct my understanding.


1) Not quite.  Since the ++ is after the variable name, you use the variable first, then increment it.  So c gets n than n get incremented.

2) Almost.  nextInt(1000) will create an integer from 0 to 999.

3) No.  The array numbers has already be defined.  numbers[c]= num; assigns num to numbers at index c.

 
Sibi Varghese
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Knute Snortum          
         int startcounter=100;  // Do I keep startcounter on '0' if I have generate 500 random numbers
   int n=0; // This is initial value of n and later it incremented as n++ and initialized as variable 'c'
 int endCounter=500;
 int arraysize = endCounter-startcounter; // this defines the array size, basically of 500 random numbers  
 int numbers[] = new int[arraysize+1]; Declaring integer array for variable 'numbers' equal to new integer[size of array +1]

Please correct my understanding. Thanks in advance.

Sibi Varghese wrote:Awesome!!! Knute Snortum. Thanks a lot.

Knute Snortum wrote:You've already been told to UseCodeTags (that's a link) when you post code.

1)  int endCounter=500; - Do I keep this 500 since, I'm generating 500 numbers


Yes.  

2)  this part of the code :
        int c = n++;
int num = rnum.nextInt(1000);
numbers[c]= num;

Some confusion with these lines of code : 1) basically you are declaring int variable 'c' and the value is incremented as n++ 2) Generates integer to be printed and return a number within > =1000 - int num = rnum.nextInt(1000); 3) numbers[c]= num; creating a array and assigning to num. Please correct my understanding.


1) Not quite.  Since the ++ is after the variable name, you use the variable first, then increment it.  So c gets n than n get incremented.

2) Almost.  nextInt(1000) will create an integer from 0 to 999.

3) No.  The array numbers has already be defined.  numbers[c]= num; assigns num to numbers at index c.

 
Knute Snortum
Sheriff
Posts: 6551
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sibi Varghese: Please don't quote the entire previous message, only enough to provide context.

int startcounter=100;  // Do I keep startcounter on '0' if I have generate 500 random numbers


What's your guess on this?

int n=0; // This is initial value of n and later it incremented as n++ and initialized as variable 'c'


Well, I'd say c is initialized to n, but yes.

int arraysize = endCounter-startcounter; // this defines the array size, basically of 500 random numbers


No, arraysize would be 400.

int numbers[] = new int[arraysize+1]; Declaring integer array for variable 'numbers' equal to new integer[size of array +1]


Yes.

 
Sheriff
Posts: 14614
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags (←click that, it's a link to a page with instructions on how) when posting code.
 
Pay attention! Tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!