Thread: Randomize (please help

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    3

    Question Randomize (please help

    Hi guys

    I am new to programming and need help. I need to create a quiz using random in C++. I have 2 arrays of the same words in the same order in 2 languages. I would like to write a piece of code that picks a word at random from one of the arrays and askes for the equivelent word from the other array. I would need a loop of some kind to give the user so many attemps. What header file do I need to use for random? Any help would be great.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    edit:

    When you says two arrays of words, are you talking about a string of words separated by spaces like
    Code:
    EnglishWord1 EnglishWord2 EnglishWord3
    or something similar? If so, I would rethink your design. It'd be much easier to have an array of char pointers and set those equal to string constants (or the addresses of other strings if the strings are supplied by file or other method at run time).

  3. #3
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    to get random numbers use the rand() function
    http://www.cprogramming.com/tutorial/random.html

    to control how many attempts the user gets use a loop (most likely a for loop)
    http://www.cprogramming.com/tutorial/lesson3.html

    instead of using arrays i would use a vector:
    http://www.cprogramming.com/tutorial/stl/vector.html
    Keyboard Not Found! Press any key to continue. . .

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    Quote Originally Posted by sean_mackrory
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    edit:

    When you says two arrays of words, are you talking about a string of words separated by spaces like
    Code:
    EnglishWord1 EnglishWord2 EnglishWord3
    or something similar? If so, I would rethink your design. It'd be much easier to have an array of char pointers and set those equal to string constants (or the addresses of other strings if the strings are supplied by file or other method at run time).
    Sorry, here is more info.

    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
     
     char engwords [50][12] = {"Dog","Cat","Computer","Chair","Love","School",
     "Talk","Word","Letter","Jump","Pencil","Paper","Coffee",
     "Milk","Bag","Board","Eraser","Wound","Morning","Night","Noon",
     "Dinner","Skin","Green","Blue","Red","Earthequake","Volcano","River",
    "Sea","Trousers","Joy","Happy","Stairway","Vegetables","Cup","Scissor","Shoes","Can","Car","Job","Work",
    "Boy","Girl","Soil","Baby","Leader","Shout","Write","Hand"};//the array of 50 english words
    
    
    char philwords [50][12]= {"Aso","Pusa","Kompyuter","Silya","Mahal","Paaralan","Usap","Salita",
    "Letra","Lukso","Lapis","Papel","Kape","Gatas","Bag","Pisara","Pambura","Sugat","Umaga",
    "Gabi","Tangali","Hampunan","Balat","Berde","Asul","Pula","Lindol","Bulkan","Ilog","Dagat",
    "Pantaloon","Ligaya","Maligaya","Hagdanan","Gulay","Tasa","Gunting","Sapatos","Lata","Kotse",
    "Trabaho","Gawa","Lalake","Babae","Lupa","Bata","Pinuno","Sigaw","Sulat","Kamag"};//the array of 50 philippine words
    
    
    
    char option;
    int translator();
    int quiz();
    int main()
    {
    i want the programme to randomley pick an english word from the english array and ask for the philopine translation. possibly giving the user 3 chances to get it right.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If your array has 50 entries, simply use the links posted above for how to get a random number between 0 and 49 (inclusive). That will randomly pick an english word from your array.

    To ask for a translation might be more difficult, since you would have to search your Tagalog array for the string the user enters. One easier option is to list all the words in the tagalog array with a number (like 1 - 50: the array index + 1) and have the user pick the number of the matching word. If the number they pick matches the random number + 1, it is correct. The +1 is because array offsets start at 0 but users usually will think starting from 1.
    Last edited by Daved; 06-05-2005 at 11:12 PM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Maybe use a map<int,pair<string,string> > container? As a demonstration of how it could be setup and the data within accessed:

    Code:
    map<int,pair<string,string> > dictionary;
    
    
    // Insert English/Tagalog strings into the map container
    dictionary.insert(make_pair(0,make_pair(string("Dog"),string("Aso"))));
    dictionary.insert(make_pair(1,make_pair(string("Cat"),string("Pusa"))));
    dictionary.insert(make_pair(2,make_pair(string("Computer"),string("Kompyuter"))));
    dictionary.insert(make_pair(3,make_pair(string("Chair"),string("Silya"))));
    ...
    
    // Pick a random index
    int indx = rand() % dictionary.size();
    
    // Show English/Tagalog side-by-side
    cout << "The Tagalog translation of the English word \"" << dictionary[indx].first
         << "\" is \"" << dictionary[indx].second << "\"." << endl;
    Possible output:
    Code:
    The Tagalog translation of the English word "Chair" is "Silya".
    The setup of the map could be made easier if the English/Tagalog words were read in from a file. Then you wouldn't need some huge set of insert statements, you'd just have a loop that reads through a file. You should be able to take the above a modify it to print out the English word stored in the map, ask the user for the translation, and then compare what the user entered with the translation that is also stored in the map.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    3
    Many thanks for you help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. randomize array blackjack c program
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-01-2005, 02:05 PM
  2. don't want to randomize two alike numbers
    By randomizer in forum C++ Programming
    Replies: 8
    Last Post: 05-26-2005, 07:42 PM
  3. randomize hex digits
    By wazilian in forum C Programming
    Replies: 3
    Last Post: 12-14-2002, 03:20 AM
  4. Problem with Visual C++ ( randomize function )!
    By marCplusplus in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2001, 01:01 PM
  5. Randomize Number Function
    By beyonddc in forum C Programming
    Replies: 3
    Last Post: 12-10-2001, 04:31 AM