|
-
Aug 15th, 2001, 05:44 PM
#1
Thread Starter
Member
Save words in variable
Hi. Newbie question again 
I have this code that randoms out 4 names on the screen and I want to save the words that are randomed out on the screen in a variable (maybe with an array?).
this is the code:
Code:
char c[4][6] = {"Tony","James","Johnny","Lars"};
srand((unsigned)time(NULL));
for (i=0;i<4;i++){
j = rand() % 4;
cout<<c[j]<<"\n";
// code for saving c[j] names in a variable
}
I want to save the c[j] in a variable so I have all four randomed names in one variable (array?).
How do I do this?
-
Aug 15th, 2001, 06:27 PM
#2
Hyperactive Member
Code:
char c[4][6] = {"Tony","James","Johnny","Lars"};
srand((unsigned)time(NULL));
char var[4][6];
for (i=0;i<4;i++){
j = rand() % 4;
cout<<c[j]<<"\n";
var[i] = c[j];
}
Try that, if it doesn't work I have something else to try
Matt 
-
Aug 16th, 2001, 05:22 AM
#3
Thread Starter
Member
No that didn't work 
I got an error that said: "Lvalue Required"
-
Aug 16th, 2001, 12:47 PM
#4
I got this to work:
Code:
#include <time.h>
#include <stdlib.h>
#include <iostream.h>
void myFunc()
{
int i, j;
char c[4][7] = {"Tony","James","Johnny","Lars"};
char var[4][7];
srand((unsigned)time(NULL));
for (i = 0; i < 4; i++)
{
j = rand() % 4;
var[i] = c[j];
}
cout << "Names are: " << endl;
for (int i = 0; i < 4; i++)
cout << " " << var[i] << endl;
}
Note that I had to change the size of the array from [4][6] to [4][7]. My compiler didn't like that. (I'm using Dev-C++ on the computer I'm at...) I think this is due to the fact that it appends a null char '\0' at the end of the strings if one doesn't exist.
Destined
-
Aug 18th, 2001, 03:11 PM
#5
Thread Starter
Member
Thanks for your answer but that code didn't work for me 
I got this error:
error C2106: '=' : left operand must be l-value
I tried to change it to "==" but that didn't work either. I'm using VC++ 6.0. Do I have to compile it in another compiler or what?
Does anyone have a solution or maybe some other code?
-
Aug 19th, 2001, 01:07 AM
#6
I got this working under VC++6:
Code:
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i, j;
string c[4] = {"Tony","James","Johnny","Lars"};
string var[4];
srand((unsigned)time(NULL));
for (i = 0; i < 4; i++)
{
j = rand() % 4;
var[i] = c[j];
}
cout << "Names are: " << endl;
for (i = 0; i < 4; i++)
cout << " " << var[i] << endl;
return 0;
}
Note that the output may have duplicate names, e.g:
Anyway, I couldn't get it to work with the char arrays, but it might be possible to get it running by using a pointer to a char array. 
I hope this helps.
-
Aug 19th, 2001, 09:46 AM
#7
And if you don't wanna use strings and you want to use the char arrays:
PHP Code:
#include <time.h>
#include <stdlib.h>
#include <iostream.h>
#include <string.h>
int main()
{
int i, j;
char c[4][7] = {"Tony","James","Johnny","Lars"};
char var[4][7];
srand((unsigned)time(NULL));
for (i = 0; i < 4; i++)
{
j = rand() % 4;
strcpy(var[i], c[j]);
}
cout << "Names are: " << endl;
for (i = 0; i < 4; i++)
cout << " " << var[i] << endl;
return 0;
}
This should also work.
-
Aug 20th, 2001, 04:59 AM
#8
Thread Starter
Member
Yeah that worked great! Thanks!
-
Aug 20th, 2001, 05:04 AM
#9
No problem.
-
Aug 20th, 2001, 05:13 AM
#10
transcendental analytic
Why not use pointers?
PHP Code:
int main(int argc, char* argv[])
{
char** i;
char c[4][7] = {"Tony","James","Johnny","Lars"};
char* var[4];
srand((unsigned)time(NULL));
for (i = &var[0]; i < &var[4]; i++)
*i= c[rand() % 4];
cout << "Names are: " << endl;
for (i = &var[0]; i < &var[4]; i++)
cout << " " << *i << endl;
return 0;
}
That way you won't need to copy over and worry about syncronisation of the duplicates and such.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 20th, 2001, 06:39 AM
#11
kedaman: I don't see how your solution solves the duplicates problem?
-
Aug 21st, 2001, 05:08 AM
#12
transcendental analytic
There's no duplicates in my solution, just pointers to the original array (dunno what you meant by that )
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 21st, 2001, 01:14 PM
#13
I'm confused now..
-
Aug 21st, 2001, 01:51 PM
#14
transcendental analytic
What's confusing you? I'll gladly explain if something is
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 21st, 2001, 02:28 PM
#15
-
Aug 21st, 2001, 04:03 PM
#16
I think Sc0rp is referring to the fact that a name will (most likely) appear more than once - ie: it will be a duplicate.
The only way of getting around this is to either randomly swap two values, check to see if the value already exists, or to use an algorithm that creates a list of some sort that moves an item (randomly selected) within that list to the final output array.
Swap is essentially a shuffle, where you pick a number between 1 and N-1 (inclusive), and swap it with it's upper neighbour.. ie:
Code:
int i = rand % (N-1);
Swap(i,i+1);
This way, you don't have to keep track of a second random variable.. The bad thing here is that you have to loop through this sufficiently enough to be "shuffled" This is easy to implement, but will become extremely inefficient if your list becomes too big.
If I were to write some code, I would use two dynamic integer arrays. The first would be a list of possible index values for your names. (ie: 1,2,3,4) I would then randomly pick a number from this array, add it to the end of the other array, and then delete it from the list.
Something like:
A = initial array = (1,2,3,4);
B = new array = ();
Pick number, result = 3.
A = (1,2,4)
B = (3)
Pick number, result = 3 (third index)
A = (1,2)
B = (3,4)
Pick number, result = 1
A = (2)
B = (3,4,1)
Pick number, result = 1 (default)
A = () // we're done here
B = (3,4,1,2)
Now take that and reorder the names:
Names are now (using original names from first post)
"Johnny", (3)
"Lars", (4)
"Tony", (1)
"James" (2)
Hope this helps a bit. 
Destined
-
Aug 21st, 2001, 04:06 PM
#17
transcendental analytic
Doh!
Ok, Swapping is fine for small arrays, if you want it to go fast on big ones you can make a bitarray and flag off as you pick out random elements
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 22nd, 2001, 11:02 AM
#18
Destined Soul: Yes, that's what I meant.
-
Aug 22nd, 2001, 11:46 AM
#19
yeah, kedaman, that would e right, but then you would need to 're'pick a number every time you picked a flagged out one. And for large lists, when most numbers are flagged out, it will be very difficult to finally get the right ones.
I think DestinedSoul's code is the optimum.
-
Aug 22nd, 2001, 01:35 PM
#20
transcendental analytic
Right, here's a better solution:
make a table of pointers or indexes, pick out index by random and fill the hole with the last index and decrement the right modulus operand on rand
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 22nd, 2001, 01:52 PM
#21
Isn't that what I said? Well, maybe yours wasn't as long winded, kedaman. 
Destined
-
Aug 22nd, 2001, 02:27 PM
#22
transcendental analytic
doh! I could have read your post before but it was so long i thought it was complicated anyway the shuffle thing might be heavy
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|