Click here to Skip to main content
15,892,797 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to use an array of structure in a function.Could anybody help me with a sample?
I have written the code as below:
C++


...
C#
struct Chrom             // creating the chrom structure
   {
      int Num_Chrom;
      unsigned int Gen[Chrom_Size];
      int Fit;
   };                   // now we have a chrom type that we can use

void Initial_Pop(Chrom[], int);

...
C#
void Initial_Pop(Chrom PopInitial[], int Pop_Size){
int i=0,j=0;
unsigned int Random;
unsigned int temp1[Num_VehicleKind-1];
unsigned int temp2[Num_Depot-1];
    for(j=0;j<Pop_Size;j++){                      // loop of j to choose chromes from [0] to [pop_size]
        for(i=0;i<(Num_VehicleKind-1);i++){       // loop of i to generate (Num_VehicleKind-1) random integer
            temp1[i]=1+rand()%Num_Depot;      // make the random integer value between 1 and (Num_Depot)

        }//end of for(i)

        for(i=0;i<(Num_Depot-1);i++){            // loop of i to generate (Num_Depot-1) random integer
            temp2[i]=1+rand()%Num_VehicleKind;   // make the random integer value between 1 and (Num_VehicleKind)
            }//end of for(i)

        int k1=(Num_VehicleKind-1);
        int k2=(Num_Depot-1);
        int p=(Num_VehicleKind+Num_Depot-2);
        while(k1==-1 || k2==-1){           // loop of i to generate (Num_Depot-1) random integer
            //Random=rand();                       // creating random value
            Random=(rand()%2);
            if (Random==0){                                    // make the random integer value between 1 and (Num_VehicleKind)
                PopInitial[j].Gen[p]=temp1[k1];
                k1--;
                p--;
            }//end of if
            else{
                PopInitial[j].Gen[p]=temp2[k2];
                k2--;
                p--;
            }//end of else
            }//end of while


when I run the program step by step, it broke in line
PopInitial[j].Gen[p]=temp1[k1];
Posted
Comments
[no name] 7-Jul-12 17:32pm    
"when I run the program step by step, it broke in line" means absolutely nothing to anyone but you.
nv3 7-Jul-12 18:07pm    
You probably didn't pass a correctly allocated array of at-least j Chrom objects. Unfortunately you didn't show us the calling sequence of your function.
Sandeep Mewara 8-Jul-12 2:50am    
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.

1 solution

C++
while(k1==-1 || k2==-1){           // loop of i to generate (Num_Depot-1)

You then use these values as indices into some array. Arrays are indexed from 0 to (positive)N; any negative index will try to address memory that does not belong to the array and will therefore cause problems.
 
Share this answer
 
Comments
Z.S.H 8-Jul-12 13:16pm    
I found my problem. The problem was exactly what Richard MacCutchan said. Thanks Richard MacCutchan.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900