Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Arrays
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
BoXxI2004




PostPosted: Mon Mar 08, 2004 12:02 am   Post subject: Arrays

in an array how would u create an random array, ask the user to input some number they can be any like 14 , 25 .. and how would u store them?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Mon Mar 08, 2004 1:53 am   Post subject: (No subject)

As array of random numbers, or an array of random length?
BoXxI2004




PostPosted: Mon Mar 08, 2004 7:05 am   Post subject: (No subject)

well the user inputs the length he wants... and radom numbers (in a range) are created in the array
Tony




PostPosted: Mon Mar 08, 2004 11:17 am   Post subject: (No subject)

so you would just use a forloop to fill in the array Confused
code:

for (int i=0;i<yourArray.length();i++)
{
     yourArray[i] = //generate random value here
}


For the random value, you got to include somethign like .util.Random; and then initialize your randNum class or what not Thinking I'm not sure
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dan




PostPosted: Mon Mar 08, 2004 11:22 am   Post subject: (No subject)

BoXxI2004-> plz make you avtare smaller, it mess with frame on small rez comps.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
wtd




PostPosted: Mon Mar 08, 2004 4:29 pm   Post subject: (No subject)

Hmmm... not tested:

code:
import java.lang.*;
import java.io.*;
import java.util.Random;

public class Test
{
   private static int desiredLength;

   public static int[] intArrayOfLength(int length)
   {
      return new int[length];
   }

   public static void promptForRandomLength(PrintStream output = System.out) throws IOException
   {
      output.print("Please input a random length: ");
   }

   public static void readRandomLength(InputStream input = System.in) throws IOException
   {
      BufferedReader in = new BufferedReader(new InputStreamReader(input));
      desiredLength = Integer.parseInt(in.readLine());
   }

   public static int[] intArrayOfDesiredLength()
   {
      return intArrayOfLength(desiredLength);
   }

   public static void populateIntArrayWithRandomValues(int[] arr)
   {
      Random randNum = new Random(System.currentTimeMillis());
      for (int i = 0; i <arr.length; i++)
          arr[i] = randNum.nextInt();
   }

   public static void main(String[] args)
   {
      int[] myArray;
      promptForRandomLength();
      readRandomLength();
      myArray = intArrayOfDesiredLength();
      populateIntArrayWithRandomValues(myArray);
   }
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: