Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 10th, 2005, 3:26 AM   #1
secrecy230
Newbie
 
Join Date: May 2005
Posts: 11
Rep Power: 0 secrecy230 is on a distinguished road
fill array with unique numbers

fill Array with unique number
i have to construct an array of integers and i have to which will generate six integer values in the range 1 to 45 inclusive. The algorithm must loop until all of the six numbers generated are unique


for now i ahve only be able to do that part>>

import java.util.Random;
class s1_1999
{
private static int[] fillArray(int [] array)
{
Random random = new Random();
for(int row = 0 ; row < array.length ;row++)
{

array[row]=random.nextInt(10);

}


private static boolean compare(int [] testarray)
{
boolean test=false;
int testValue=0;
for(int row = 0; row<testarray.length;row++)
{
for (int i=1 ;i<testarray.length ; i++)
{
if(testarray[row]==testarray[i])
{
test=true;

}

}

return test;

}






public static void main(String []args)
{
int [] inarray=new int[6];

fillArray(inarray);

}



can you help me to complete this java program plz??????????
thanx in
advance
from
andy

Forum: Java Post New Thread
Logged in as secrecy230
Title:

Message:
secrecy230 is offline   Reply With Quote
Old Jul 10th, 2005, 3:30 AM   #2
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
for(int row = 0 ; row < array.length ;row++)
{

array[row]=row;

}


There you have it, they're all unique.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote
Old Jul 10th, 2005, 3:43 AM   #3
secrecy230
Newbie
 
Join Date: May 2005
Posts: 11
Rep Power: 0 secrecy230 is on a distinguished road
hey thanx
but the number has to be random so when i apply what u told me it is not random infact the overall question is

Write a pseudo code algorithm which will generate six integer values in the range 1 to
45 inclusive. The algorithm must loop until all of the six numbers generated are unique
(i.e. none two numbers are the same). You may assume that you have access to the
following methods:
double Math.random() Generates a random number between 0.0 and
1.0 inclusive.
boolean NoneTheSame( int [] numbers) Returns true if all the numbers in the array
numbers are unique and false otherwise.
secrecy230 is offline   Reply With Quote
Old Jul 10th, 2005, 3:54 AM   #4
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
That says nothing about all the numbers having to be random.
Also, I'm not trying to be mean, but could you please use less abbreviations like "u"? It makes your post harder to read. Thanks.

Here's what you could do if they have to be random, I don't know Java very well though so it might not compile
void fill_array(int[] array)
{
	do
	{
		for(int i=0;i<6;i++)
		{
			array[i] = Math.random()*45;
		}
	}while(!NoneTheSame(array));
}
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials.
--WilliamSChips on Slashdot
uman is offline   Reply With Quote
Old Aug 9th, 2005, 9:55 PM   #5
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
Hmm.. not the most efficient algorithm by any means. It works, certainly. I'm not trying to rag on Uman, but there are other ways. Most notably, instead of filling the entire array and checking for duplicates at the end, pick each number individually. Also, use a second array to check for collisions instead of using nested for-loops. (Nested loops run in n-squared time, while an array method would run in constant time.. sorta).

  public static int getRandom(int min, int max) {
    return ((int) (Math.random() * (max - min + 1))) + min;
  }

  public static void fillArray(int[] toFill) {
    boolean[] beenUsed = new boolean[46];
    for (int i = 0; i < 45; i++) {
      beenUsed[i] = false;
    }

    //for each element in the array to me filled
    for (int j = 0; j < toFill.length; j++) {

      //find a number that's not been used yet
      int ran;
      do {
        ran = getRandom(1, 45);
      } while (beenUsed[ran]);

      //put that number in the toFill array, and mark it used in the beenUsed array
      toFill[j] = ran;
      beenUsed[ran] = true;
    }
  }

If you're wondering, the boolean array is in fact one size larger than it really needs to be. I could have make it one size smaller and subtraced all values by one, but this way's easier to understand. And before anyone gets on my case over my sacrificing memory efficiency for speed efficienfy, just know that I always favor speed. *shrugs*
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose?
EdSalamander is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:16 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC