Thread: Beginner question to shuffling arrays , and input and output to a function

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    Beginner question to shuffling arrays , and input and output to a function

    Hey Everyone.


    I am relatively new to C++, I have only started learning it this year.
    I am having trouble writing a little program to take in a text document and store it in an array. Then take that array and randomise the values in a certain way. It is for a rostering program to arrange the riders + horses into suitable positions.


    What I am trying to attempt in this function (gen() ) is to copy the entry[num] to entry2[num2] . Where num is the amount of entries there are, and num2 is a randomly generated rand() number. I then wanted it to check if the new entry is atleast 15 positions more then the old entry (in this case.. so the rider of the horse can have a break before his next event)


    so as you can guess .. this application doesnt work, im guessing there is an easier way?
    How can I send the variables into this array, And how can I get this function to return the array back to my main .. im guessing via a pointer?


    I have attached the whole code below.


    Thankyou.
    Sam.

    Code:
    /*   ________________________________________________________________________________
    *    |   TITLE:     main.cpp                                                        |
    *    |   AUTHOR:    Samuel Abbott ([email protected])                         |
    *    |   PURPOSE:   to take camp draft data in, randomise it with certain values,   |
    *    |              and output the new roster as a text document                    |
    *    |   DATE:      may 1, 2012                                                     |
    *    |   LAST EDIT: may 3,2012                                                      |
    *    |______________________________________________________________________________|
    */
    
    #include <iostream>
    #include <string>
    #include <stdlib.h> //random number
    #include <time.h> //random number
    #include <fstream> //used for input / output of external files
    
    using namespace std;
    /*
    *   TITLE: gen
    *   PURPOSE: to randomise the entries , check to see if they are more then 15 units apart, and return a pointer to the randomised array
    *   string entry[] holds original values
    *   int num [] holds number of entries
    */
    sting gen(string entry[], int num)
    {
        int count = 0;
        string entry2[num]; //randomised array
    
        /* initialize random seed: */
            srand ( time(NULL) );
        for(count =0; count < num)
        {
            num2 = rand() % num; //generate random number
            entry[num] = entry2[num2]; //copy number from array to a random position in the next array
    
            /* a series of if checks to make sure the entry is no less then 15 positions from the previous entry */
            if(entry2[num2]=entry[num-15])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-14])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-13])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-12])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-11])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-10])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-9])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-8])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-7])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-6])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-5])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-4])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-3])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-2])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]=entry[num-1])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else
            {
                entry[num] = entry2[num2];
            }
        }
        string pointer* = entry2[]
        return pointer
    }
    
    /*
    *   Title : Loading
    *   Purpose: This function generates a loading animation. in other words. just a fancy graphic.
    *   HAS NO LOADING PURPOSE
    */
    
    void loading()
    {
        /* Declare loading variable, this variable is just the screen printout */
        string loading = "...";
        int time = 0;
        int loadtime = 10;
    
        /* print out loading animation */
        while(time != loadtime)
            {
                cout << loading;
                time++;
            }
    
    }
    void loading();
    
    /*
    *   Title: Main
    *   Purpose: this is the main function for the randomisation program
    */
    int main()
    {
        //declare all variables
        string fname = "";
        int count;
    
        string line;
        ifstream inputfile;
        char c;
        int num=0;
    
    
        /* introduction */
        cout << "Roster Randomisation Generator" << endl;
        /* Get input for roster name */
        cout << "Enter a name for the roster, such as 13022012coolah:"<<endl;
        cin>> fname;
        /* Begin generating roster */
        cout << "Roster Generation in progress." << endl;
        loading();
        /* get amount of lines in text file .. used to determine how many entries there are to avoid printing blank spaces*/
    
            inputfile.open ("output.txt");
                while (inputfile.good())
                {
                    c = inputfile.get();
                    if (c=='\n')num++;
                }
            inputfile.close();
        cout<<"Number of Entries counted is: "<<num<<endl;
    
    string entry[num];
    
        cout << "Grabbing entries from entry file..."<<endl;
        /* open text file*/
        inputfile.open ("output.txt");
        /* read all data into the array entry[] */
        for (count = 0; count < num; count++)
            {
                getline (inputfile,line); //Gets a single line from example.txt
                entry[count]=line;
    
               // inputfile >> entry[count];
            }
        inputfile.close();
        cout <<"Found the following entries: " <<endl;
        /* output captured entries*/
        for(count =0; count < num; count++)
            cout <<entry[count] <<endl;
    
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for(count =0; count < num)
    Does this even compile?

    > if(entry2[num2]=entry[num-15])
    All these assignments (=) need to be comparisons (==)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I suggest you write out fifteen times:
    "I will use loops instead of copying and pasting code".

    Well I'm just kidding really, but I'd like to do you a favour by urging you to have a go at converting that repetitive code into a loop. Don't worry, we'll help you if you can't quite get it right.
    If you grader is anything like me then he will also be facepalming at this, and you don't want that.

    Oh another really big hint: Turn your warning levels up and make sure you pay attention to them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    2
    Quote Originally Posted by Salem View Post
    > for(count =0; count < num)
    Does this even compile?

    as i found out .. no

    > if(entry2[num2]=entry[num-15])
    All these assignments (=) need to be comparisons (==)
    EDIT:
    @imalc - I will have a go at it now ... only just noticed your reply.
    i am using code::blocks on linux .. and it only seems to spit out my warning when i hit the compile button.
    i am facepalming myself over this haha.


    so i have made a few more amendments to the code . and i do believe its the for() loop that is causing me grief now .. What would be the best loop to use for this function ?

    i posted my amended code, and the error log. the code starts from line 24

    Code:
    string gen(string[], int num)
    {
        int count = 0;
        int num2 = 0;
        string entry2[num]; //randomised array
    
        /* initialize random seed: */
            srand ( time(NULL) );
        for(count =0; count < num)
        {
            num2 = rand() % num; //generate random number
            entry[num] = entry2[num2]; //copy number from array to a random position in the next array
    
            /* a series of if checks to make sure the entry is no less then 15 positions from the previous entry */
            if(entry2[num2]==entry[num-15])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-14])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-13])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-12])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-11])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-10])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-9])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-8])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-7])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-6])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-5])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-4])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-3])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-2])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else if(entry2[num2]==entry[num-1])
            {
                num2 = rand() % num; //generate random number
                entry[num] = entry2[num2]; //copy number from array to a random position in the next array
            }
            else
            {
                entry[num] = entry2[num2];
            }
        }
        string &pointer = entry2[];
        return pointer;
    }

    error log:

    /home/samuel/Projects/campd/campdraft/main.cpp||In function ‘std::string gen(std::string*, int)’:|
    /home/samuel/Projects/campd/campdraft/main.cpp|32|error: expected ‘;’ before ‘)’ token|
    /home/samuel/Projects/campd/campdraft/main.cpp|35|error: ‘entry’ was not declared in this scope|
    /home/samuel/Projects/campd/campdraft/main.cpp|118|error: expected primary-expression before ‘]’ token|
    ||=== Build finished: 3 errors, 0 warnings ===|
    Last edited by Samuel Abbott; 05-03-2012 at 01:32 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I guess it's because your for loop construct is still broken.

    Go read your book again, and note how many expressions and ; a proper for loop has.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Code:
    and it only seems to spit out my warning when i hit the compile button.
    thats right, because you are not using an interpreter.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    There are actually two problems here. The first one is to find an algorithm that re-orders the array the way you want, and the second one is to translate it into a correct c++ program.

    To re-arrange the array so that all elements move at least 'n' positions AND to do it in a random way is tricky.

    Your attempt to solve this is - as far as I understand it:

    Code:
    for each array index
      repeat
        new_index = random
      until distance(old_index, new_index) >= 15
      copy element
    end
    But does it work? Let's say your array size is 7 and the minimum distance is 2.

    Code:
    [a][b][c][d][e][f][g] original array
    
    re-arrange the elements:
    
    [ ][ ][ ][ ][ ][ ][a] a: random index 6 -> OK
    [ ][ ][ ][b][ ][ ][a] b: random index 3 -> OK
    [c][ ][ ][b][ ][ ][a] c: random index 0 -> OK
    [c][d][ ][b][ ][ ][a] d: random index 1 -> OK
    [c][d][e][b][ ][ ][a] e: random index 2 -> OK
    [c][d][e][b][ ][ ][a] f: no valid position left...
    See? It may or may not work, depending on the random index sequence.

    You should try to find an algorithm (that is guaranteed to work) first, and then figure out how to implement it in c++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question for a beginner with arrays
    By Student12321 in forum C Programming
    Replies: 5
    Last Post: 04-08-2012, 12:10 PM
  2. Shuffling Arrays
    By ISUFrosh in forum C Programming
    Replies: 5
    Last Post: 11-03-2010, 08:32 PM
  3. Beginner's question concerning pointers and arrays
    By rcomj in forum C Programming
    Replies: 5
    Last Post: 08-11-2009, 09:09 AM
  4. Input / Output help (arrays)
    By fp123 in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2006, 12:30 PM
  5. shuffling arrays?
    By Munkey01 in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2003, 05:00 PM