GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Jun-2005, 23:11
swayp swayp is offline
New Member
 
Join Date: Jan 2005
Posts: 25
swayp is on a distinguished road

weird loop problem. plz help


Hey,
I've been looking at this for a while and it is probably something so simple and needs another pair of eyes to solve it. So here goes...

I have an integer array of 6 integers. The point of it is that each integer holds a position between 0-5, but no position can be repeated twice. I want it to be completely random so I use srand to generate a random number, then I want to test to make sure it is not already in the array, and if it isn't place it in the array.

Here is what my code looks like (I put a bunch of cout's to try and debug this thing which I'll explain the results of below)

CPP / C++ / C Code:
while ( i < 6 )
	{
		x = rand() % 6;
		cout << "1. x: " << x << endl;

		for ( j = 0; j < 6; j++ )
		{
			cout << "1b. letterOrder[" << j << "]: " << letterOrder[j] << endl;
			if ( x == letterOrder[j] )
				j = 6;
		}
		
		letterOrder[i] = x;
		cout << "2. x: " << x << endl;
		cout << "3. letterOrder[" << i << "]: " << letterOrder[i] << endl;
		i++;
	}

i is intialized to 0 before the loop, so it starts as 0.

I used a for loop to set all the integers in the letterOrder array to 9, that way when the loop starts, no number will match up between the random number generated between 0-5:

CPP / C++ / C Code:
	for ( int m = 0; m < 6; m++ )
	{
		letterOrder[m] = 9;
		cout << "0. letterOrder[" << m << "]: " << letterOrder[m] << endl;
	}

More cout's for my testing, and the result of this cout is 9's for every integer.

However, the first time the loop runs, letterOrder[5] is equal to 5, instead of 9. I have no idea why it changes to 9, and it is the only one that changes to 9. letterOrder[0-4] are all equal to 9. This is where I'm stuck, because obviously as it loops, the final output won't be correct since 5 already occupies one of the positions in the array.
Last edited by LuciWiz : 03-Jun-2005 at 00:27. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 03-Jun-2005, 03:26
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
There is the following error with your code:
Quote:
Originally Posted by swayp
Code:
for ( j = 0; j < 6; j++ ) { cout << "1b. letterOrder[" << j << "]: " << letterOrder[j] << endl; if ( x == letterOrder[j] ) j = 6; }
this piece of your code does nothing. it just runs in circles and exits.
I think you actually wish it to do the following:
CPP / C++ / C Code:
    for ( j = 0; j < 6; j++ )
    {
      if ( x == letterOrder[j] )
      {
          x = rand() % 6;
          j = -1; // because in "}" there will be "j++" and then j==0
          // now the checking begins again with new x.
      }
    }


P.S: just for the sake of clarity, if you have something like:
Code:
i=0; while( i < 6 ) { . . . i++; }
it would be more simple to use the for-statement. +makes it more easy to read.
Last edited by LuciWiz : 03-Jun-2005 at 07:26. Reason: Please insert your C code between [c] & [/c] tags
  #3  
Old 03-Jun-2005, 13:15
swayp swayp is offline
New Member
 
Join Date: Jan 2005
Posts: 25
swayp is on a distinguished road
Hey, thanks for your help, but... I think you were a little confused on what I was trying to do, most likely because I didn't explain it well enough.

I got it to work by changing my code to this:

Code:
do { //get random number between 0-5 and place in x x = rand() % 6; cout << "1. x: " << x << endl; } while ( x == letterOrder[0] || x == letterOrder[1] || x == letterOrder[2] || x == letterOrder[3] || x == letterOrder[4] || x == letterOrder[5]);

My logic is as follows: It generates a random number between 0-5. Then it tests to see whether this random number has already been generated and placed in any of the slots of the letterOrder array. If it has been used, it generates a random number again and again until it generates one that has not. If it hasn't been used, it breaks out of the loop and places it in the array in the correct spot.

This seems extremely inefficient to me. For instance, if letterOrder was anything more than a 6 integer array, that do/while statement will look extremely messy and long.

Is there a better way to write that while statement? Or even the whole loop for that matter.

At least I got it to work tho, thanks
  #4  
Old 03-Jun-2005, 13:44
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
Quote:
Originally Posted by swayp
Hey, thanks for your help, but... I think you were a little confused on what I was trying to do, most likely because I didn't explain it well enough.
I disagree. If you would have read my previous message more carefully you would have realized that my correction to your code has exactly the same logic than in your new code and explanation. Although I am too tired to prove it step by step. what's the point besides you do your own thinking. Ultimately your solution still works as well as mine so there is of course no reason to debate about what is better.

Quote:
This seems extremely inefficient to me. For instance, if letterOrder was anything more than a 6 integer array, that do/while statement will look extremely messy and long.
There is other serious inefficiency too. Consider if you have array of 1000 numbers and you are inserting random values to the array with your algorithm. Your algorithm test always that the most reasently created random number is not yet in that array. Imagine that your algorithm is trying to insert the number value to the 999.th place in the array. So there is only ONE number out of 1000 possible numbers that is not yet used. This means that the loop will try to find that 1 number that will fit with a random generator. For each time the rand() is called there exists only 1/1000 probability that the x returned will fit to the array.

Your problem is actually a re-arranging problem. In principle you have numbers 1-n which you have to rearrange to a random order. Correct me if you think otherwise but please put tought to your argumentation.

Then your problem becomes more simple if you initialize your array with values 1-n after which you call n times function (or loop n times through a piece of code) that will put the existing values in array to a new, random, order.
like
CPP / C++ / C Code:
int arr[] = new int[1000];
int i;
for(i=0; i<1000; i++)  arr[i] = i; //orderly order

// break the order
int helper, rtarg;
for(i=0; i<1000; i++) {
  rtarg = rand() % 1000;
  helper = arr[i];      // backup the content of arr[i]
  arr[i] = arr[rtarg];  // from random place rtarg the value to place i
  arr[rtarg] = helper;  // now the values of two place are exchanged 
}
// now you have randomly ordered array of numbers 0 - 999.
  #5  
Old 03-Jun-2005, 19:26
swayp swayp is offline
New Member
 
Join Date: Jan 2005
Posts: 25
swayp is on a distinguished road
Oh, I see. I knew there was a better way to do it. Thank you for help, I was not trying to be rude or anything.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Array 1 dimensional help please asap lion123 C Programming Language 10 18-Feb-2005 21:53
Problem - for loop input st8tic1 C++ Forum 1 06-Mar-2004 06:42
weird search problem!! JUNK KED Open Discussion Forum 3 11-Oct-2003 00:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:28.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.