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 11 1612
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]
- mv*@spam.guard.caspershouse.com
"fsd" <sg@hotmial.com> wrote in message
news:bs************@ID-83837.news.uni-berlin.de... 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
Something like this (pseudo-code):
var1 = Random(3);
var2 = Random(3);
while (var2 == var1)
{
var2 = Random(3);
}
--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
"fsd" <sg@hotmial.com> wrote in message
news:bs************@ID-83837.news.uni-berlin.de... 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
fsd wrote: 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
"Sudsy" <bi*********@hotmail.com> wrote in message
news:3F**************@hotmail.com... fsd wrote: 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.
Simple, but highly effective. Well done.
Seems obvious now, eh?
It does *now*. :)
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" <sg@hotmial.com> wrote in message
news:bs************@ID-83837.news.uni-berlin.de... 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
fsd wrote: 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!");
-- Er*********@sun.com
Sorry, the last if should have been;
if(nArrayPos != -1)
"Marco Martin" <ma**********@sympatico.ca> wrote in message
news:9Y********************@news20.bellglobal.com. .. 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" <sg@hotmial.com> wrote in message news:bs************@ID-83837.news.uni-berlin.de... 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
fsd wrote: 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
--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:#r**************@TK2MSFTNGP12.phx.gbl... 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] - mv*@spam.guard.caspershouse.com
"fsd" <sg@hotmial.com> wrote in message news:bs************@ID-83837.news.uni-berlin.de... 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
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]
- mv*@spam.guard.caspershouse.com
"Eric Johannsen" <no*********@johannsen.us> wrote in message
news:h8****************@newssvr29.news.prodigy.com ... 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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:#r**************@TK2MSFTNGP12.phx.gbl... 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] - mv*@spam.guard.caspershouse.com
"fsd" <sg@hotmial.com> wrote in message news:bs************@ID-83837.news.uni-berlin.de... 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
fsd wrote: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Martin Marcher |
last post by:
Hi,
I've read several questions and often the answer was
'C knows nothing about .'
So if C knows that little as some people say, what are the benefits, I
mean do other languages know more...
|
by: Perception |
last post by:
Hello all,
If I have a C-like data structure such that
struct Data {
int a; //16-bit value
char; //3 ASCII characters
int b; //32-bit value
int c; //24-bit value
}
|
by: Bit byte |
last post by:
I have the following methods:
static void Foo::setBar(const Bar*) ; //store a copy of Bar
static const Bar* Foo::getBar(void) const ; //return an UNMODIFIABLE ptr
to our internal copy
In...
|
by: petermichaux |
last post by:
Hi,
It seems like determining element position in a web page is a difficult
task. In the position reporting source code I've looked at there are
special fixes for at least some versions of...
|
by: Ethan Furman |
last post by:
len wrote:
I've never had the (mis?)fortune to work with COBOL -- what are the
files like? Fixed format, or something like a dBase III style? I
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |