Thread: can a C program run for more than 3 days?

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    18

    can a C program run for more than 3 days?

    I have written a C program. Which is running from 2 days. Till now, it has not given any output. On terminal ps-aux | grep a.out command shows 100% CPU utilization and .2% memory utilization.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    char *c,*temp;
    int k1,k2,p=0;
    int power_five_check(char *w)
    {
      int pos,p,l,k=0,i,l1;
      l1=strlen(w);
      if(l1>=5)
      {
      for(pos=0;pos<=l1-5;pos++)
        {
          l=(strlen(w)-pos)/5;
          for(p=1;p<=l;p++)
            {
              for(i=0;i<p;i++) 
                {
                  if(w[pos+i]==w[pos+i+p] && w[pos+i+p]==w[pos+i+2*p] && w[pos+i+2*p]==w[pos+i+3*p] && w[pos+i+3*p]==w[pos+i+4*p]) k++;
                }
              if(k==p) {return(1);}
            }
          k=0;
        }
      }
    return 0;
    }
    
    int binary()
    {
    int i,b[100],a[100];
    for(a[0]=0;a[0]<2;a[0]++)
    {
    for(a[1]=0;a[1]<2;a[1]++)
    {
    for(a[2]=0;a[2]<2;a[2]++)
    {
    for(a[3]=0;a[3]<2;a[3]++)
    {
    for(a[4]=0;a[4]<2;a[4]++)
    {
    for(a[5]=0;a[5]<2;a[5]++)
    {
    for(a[6]=0;a[6]<2;a[6]++)
    {
    for(a[7]=0;a[7]<2;a[7]++)
    {
    for(a[8]=0;a[8]<2;a[8]++)
    {
    for(a[9]=0;a[9]<2;a[9]++)
    {
    for(a[10]=0;a[10]<2;a[10]++)
    {
    for(a[11]=0;a[11]<2;a[11]++)
    {
    for(a[12]=0;a[12]<2;a[12]++)
    {
    for(a[13]=0;a[13]<2;a[13]++)
    {
    for(a[14]=0;a[14]<2;a[14]++)
    {
    for(a[15]=0;a[15]<2;a[15]++)
    {
    for(a[16]=0;a[16]<2;a[16]++)
    {
    for(a[17]=0;a[17]<2;a[17]++)
    {
    for(a[18]=0;a[18]<2;a[18]++)
    {
    for(a[19]=0;a[19]<2;a[19]++)
    {
    for(a[20]=0;a[20]<2;a[20]++)
    {
    for(a[21]=0;a[21]<2;a[21]++)
    {
    for(a[22]=0;a[22]<2;a[22]++)
    {
    for(a[23]=0;a[23]<2;a[23]++)
    {
    for(a[24]=0;a[24]<2;a[24]++)
    {
    for(a[25]=0;a[25]<2;a[25]++)
    {
    for(a[26]=0;a[26]<2;a[26]++)
    {
    for(a[27]=0;a[27]<2;a[27]++)
    {
    for(a[28]=0;a[28]<2;a[28]++)
    {
    for(a[29]=0;a[29]<2;a[29]++)
    {
    for(a[30]=0;a[30]<2;a[30]++)
    {
    for(a[31]=0;a[31]<2;a[31]++)
    {
    for(a[32]=0;a[32]<2;a[32]++)
    {
    for(a[33]=0;a[33]<2;a[33]++)
    {
    for(a[34]=0;a[34]<2;a[34]++)
    {
    for(a[35]=0;a[35]<2;a[35]++)
    {
    for(a[36]=0;a[36]<2;a[36]++)
    {
    for(a[37]=0;a[37]<2;a[37]++)
    {
    for(a[38]=0;a[38]<2;a[38]++)
    {
    for(a[39]=0;a[39]<2;a[39]++)
    {
    for(a[40]=0;a[40]<2;a[40]++)
    {
    for(a[41]=0;a[41]<2;a[41]++)
    {
    for(a[42]=0;a[42]<2;a[42]++)
    {
    for(a[43]=0;a[43]<2;a[43]++)
    {
    for(a[44]=0;a[44]<2;a[44]++)
    {
    for(a[45]=0;a[45]<2;a[45]++)
    {
    for(a[46]=0;a[46]<2;a[46]++)
    {
    for(a[47]=0;a[47]<2;a[47]++)
    {
    for(a[48]=0;a[48]<2;a[48]++)
    {
    for(a[49]=0;a[49]<2;a[49]++)
    {
    for(a[50]=0;a[50]<2;a[50]++)
    {
    for(a[51]=0;a[51]<2;a[51]++)
    {
    for(a[52]=0;a[52]<2;a[52]++)
    {
    for(a[53]=0;a[53]<2;a[53]++)
    {
    for(a[54]=0;a[54]<2;a[54]++)
    {
    for(a[55]=0;a[55]<2;a[55]++)
    {
    for(a[56]=0;a[56]<2;a[56]++)
    {
    for(a[57]=0;a[57]<2;a[57]++)
    {
    for(a[58]=0;a[58]<2;a[58]++)
    {
    for(a[59]=0;a[59]<2;a[59]++)
    {
    for(a[60]=0;a[60]<2;a[60]++)
    {
    for(a[61]=0;a[61]<2;a[61]++)
    {
    for(a[62]=0;a[62]<2;a[62]++)
    {
    for(a[63]=0;a[63]<2;a[63]++)
    {
    for(a[64]=0;a[64]<2;a[64]++)
    {
    for(a[65]=0;a[65]<2;a[65]++)
    {
    for(a[66]=0;a[66]<2;a[66]++)
    {
    for(a[67]=0;a[67]<2;a[67]++)
    {
    for(a[68]=0;a[68]<2;a[68]++)
    {
    for(a[69]=0;a[69]<2;a[69]++)
    {
    for(a[70]=0;a[70]<2;a[70]++)
    {
    for(a[71]=0;a[71]<2;a[71]++)
    {
    for(a[72]=0;a[72]<2;a[72]++)
    {
    for(a[73]=0;a[73]<2;a[73]++)
    {
    for(a[74]=0;a[74]<2;a[74]++)
    {
    for(a[75]=0;a[75]<2;a[75]++)
    {
    for(a[76]=0;a[76]<2;a[76]++)
    {
    for(a[77]=0;a[77]<2;a[77]++)
    {
    for(a[78]=0;a[78]<2;a[78]++)
    {
    for(a[79]=0;a[79]<2;a[79]++)
    {
    for(a[80]=0;a[80]<2;a[80]++)
    {
    for(a[81]=0;a[81]<2;a[81]++)
    {
    for(a[82]=0;a[82]<2;a[82]++)
    {
    for(a[83]=0;a[83]<2;a[83]++)
    {
    for(a[84]=0;a[84]<2;a[84]++)
    {
    for(a[85]=0;a[85]<2;a[85]++)
    {
    for(a[86]=0;a[86]<2;a[86]++)
    {
    for(a[87]=0;a[87]<2;a[87]++)
    {
    for(a[88]=0;a[88]<2;a[88]++)
    {
    for(a[89]=0;a[89]<2;a[89]++)
    {
    for(a[90]=0;a[90]<2;a[90]++)
    {
    for(a[91]=0;a[91]<2;a[91]++)
    {
    for(a[92]=0;a[92]<2;a[92]++)
    {
    for(a[93]=0;a[93]<2;a[93]++)
    {
    for(a[94]=0;a[94]<2;a[94]++)
    {
    for(a[95]=0;a[95]<2;a[95]++)
    {
    for(a[96]=0;a[96]<2;a[96]++)
    {
    for(a[97]=0;a[97]<2;a[97]++)
    {
    for(a[98]=0;a[98]<2;a[98]++)
    {
    for(a[99]=0;a[99]<2;a[99]++)
    {
            for(i=0;i<100;i++) b[i]=a[i];
            c=(char *)malloc(sizeof(char)*102);
            k1=0;
            k2=0;
            while(k2<100) {if(b[k1]==0) {c[k1]=temp[k2];k2++;k1++;} if(b[k1]==1) {k2++;c[k1]=temp[k2];k1++;k2++;}}
            c[k1]='\0';
            b[100]='\0';
            if(power_five_check(c)) {free(c); return(1);} 
            free(c);
    }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
    return 0;
    }
    int grosshopper_morph_x_5(char *w, char *hx, char *hy)
    {
    temp=(char *)malloc(sizeof(char)*103);
    int i,p,p1,p2; 
    temp[0]='\0';
    for(i=0;i<strlen(w);i++)
    {
    if(w[i]=='a') strcat(temp,hx);
    else strcat(temp,hy);
    }
    p=binary();
    return(p);
    }
    int main()
    {
    FILE *fp1,*fp2,*fp3;
    int i;
    int p1,p2,p3;
    const size_t line_size1=13;
    char *line1 =(char *)malloc(line_size1);
    const size_t line_size2=13;
    char *line2 = (char *)malloc(line_size2);
    const size_t line_size3=13;
    char *line3 = (char *)malloc(line_size3);
    
    fp1=fopen("/home/mrityunjay/Desktop/testa.txt", "r+");
    while(fgets(line1, line_size1, fp1)!=NULL){
    p1=strlen(line1);
    line1[p1-1]='\0';
    fp2=fopen("/home/mrityunjay/Desktop/testb.txt", "r+");
    while(fgets(line2, line_size2, fp2)!=NULL){
    p2=strlen(line2);
    line2[p2-1]='\0';
    i=0;
    fp3=fopen("/home/mrityunjay/Desktop/test.txt", "r+");
    while(fgets(line3, line_size3, fp3)!=NULL){
    p3=strlen(line3);
    line3[p3-1]='\0';
    if(!grosshopper_morph_x_5(line3,line2,line1))
    {
    i++;
    }
    }
    fclose(fp3);
    if(i>=331)
    {
    printf("%s\n",line1);
    printf("%s\n",line2);
    }
    }
    fclose(fp2);
    }
    fclose(fp1);
    free(temp);
    free(line1);
    free(line2);
    free(line3);
    return 0;
    }
    Will this program give output or not?
    How many for loop can be nested in C and C++ program?
    I need more nesting of for loops, at least up to 256 for loops.
    I have bee uploaded my files test*.txt.
    test.txt
    Attached Files Attached Files
    Last edited by mrityunjay23; 02-28-2017 at 01:35 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    It can run for as long as you want it to and as long as your hardware and operating system permit it.

    There's no theoretical limit to how many loops you can nest inside each other, although your hardware does realistically impose that limit. Sometimes, even your compiler may have a hard limit on how many nestings you can do.

    I looked into your code a bit, and besides just experimenting with the language, why are you doing this? There are far more efficient algorithms to do what you're trying to do, I think( I couldn't understand exactly what you're trying ).

    Why don't you explain to us what you're trying to do?
    Last edited by GReaper; 02-28-2017 at 02:36 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    It looks like the program is going to run 2^99 or 2^100 loops. At a billion loops per second, it would take more than 5 million years.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,620
    > for(a[0]=0;a[0]<2;a[0]++)
    Assume this takes 1nS on a really fast machine.

    > for(a[30]=0;a[30]<2;a[30]++)
    Every time you nest the loop, you double the run-time.
    So around about here, you're up to 1 second of run-time.

    > for(a[55]=0;a[55]<2;a[55]++)
    Now it's 1 year of run-time.

    At a[62], you're talking more than a lifetime.

    At a[99], you're at 100x the age of the universe.

    > I need more nesting of for loops, at least up to 256 for loops.
    You either need to find a universe with a much longer lifespan, or find a much better algorithm for your problem than try every possible combination.

    You also need to learn how to indent code, but with 100 nested loops - sheesh!
    Indent style - Wikipedia
    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.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    Let
    Code:
    abbabbabaab
    be a string on
    Code:
    {a,b}
    .
    Now, we make some subword from string.
    We have to make all substring which is of the form in which we can skip at most one symbol.
    eg.
    Code:
     a..ba..bab..ab
    double dot .. means skipped symbol. So, this sub string becomes
    Code:
    abababab
    . By this way we can get many such substring of string
    Code:
    abbabbabaab
    . We have to check that all these substring does not contain 5 consecutive sustrings.
    Let see by example, what is mean by 5 consecutive sub string.
    The string
    Code:
    ccabababababaca
    contains
    Code:
    ababababab
    , 5 consequtive substring ab. Where as
    the string
    Code:
    ccabcababababaca
    does not contain 5 consequtive x. where x is any string on
    Code:
    {a,b,c}
    . How to check it this whole conditions efficiently for any string?
    Last edited by mrityunjay23; 02-28-2017 at 03:24 AM.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Finding repetitions of arbitrary length inside a string isn't trivial. If it was, there would only be a single compression method...

    An algorithm that would get the job done, really, REALLY slowly but much faster that yours, is maybe:
    ( Warning!!! Untested code! It probably doesn't work! )
    Code:
    // A function that returns how many times "sub"( up to "subLen" characters ) is found inside "str"
    int subCount(const char* str, const char* sub, int subLen); 
    
    for (i = 1; i <= strLen/2; i++) {
        for (j = 0; j <= strLen-i; j += i) {
            if (subCount(str, str+j, i) == 5) {
                return true;
            }
        }
    }
    return false;
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    18
    In each string, I have to get all possible string which we get from above procedure. Then we have to check condition that 5 continuation of some sub string must not appear. I have explaind every thing in my previous post. We have to do two work.
    First generate all possible special sub string in which we can skip at most one symbol.
    Second, in all such string, check occurrence of 5 consecutive x where x is any string.

  8. #8
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    What's the point of writing code that runs for days at a time? Shouldn't you be trying to optimize the code, and spend as little in terms of allocating resources, so that it doesn't use up so much memory?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-13-2014, 05:28 AM
  2. need help with a c program (12 days of christmas)
    By tshort82392 in forum C Programming
    Replies: 9
    Last Post: 09-23-2013, 10:06 AM
  3. Replies: 5
    Last Post: 12-04-2011, 03:32 AM
  4. Days of the week program
    By Chinnie15 in forum C Programming
    Replies: 4
    Last Post: 11-15-2011, 05:11 PM
  5. learn C In 21 days Appendix F ERRORS (bug in program)
    By Paul Tidwell in forum C Programming
    Replies: 6
    Last Post: 06-19-2011, 11:00 PM

Tags for this Thread