Short Shuffle for 1D Array using C++

miteigi-san 0 Tallied Votes 362 Views Share

NOTE:
~this code is in c++
~you need not change anything in the code
~the commented lines are there just in case you want to see how the program works in real time - simply uncomment them and run the program, these commented lines are not necessary for the program to work
~code does not swap the characters of the string n times just to randomize
~string contents are shuffled character by character only
~the code is done with Code::Blocks

HOW THE CODE WORKS:
~the program asks user to input a string, then it outputs the shuffled version of the string
1. the program tells the user to enter a string
2. getline(cin,word); saves the string into string variable word
3. the length of the string is determined and saved to int variable x
4. srand is called so that the program will randomize differently for each instance you run the program
5. the while loop does the assigning of characters found in string variable word to temp
6. the resulting string temp is outputted so you can see the result of the shuffle

~further explanation for number 5
for example, you entered "twister"
twister is saved to string variable word
x is given the value of the length of the string variable word, which in this example, is 7 (seven)
in the loop,
temp is the string where we add characters found in string variable word. the character that we will use and copy to temp is determined by the int variable y, which generates a value from 0(zero) to the length of the string variable minus one, which in this instance, is 6. after choosing the character, the character is appended to temp.
the problem now is, we dont want that same character to be used again/selected by the randomized selector y - this problem is solved by the other lines in our program.
the idea is, we remove that chosen character (after appending it to temp) from the original string word, so it would not be chosen again.
we can remove that character by:
1.) cutting the string into three, first part will be from the start of the string to the character before the chosen character, second part will be the chosen character, then the last part will be after the chosen character to the last of the string... after cutting it and saving the first and third(last) parts to new strings, we fuse them into one....or
2.) simply assigning the last character of the string into the place of the chosen character (which will overwrite and delete the chosen character) , then cutting the string from the beginning up to the second-to-the-last part of the string (which will delete the last character of the string - which is okay since we moved it to the chosen character's place already)
after that, you see, we don't have the chosen character anymore, we have the last character of the string on it, and the length of the string decreased by one.
the loop then continues until all of the characters from string variable word is gone.

INPUT RESTRICTIONS:
~actually from what i tested, there are no restrictions for the program input...
~it even accepts strings with spaces
~there are no restrictions for values of the string variable word too.

uhm i think thats all about the code. i don't really know if this kind of code exists some place else other than this post because all i see are either random swapping shufflers or threads that needs help on shuffling. and just so you know, it is me who made this code, so yes its my mistake if ever there is one in the code.

#include<iostream>

using namespace std;

main(){
    int x, y;
    string word,temp;

    cout<<"Enter the word you want to shuffle : ";
    getline(cin,word);

    x=word.length();

    srand(time(0));
    while(x!=0){
        int y=rand()%x;
        //cout<<"when x is "<<x<<" the y generated is "<<y;
        temp+=word[y];
        //cout<<" temp is now is "<<temp;
        word[y]=word[x-1];
        word=word.substr(0,(x-1));
        //cout<<" and the word is "<<word<<endl;
        x--;
    }
    //cout<<"The final shuffled word is ";
    cout<<temp;
}
dusktreader 137 Posting Whiz in Training

I hate to rain on your parade, but as long as you are using classes from the standard template library (string), you might as well use methods from the library as well:

#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[]){
    string alphabet = "abcdefghijklmnopqrstuvwxyz";
    random_shuffle( alphabet.begin(), alphabet.end() );
    cout << "alphabet after shuffling: " << alphabet << endl;
    return 0;
}

As you can see, the shuffling can be done with a single line by invoking the random_shuffle algorithm from the <algorithm> library.

commented: You are right. +26
miteigi-san 24 Newbie Poster

Im only a beginner so I didn't know about that, but thanks anyway, at least now i know something like that existed. makes life more easier = ).

Thank you! at least someone noticed my post and helped me with it = ).

Nick Evan commented: That's the spirit! +12
dusktreader 137 Posting Whiz in Training

Glad you appreciated the reply. If you are a beginner, you should read up on everything that is available in the standard template library. There's a lot of really useful stuff there, and it can save you a lot of time!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.