Results 1 to 22 of 22

Thread: Save words in variable

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    33

    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?

  2. #2
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    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

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    33
    No that didn't work
    I got an error that said: "Lvalue Required"

  4. #4
    Destined Soul
    Guest
    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

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    33
    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?

  6. #6
    Sc0rp
    Guest
    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:
    James
    James
    Lars
    James
    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.

  7. #7
    Sc0rp
    Guest
    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 ij;
       
    char c[4][7] = {"Tony","James","Johnny","Lars"}; 
       
    char var[4][7];
       
       
    srand((unsigned)time(NULL));   
       
       for (
    04i++)
       {    
          
    rand() % 4;
          
    strcpy(var[i], c[j]);
       }
       
       
    cout << "Names are: " << endl;
       for (
    04i++)
          
    cout << " " << var[i] << endl;

       return 
    0;

    This should also work.

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    33
    Yeah that worked great! Thanks!

  9. #9
    Sc0rp
    Guest
    No problem.

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Why not use pointers?
    PHP Code:
    int main(int argccharargv[])
    {


       
    char** i;
       
    char c[4][7] = {"Tony","James","Johnny","Lars"}; 
       
    char* var[4];
       
       
    srand((unsigned)time(NULL));   
       
       for (
    = &var[0]; < &var[4]; i++)
          *
    ic[rand() % 4];
       
       
    cout << "Names are: " << endl;
       for (
    = &var[0]; < &var[4]; i++)
          
    cout << " " << *<< 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.

  11. #11
    Sc0rp
    Guest
    kedaman: I don't see how your solution solves the duplicates problem?

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  13. #13
    Sc0rp
    Guest
    I'm confused now..

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  15. #15
    Sc0rp
    Guest
    Originally posted by kedaman
    There's no duplicates in my solution, just pointers to the original array (dunno what you meant by that )
    Rephrase please?

  16. #16
    Destined Soul
    Guest
    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

  17. #17
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  18. #18
    Sc0rp
    Guest
    Destined Soul: Yes, that's what I meant.

  19. #19
    Aragorn
    Guest
    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.

  20. #20
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  21. #21
    Destined Soul
    Guest
    Isn't that what I said? Well, maybe yours wasn't as long winded, kedaman.

    Destined

  22. #22
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
  •  



Click Here to Expand Forum to Full Width