Thread: Array of Arrays

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    82

    Array of Arrays

    Title self explainitory

    I've been trying to create a population of individuals each of which contains a string/or array of numbers.

    For example: Individual A: 110010101101

    then basically * 100 to get 100 copies.

    I need to be able to call each individual and then call each element of the individual so i can mutate them.

    here is what i tryed out:

    Code:
    void GetPopulation(int (&Output)[21], int (&Pop)[PopSize][21])
    
    {
        int index;
        int j;
        
        for ( index = 0; index < PopSize; index++)
        {
            for ( j = 0; j < 21; j++)
            
                {
                        int Out;
                        Out = Output[j];
             
                        Pop[index][j] = Pop[index][Out];
                        cout << Pop[index][j];
            cin.get();
                }    
                       
       
        }    
        
    }
    Output is our Individual, and contains 21 int's of (1,0)

    I was hoping the code would populate 100 arrays Pop[index]
    each of which would contain a copy of the original individual
    Pop[j].

    Then try and print out what is in the arrays: ie

    Pop[1st ind][1-21] = "01010101010101" etc

    then on to the next ad infinitum.

    Sadly it only prints out "0" then crashes at the end

    Output and Pop are both declared in Main()

    int Pop[PopSize][21];

    Popsize is defined as 100

    Is there anything grossly wrong,

    And i understand that there maybe better ways of doing it, ie someone suggesting Vectors etc but until i learn about them i'd rather stick with plain old arrays

    Unless i fundementally have to change it

    Thanks for any help

    Regards Wolfe

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    umm, your crash might be due to a incorrect loop size like in your for loop ( if it is 1 to big or to small your program will crash sometimes )

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Well i found out what is wrong i think.

    this part

    int Out;
    Out = Output[j];

    Pop[index][j] = Pop[index][Out];
    cout << Pop[index][j];

    Having Out as what is in [j] at the time may sound fine and dandy but stupid of my to think it would go into the array like that.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    there we go works but still crashes.

    Pop[index][j] = Output[j];
    cout << Pop[index][j];

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > void GetPopulation(int (&Output)[21], int (&Pop)[PopSize][21])
    1. What are those & for ?
    2. How are you calling this function
    3. Do you get any warnings when you compile the code.
    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
    Join Date
    Oct 2005
    Posts
    82
    The &'s are for referencing both Output and Pop which are in main.

    GetPopulation(Output, Pop);

    is how function is called.

    It works now.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you are trying to get the program to work and not make it so hard on yourself, you should really switch to strings and vectors instead of arrays.

    "10001010101" is basically a string, therefore there is not much reason to treat it as an array of ints.

    If you want multiple copies of this, you can put them all in a vector of strings (vector<string> Individuals) where you can easily access and manipulate each member.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Cdrwolfe
    The &'s are for referencing both Output and Pop which are in main.
    You don't need them arrays are automatically passed by refference. Or by pointer, depending on how you look at it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Didn't realise that, thanks.

    Well here is the whole thing for anyone interested.

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #define RAND_MAX 100
    #define srand 12
    #include <ctime>
    #define PopSize 10
    #define GATarget 1111111111
    
    
    
    
    using namespace std;
    using std::string;
    using std::cout;
    using std::endl;
    using std::cin;
    
    void GetUserInput(string &Input)
    
    {
        cout << "Please enter Amino Acid Sequence \"H,P\": " << endl;
        cin >> Input;
         
        cin.get();
    }
    
    void GetSetupString(string theInput, int (Output)[10])
    // To declare an Array as a reference from Main, syntax is differant then other 
    // references, ie: Type (&ArryName)[Size], try finding that in any tutorial :) 
    
    
    {
    
    
       int index;
       
       for (index = 0; index < theInput.length(); ++index)
       {
           switch (theInput[index])
           {
               case 'H':
                   Output[index] = 1; 
                   break;
               case 'P':
                   Output[index] = 0;
                   break;
               default:
                   break;
        
           }   
            cout << Output[index] ;
       }  
    
    cin.get();
    }    
    
    void GetPopulation(int (Output)[10], int (Pop)[PopSize][10])
    
    {
        int index;
        int j;
        
        for ( index = 0; index < PopSize; index++)
        {
            for ( j = 0; j < 10; j++)
            
                {
                       
                        Pop[index][j] = Output[j];
                        cout << Pop[index][j];
            
                }    
             cin.get();          
       
        }    
        
    }
    
    
    
    
    int GetMutation (int (Pop)[PopSize][10])
    
    {
        //Don't forget to seed rand() each time you run the program, 
        
        int index;
        int j;
        int Num = 0;
        
        do
        {
        
            for ( index = 0; index < PopSize; index++)
            {
                for ( j = 0; j < 10; j++)
            
                {
            
                    if (rand()%100 > 50)
            
                    {
                
                
                            if ( rand()%100 < 5)
                                        {
                                         Pop[index][j] = 0;
                                        }
                            else
                                        {
                                        Pop[index][j] = 1;
                                        }
                                        Num++;
                    }
                
                    else
            
                    {
                      //do nothing
                    }
            
            cout <<  Pop[index][j] ;
            
                }    
          cout << endl;//cin.get(); 
            }        
        }                
        while (Pop[index][j] = GATarget);
                
        
        
    cin.get();
          
    }
       
    int main ()
    
    {
        string Input;
        int Output[10];
        int Pop[PopSize][10];
        //int size = Input.size();
    
    
        GetUserInput(Input);
        GetSetupString(Input, Output);
        
        GetPopulation(Output, Pop);
        
        GetMutation(Pop);
    }
    Changed the mutation function so it runs through the entire population.

    Also added a do while loop so that it continues until it matches some kind of target. Of which i haven't qutie got to work yet .

    Now to try and build a cross over function, a little bit harder.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There are still some errors:

    Concerning seeding the random number generator - I see you have included <ctime> (that's where the time functions are) but you don't seem to be using it anywhere. The point about putting
    Code:
    srand((unsigned)time(NULL))
    in the main is that it produces a unique number (current calendar time) each time you run the program, and as a result the rand() function will produce a unique series of numbers each time.

    Code:
    #define srand 12
    This doesn't seed the RNG. All it does is tells the compiler to replace each case of 'srand' with '12' in the code while compiling.

    Then, why are you still redefining RAND_MAX?

    Code:
    using namespace std; //what's the point of this
    using std::string;           //... if you use this directives?
    using std::cout;
    using std::endl;
    using std::cin;
    Another potential problem is using literal constants (like 10) everywhere. It would be easier to define the constants once and then use the constant. This way, if you want to change any of them later (and you seem to be doing this a lot) you wouldn't have to go through the whole code to replace all 10-s.
    Last edited by anon; 07-11-2006 at 01:50 PM.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Don't #define them, use global const int.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Here is a Half fnished single bit crossover operator i was trying out using simple coding that of which i only know

    It compiles but i have yet to see if it really works.

    Code:
    void GetCrossover(int (Pop)[PopSize][10])   
    {
        int NewPop[PopSize][10];
        int NewPop2[PopSize][10];
        int index;
        int j;
        int Inc;
        
       for (index = 0; index < PopSize; index++)
          {
              for (j = 0; j < 10; j++)
              {
                  NewPop[index][j] = Pop[index][j];
                  NewPop2[index][j] = Pop[index][j];
              }
          }     
          
                
       for (index = 0; index < PopSize; index++)
       {
           Inc = index;
           
         char  Check = 'Y';   // Reinitialise Check to 'Y' for each loop
           
           for (j = 0; j < 10; j++)
           {
               if ( Check != 'X')
               {
                   // If Not 'X' the perform this block
                          if (rand()%100 > 50)
                          {
                              Inc = index;
                          
                              Check = 'X';
                              Pop[index][j] = NewPop[++Inc][j];
                          }
                          else
                          {
                              Pop[index][j] = NewPop[index][j];
                          }    
               }  
                 
               else
               // If Check = 'X' the perform this block
               {
               
               Pop[index][j] = NewPop[++Inc][j];               
               }
               
               cout << Pop[index][j];
              
           }
           cout << endl;
           cin.get();
         }              
                           
       
    }
    Thought some people might be interested, to tired at the moment to finish it off.

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Do While loop help if possible.

    Didn't think it needed a new thread.

    I was trying to get a do while loop in Main () to continously run 3 functions until it reaches a condition

    Code:
    int main ()
    
    {
        string Input;
        int Output[10];
        int Pop[PopSize][10];
        //int size = Input.size();
    
    
        GetUserInput(Input);
        GetSetupString(Input, Output);
        
        do 
        {
        GetPopulation(Output, Pop);
        GetMutation(Pop);
        GetCrossover(Pop);
         }         
        while (Pop[PopSize][10] != {1,0,1,0,1,0,1,0,1,0}); <-- error?
       
    }
    But i get an error which seems simple enough to figure out yet i can't seem to do it.

    syntax error before `{' token

    I'am thinking it must do with {1,0,1,0,1,0,1,0,1,0} but i'am not sure.

    Any help is appreciated.

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    is Pop[PopSize][10] a 2-D array? If it is then I don't think the
    Code:
    {1,0,1,0,1,0,1,0,1,0}
    thing will work. I'd say make a bool type function, and pass the array into the function, and if it is ok, return true, or else return false. Then put that function into the do while loop. Have the function have a nested for loop or something.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can't compare the arrays like that.

    What you could do is make another array that contains the desired results. Then there are several things you could try:

    1) just loop through both arrays checking if all elements are equal

    2) a somewhat easier way would be to try the equal algorithm from the <algorithm> library.

    3) the easiest way (as always) to achieve your goal is to use a string instead of the int arrays, which lets you simply compare the population string with a test string using the == operator

    Code:
    std::string population;
    std::string test = "101010101010";
    do {
        GetPopulation(population);
    } while (population != test);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. pointer to array of arrays
    By linuxdude in forum C Programming
    Replies: 3
    Last Post: 10-03-2006, 12:25 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:07 PM