program help

This is a discussion on program help within the C++ Programming forums, part of the General Programming Boards category; I am writing a program that takes the alphabet and randomly mixes it up and compares it to the alphabet ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    program help

    I am writing a program that takes the alphabet and randomly mixes it up and compares it to the alphabet (you have to compile it to fully understand) i got as far as making a random mixed alphabet but sometimes letters are used more than once, what do i put in to make sure that no letter is used more than once?

    here's what i got so far
    Code:
    
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    using namespace std;
    
    void crypt();
    int rand(int n);
    
    
    char *alphabet[26] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    int main() {
        int i, n; 
        srand(time(NULL));
        
        while(1) {
              cout<< "type 1 start type 0 to exit"<<endl;
              cin>> n;
              if (n == 0)
              break;
             
             if ( n == 1) {
              for (i=1; i<=26; i++)
              crypt();
              cout<<endl; 
              cout<< "a b c d e f g h i j k l m n o p q r s t u v w x y z"<<endl;
              }
             
              }
              return 0;
              }
              
              void crypt() {
                   int a;
                   a = rand(26);
                   
                   cout<< alphabet[a]<<" ";
                   
                   }
                   
                   int rand(int n)
                   {
                       return rand() % n;
                       }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    char[] Alphabet = "abcdefghijklmnopqrstuvwxyz";
    std::random_shuffle(Alphabet, Alphabet + 26); // located in <algorithm>
    Besides, you should use <cstdlib>, <ctime> and <cmath> over their deprecated .h alter-egos.

    Edit: Can I also mention that you aren't using anything out of both the math.h and stdlib.h header files ?

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    srand() and rand() are both in stdlib
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You don't randomly mix up the letters at all, you just pick random letters out of them. You may very well get lots of repeats.

    Example:
    Code:
    [a, b, c, d, e]
    rand(5) = 6 % 5 = 1 (a)
    rand(5) = 17 % 5 = 2 (b)
    rand(5) = 1 % 5 = 1 (a) //repeat!
    You'll need to make sure letters which have already been printed can never be choosen again. You could shuffle the array, as suggested; it's prolly the simplest way. You could also add tokens to letters (eg. a second list of booleans) if they have already been printed, and check letters for existing tokens before printing them again. Slow and hackish though.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    2
    (i'm pretty new to c++) Can somebody explain how to use the shuffle array thing?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,524
    An article will probably help. Here's something concise:
    Using the random_shuffle() Algorithm to Randomize a Sequence of Elements

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    On many implementations, before you call random_shuffle you need to call srand once to seed the random number generator just like you would for rand(). You also have to #include <algorithm>. Then, you call random_shuffle just like Desolation showed, with the first argument being the start of the array and the last being one past the end.

    If you call random_shuffle once, it will shuffle everything in your array. Then you can go about comparing it to the actual alphabet like you had planned.

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    And some other popular implementations of random_shuffle() doesn't use rand() and srand() at all ( e.g. the one that comes with g++ 3.3.5 ).
    in that case you have to write your own RandomGenerator function object to be able to seed the random function.
    e.g.
    Code:
    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    struct rndgen {
        int operator ()( int num ) {
            static bool isinitialized = false;
            if ( ! isinitialized ) {
                srand(time(0)); isinitialized= true;
            }
            return rand() % num;    
        }
    };
    
    int main() {
        char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
        rndgen r;
        random_shuffle(alphabet, alphabet+strlen(alphabet), r );
        cout << alphabet << endl;
    }
    Kurt
    Last edited by ZuK; 08-12-2006 at 05:58 AM.

  9. #9
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    The random_shuffle is probably better. If you wanted to write your own, you might want to just try picking 2 random numbers between 0 and 25 and just swapping the letters at those indexes.

    Code:
    char alphabet[]="abcdefghijklmnopqrstuvwxyz";
    char temp;
    
    int random_num1=rand()%25;
    int random_num2=rand()%25;
    
    temp=alphabet[random_num1];
    alphabet[random_num1]=alphabet[random_num2];
    alphabet[random_num2]=temp;
    
    rinse and repeat.....
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you want to write it yourself the algorithm is pretty simple although it is easy to get wrong. You start from the end and pick a random number from 0 to the that index, swapping with the value at the random index. Then move to the next index down and continue until there is only one index left.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Daved: It's called the Knuth shuffle (or Fisher-Yates shuffle).
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21