Little Problem

F

fsd

I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA
 
J

Joseph Dionne

fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA

Add var1 and var2 together

psuedo code;

switch(var1 + var2)
case 3: three is left
case 4: two is left
case 5: one is left
 
N

Nicholas Paldino [.NET/C# MVP]

fsd,

What I would do is store the values in an ArrayList. When you pick the
numbers randomly, remove them from the ArrayList and place into a new
collection. This would require you to change the range of selected random
numbers every time (the first time you pick a random number from the range
1-3, then 1-2, etc, etc). You just have to stop when you have enough
numbers selected. Then, whatever is left over in the ArrayList is what was
not selected.

Hope this helps.
 
S

Sudsy

fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA

Think! What do the numbers 1, 2 and 3 add up to? Add up the two
you know, subtract from 6 and you've got the third.
Seems obvious now, eh?
 
D

Dmitriy Lapshin [C# / .NET MVP]

Something like this (pseudo-code):

var1 = Random(3);
var2 = Random(3);
while (var2 == var1)
{
var2 = Random(3);
}
 
E

Eric Sosman

fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

if (var1 != var2)
System.out.println ( (6 - var1 - var2) + " is missing" );
else
System.out.println ("Two values are missing, you liar!");
 
M

Marco Martin

try this;

int nArrayPos = -1;
for(int x = 0; x < arrayOfVars.Length; x++)
{
if(var1 != arrayOfVars[x] & var2 != arrayOfVars[x])
{
nArrayPos = x;
}
}
if(nArrayPos != 01)
MessageBox.Show(arrayOfVars[x].ToString() + " is not beeing used");
 
M

Marco Martin

Sorry, the last if should have been;

if(nArrayPos != -1)

Marco Martin said:
try this;

int nArrayPos = -1;
for(int x = 0; x < arrayOfVars.Length; x++)
{
if(var1 != arrayOfVars[x] & var2 != arrayOfVars[x])
{
nArrayPos = x;
}
}
if(nArrayPos != 01)
MessageBox.Show(arrayOfVars[x].ToString() + " is not beeing used");


fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA
 
B

Brad BARCLAY

fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

Think more mathematically. Sum the numbers you have together, and
deduct them from the sum of all three numbers. No loops required.

Brad BARCLAY
 
E

Eric Johannsen

Unless I'm missing something, this would not work because theoretically you
could get "1" as your answer every time...and that would be stored in each
element of the array list.

Eric

Nicholas Paldino said:
fsd,

What I would do is store the values in an ArrayList. When you pick the
numbers randomly, remove them from the ArrayList and place into a new
collection. This would require you to change the range of selected random
numbers every time (the first time you pick a random number from the range
1-3, then 1-2, etc, etc). You just have to stop when you have enough
numbers selected. Then, whatever is left over in the ArrayList is what was
not selected.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA
 
N

Nicholas Paldino [.NET/C# MVP]

Eric,

That's not what I meant. The array list would be populated with values,
but not randomly. It would be populated with the set of values to select
from (however one comes to those values is irrelevant). Then, a random
number between 0 and the length of the array - 1 is chosen, representing the
index that the value selected is at. That value is placed in another
collection, and the element at that index is removed from the original set.
This reduces the length by one. Even if one is selected every time, it
works because the elements are shifted down one with each removal, providing
a different value.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Eric Johannsen said:
Unless I'm missing something, this would not work because theoretically you
could get "1" as your answer every time...and that would be stored in each
element of the array list.

Eric

message news:#r#[email protected]...
fsd,

What I would do is store the values in an ArrayList. When you pick the
numbers randomly, remove them from the ArrayList and place into a new
collection. This would require you to change the range of selected random
numbers every time (the first time you pick a random number from the range
1-3, then 1-2, etc, etc). You just have to stop when you have enough
numbers selected. Then, whatever is left over in the ArrayList is what was
not selected.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA
 
A

Alex Hunsley

fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA
You could perhaps use the observation that the final number is equal to:

6 - var1 - var2

alex
 
W

Walter Mitty

I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

Subtract the two you have from 6.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top