-2

This is GA for a timetable problem. I'm trying to create an initial population, but it isn't working as it isn't entering the if condition. can someone point out the error?

I tried inserting statements in each condition, but everything checks out. Still, I don't seem to find a solution.

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
int random_number_creator(int upper, int lower)
{
    int n;
    n = rand() % (upper-lower)+ lower;
    return n;
}

struct pop{
    int subjects[6];
    int days[5][9];
}oldpop, bench;

main()
{
    int i,j,s=1,d,h,stop=1,cou=0;
    for(i=0;i<5;i++)
    {
         for(j=0;j<9;j++)
         if(j!=6)
            bench.days[i][j]=0;
         else
            bench.days[i][j]=11111;
    }

    for(i=0;i<6;i++)
    {   
       if(i<4)
           oldpop.subjects[i]=3;
       else 
           oldpop.subjects[i]=2;
    }
    for(i=0;i<5;i++)
    {
      printf("\n");
      for(j=0;j<9;j++)
        printf("    %d",bench.days[i][j]);  
    }
    for(i=0;i<6;i++)
    {   
        printf(" \n %d",oldpop.subjects[i]);
    }
    cou=0;
    for(i=0;i<6;i++)
    { 
        cou=cou+ oldpop.subjects[i];
    }
    printf("\n%d\n",cou);
    do
    {   
        s=random_number_creator(5,0);
        printf("\nsubject number:%d\n",s);
        printf("\nloop 1 entery %d",cou);
        do
        {   
            printf("\nloop 2 entry\n");
            d=random_number_creator(5,0);h=random_number_creator(8,0);
            printf("\nDay Number:%d   \nHour Number:%d\n",d,h);
            if(bench.days[d][h]==0&&oldpop.subjects[s]!=0)
            {   
                 printf("\nif condition reached\n");
                 oldpop.days[d][h]=10+s;
                 bench.days[d][h]=11111;
                 stop=0;
                 cou--;
                 oldpop.subjects[s]--;
            }

            else  
            {
                printf("\nIf condition not satisified.\n");
                break; 
            }
        }while(stop!=0);
    }while(cou!=0);

    for(i=0;i<5;i++)
    {
        printf("final entery \n");
        for(j=0;j<9;j++)
            printf("    %d",oldpop.days[i][j]);

    }
}

I want the oldpop variable to be initialized for this timetable problem but the code does not enter the if condition in the do while loop.

  • What is your code supposed to do? (Note that it lakes a `}` to compile) Could you add some comments explaining your code? In my test, the `if` block is entered some times. – Mathieu Jul 02 '19 at 15:09
  • 1
    the posted code does not compile! regarding: `if(bench.days[d][h]==0&&oldpop.subjects[s]!=0) { printf("\nif condition reached\n"); oldpop.days[d][h]=10+s; bench.days[d][h]=11111; stop=0; cou--; oldpop.subjects[s]--; else {` This is missing the `}` before the `else` statement. – user3629249 Jul 02 '19 at 15:09
  • 1
    OT: for ease of readability and understanding: 1) place an appropriate space: inside parens, inside braces, inside brackets, after commas, after semicolons, around C operators. 2) follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* 3) variable and parameter names should indicate `content` or `usage` (or better, both) names like `h` and `d` and `s` are meaningless, even in the current context – user3629249 Jul 02 '19 at 15:18
  • OT: the posted code contains several 'magic' numbers. I.E. 5, 6, 9. 'magic' numbers make the code much more difficult to understand, debug, etc Suggest using an `enum` statement or `#define` statements to give those 'magic' numbers meaningful names, then use those meaningful names throughout the code. – user3629249 Jul 02 '19 at 15:25
  • My code is supposed to initialise the variable oldpop i.e, to create a random timetable in the days variable. The structure contains the six subjects, each subject is given the number of hours it has to be taught per week. This oldpop.subjects file consists of number of hours each subjects index is supposed to be taught each week. So each time one subject is placed in the days, it is subtacted from the respective index value fro oldpop.subject[index] is decremented. Hereby all the subjects can be placed in this timetable randomly. – Vinay Mathew Jul 02 '19 at 15:28
  • regarding: `main()` This will cause the compiler to output a warning message, similar to: *untitled.c:18:1: warning: return type defaults to ‘int’ [-Wimplicit-int]*. There are only two valid signatures for `main()` they are: `int main( void )` and `int main( int argc, char *argv[] )` Old compilers (including visual studio) fail to meet the requirements of the C standard – user3629249 Jul 02 '19 at 15:30
  • The objective is to initialise oldpop.days[][] randomly, given that the subjects are 6 and each subject has a minimum number of hours/ week. I tried changing the code several times, but sometimes I get one subject value coming up in the days array having more than the number of hours it should be taught – Vinay Mathew Jul 02 '19 at 15:30
  • regarding: `n = rand() % (upper-lower)+ lower;` Before calling `rand()`, the function `srand()` must be called (only once) early in the `main()` function. The function: `srand()` is usually called via: `srand( (unsigned)time( NULL ) );` to initialize the random number generator – user3629249 Jul 02 '19 at 15:35
  • OT: regarding: `#include #include #include #include #include` it is a poor programming practice to include header files those contents are not used. The header files: `time.h` and `ctype.h` and `math.h` are not used, so suggest removing them – user3629249 Jul 02 '19 at 15:38
  • OT: for ease of readability and understanding (by us humans, the compiler doesn't care) Please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces. – user3629249 Jul 02 '19 at 15:44
  • OT: regarding; *My code is supposed to initialise ...* clarifying information should be in the question, itself, not scattered in the comments to the question – user3629249 Jul 02 '19 at 15:54

1 Answers1

0

The problem in your program comes from the subject selection (after adding the missing }):

s=random_number_creator(5,0); 

Will return a random number between 0 and 4 included.

To correct this, just replace this line by

s=random_number_creator(7,0);

To pick a number between 0 and 6. So the cou variable will be able to reach 0


Your code can be improved:

Instead of this kind of block:

for(i=0;i<5;i++)
{
    printf("final entery \n");
    for(j=0;j<9;j++)
        printf("    %d",oldpop.days[i][j]);

}

Create a function, learn to use printf:

void print_table(struct pop pop, const char *label)
{
    int i, j;
    printf("\n%s\n", label);
    for (i=0;i<5;i++)
    { 
        for (j=0;j<9;j++)
        {
            printf(" %5d",pop.days[i][j]);
        }
    }
}

And use it this way

print_table(oldpop, "oldpop");
Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • Hello, thank you so much this worked. My objective is to create an array of oldpops which have random timetables created for each iteration. So the entire code must run say 5 times to create 5 different timetables. Can you suggest some efficient way to edit the code this way? – Vinay Mathew Jul 02 '19 at 15:36
  • Yes that is a good adittion, i will surely implement it. However if I need to create say 5 random timetables of oldpop, thats is an array of oldpop can you recommend a better method. Thank you soo much. Youve genuinely saved my life, – Vinay Mathew Jul 02 '19 at 15:43