Thread: too many for loops??

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    too many for loops??

    hey.. im having a problem with my code...

    its quite a long script and this is a pretty big extract but the main part im having the issue with is the inner most nested for loop

    Code:
    for(i=0;i<N_gnc2;i++){
    
        for(l=0;l<N_gnc1;l++){                  
    
    absolute3[l]=abs(index_gnc2[index_gncthistag[i]]-index_TS2[index_TSthistag[l]]);
        
        }
    }
    if i set N_gnc1 to 10 or 100 the code runs but if i set it to the correct value which is 109727 then it appears to run indefinetly

    here is the larger part of the script that the code is a part of, important variables are

    end_i=462154
    n_TS = 109727;
    n_TN = 7;
    lengthtypeofTT = 5;

    i apologise if i made any mistakes posting hear... im still a beginner on these forums and with programming in general.

    Code:
    int N_gnc1;
    int N_gnc2;
    
    int *index_gnctoTS;    
    index_gnctoTS = (int *)malloc(end_i * sizeof(int));
    
    int *index_TSthistag;    
    index_TSthistag = (int *)malloc(end_i * sizeof(int));
    
    int *index_gncthistag;    
    index_gncthistag = (int *)malloc(end_i * sizeof(int));
    
    int *absolute3;    
    absolute3 = (int *)malloc(end_i * sizeof(int));
    
    int n_TN;
    
    n_TS = elnum_index_TS2;
    n_TN = elnum5;
    lengthtypeofTT;
    
    n=0;
    m=0;
    
    printf("n_TS = %d\n", n_TS);
    printf("n_TN = %d\n", n_TN);
    printf("end_i = %d\n", end_i);
    printf("lengthtypeofTT = %d\n", lengthtypeofTT);
    
    for(i=0;i<n_TS;i++){
        index_gnctoTS[i]=0;
    }
    
    for(j=0;j<n_TN;j++){
        for(k=0;k<lengthtypeofTT;k++){
            for(i=0;i<n_TS;i++){
                if( (time_tag2[index_TS2[i]]==typeofTT[k]) && (node_id2[index_TS2[i]]==node_type[j]) ){
                    index_TSthistag[m]=i;
                    m=m+1;
                }
            }
            N_gnc1=m;
    
            for(i=0;i<n_TS;i++){
                if( (time_tag2[index_gnc2[i]]==typeofTT[k]) && (node_id2[index_gnc2[i]]==node_type[j]) ){
                    index_gncthistag[n]=i;
                    n=n+1;
                }
            }
            N_gnc2=n;
            for(i=0;i<N_gnc2;i++){
                for(l=0;l<N_gnc1;l++){
                     absolute3[l]=abs(index_gnc2[index_gncthistag[i]]-index_TS2[index_TSthistag[l]]);
    
                }
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    oh and if its useful here is the Matlab code that im trying to convert to C

    Code:
    n_TS=length(find(index_TS));
        index_gnctoTS=zeros(n_TS,1);
        delta_index=0;
        n_TN=length(typeofnode);
        for j=1:n_TN
            for k=1:length(typeofTT)
                index_TSthistag=find((time_tag(index_TS)==typeofTT(k))&(node_id(index_TS)==typeofnode(j)));
                index_gncthistag=find((time_tag(index_gnc)==typeofTT(k))&(node_id(index_gnc)==typeofnode(j)));
                N_gnc=length(index_gncthistag);
                for i=1:N_gnc
                    [dummy,index_min]=min(abs(index_gnc(index_gncthistag(i))-index_TS(index_TSthistag)));    
                    index_gnctoTS(index_gncthistag(i))=index_TS(index_TSthistag(index_min));
                    delta_index=max(dummy,delta_index);
                end
            end
        end

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What makes you say it runs indefinitely? It should take on the order of 1000 times longer, and if it was originally taking a second then you can expect it to take several minutes.
    What compiler are you using? Not a 16-bit one by chance?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    thanks for the reply, and sorry for my late reply ... i put it aside for a few days and did other coding...

    im using Netbeans with Cygwin... i dont know if thats a 16bit compiler... haha sry if im a bit of a programming virgin

    when i say indenfinite i left it for about 8-11 minutes and it still hadnt finished... i assumed it wasnt going to stop.... anyway if its gonna take more than a minute or 2 to run this then thats far too long and ill have to find some other ideas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 more for loops
    By newbie101 in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2006, 11:46 AM
  2. how can i do this without using loops?
    By Guti14 in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2003, 01:48 AM
  3. For Loops
    By apoc632 in forum C Programming
    Replies: 10
    Last Post: 11-05-2003, 11:51 AM
  4. Loops
    By goosematt in forum C Programming
    Replies: 1
    Last Post: 10-20-2003, 11:59 AM