Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6352363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:11:29+00:00 2026-05-24T22:11:29+00:00

I’ve been trying to write a code for two point crossover operation in a

  • 0

I’ve been trying to write a code for two point crossover operation in a genetic algorithm. At first two random gene location is selected. After that, two chromosomes swap their genes which are located btw random numbers called genelocation1 and genelocatıon2.

for example  First Gene [0.3,0.2,0.4,0,0.1,0.5,0.7]
             Second Gene [0.25,0.6,0.45,0.15,0.80,0.9,0.85]
        rndm    genelocation1=3
           rdnm  gnelocation2 =5
child Gene1 [0.3,0.2,0.4,0.15,0.80,0.5,0.7]
      Gene2 [0.25, 0.6, 0.45, 0, 0.1,0.9,0.85]

my problem is this: since two numbers are generated randomly, i could not define an array like array[genelocation2-genelocation1].. How can i solve the problem. here is my whole code about two point crossover. pointers maybe a solution but i am not good at pointers.

Here is the code:

void Xover (int mother,int father)
{
    int tempo;
    int Rndmgenelocation1=(rand()%ActivityNumber);
    int Rndmgenelocation2=(rand()%ActivityNumber);

    if (Rndmgenelocation1>Rndmgenelocation2)//sure that 2>1
    {
        tempo=Rndmgenelocation1;
        Rndmgenelocation1=Rndmgenelocation2;
        Rndmgenelocation2=tempo;
    }

    int size=(Rndmgenelocation2-Rndmgenelocation1);
    int Temp1[size];//this makes an error

    int ppp=Rndmgenelocation1;
    for (int pp=Rndmgenelocation1;pp<Rndmgenelocation2;pp++)
    {
        Temp1[pp]=Sol_list[father].Chromosome[ppp];
        ppp++;
    }
    int pppx=Rndmgenelocation1;
    for (int ppx=Rndmgenelocation1;ppx<Rndmgenelocation2;ppx++)
    {
        Sol_list[father].Chromosome[ppx]=Sol_list[mother].Chromosome[pppx];
        pppx++;
    }
    int ppplx=Rndmgenelocation1;
    for (int pplx=Rndmgenelocation1;pplx<Rndmgenelocation2;pplx++)
    {
        Sol_list[father].Chromosome[pplx]=Temp1[ppplx];
        ppplx++;
    }

    return;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:11:31+00:00Added an answer on May 24, 2026 at 10:11 pm

    You can’t define an array of variable size on the stack.
    You could use

    int *Temp1=new int[size]
    

    You then must not forget to call

    delete[] Temp1;
    

    at the end of your function!

    edit:

    I didn’t test my code below, but the following should do what you want in a more efficient (and more understandable) way:

    #include <algorithm>
    void Xover (int mother,int father)
    {
        int Rndmgenelocation1=(rand()%ActivityNumber);
        int Rndmgenelocation2=(rand()%ActivityNumber);
    
        if (Rndmgenelocation1>Rndmgenelocation2)//sure that 2>1
        {
            std::swap(Rndmgenelocation1,Rndmgenelocation2);
        }
    
        for (int pp=Rndmgenelocation1;pp<Rndmgenelocation2;pp++)
        {
            std::swap(Sol_list[father].Chromosome[pp],Sol_list[mother].Chromosome[pp]);
        }
        return;
    }
    

    edit2:

    I just found here another even better way – the STL implements a ready-to-use cross over algorithm. Use:

    #include <algorithm>
    void Xover (int mother,int father)
    {
        int Rndmgenelocation1=(rand()%ActivityNumber);
        int Rndmgenelocation2=(rand()%ActivityNumber);
    
        if (Rndmgenelocation1>Rndmgenelocation2)//sure that 2>1
        {
            std::swap(Rndmgenelocation1,Rndmgenelocation2);
        }
    
        std::swap_ranges(
            Sol_list[father].Chromosome[Rndmgenelocation1],
            Sol_list[father].Chromosome[Rndmgenelocation2],
            Sol_list[mother].Chromosome[Rndmgenelocation1]
        );
    
        return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.