Thread: computing the sum within a loop

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    16

    computing the sum within a loop

    Hi, a section of my code is as follows:

    insert
    Code:
    for (i = 0; i < N; i++){
    		
    
    		
    		result[0] = distance(myPoints[i], myPoints[(i+1)%N]);
    		
    		printf("first distance:%f\n", result[0]);
    		
    		for (j = 0; j < N; j++){
    		
    		
    		
    			if (i == j)
    			 	continue;
    				
    				
    				
    				dis[i] = distance(myPoints[i], myPoints[j]);
                printf("%d %d\n", i, j);
    			printf("%f\n", dis[i]);
    				
         		if (result[0] > dis[i])
    	 
    	       result[0] = dis[i];
    		 
    			
          }    
    	if(result[0] != 0) {entropyxy[i] += (log(2*result[0]));
    	}
    		   
    		   printf("shortest: %f\n", result[0]);
    	    
    		   
    		   
    		   
    	
    	}
    my formula for entropy is : entropyxy = SUM(from i=1 to N) log(2*result(i))

    However, my entropyxy doesn't seem to increment as it stands in my code. It only computes log(2*result[0]) but doesn't add this onto the previous value of the entropyxy i.e. entropyxy[i-1]. any help on how to solve this?

    N.B result[0] changes for each value of i. (it is the shortest distance to a point)


    thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zesty View Post
    my entropyxy doesn't seem to increment as it stands in my code. It only computes log(2*result[0]) but doesn't add this onto the previous value of the entropyxy i.e. entropyxy[i-1]. any help on how to solve this?
    Your code does not say add to entropyxy[i-1], it says add to entropyxy[i].
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  3. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  4. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM