Lost in a loop, sum not adding up correctly... Please help.

This is a discussion on Lost in a loop, sum not adding up correctly... Please help. within the C Programming forums, part of the General Programming Boards category; Hello, The code below is suppose to add up numbers from part of an array and print out the sum... ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Lost in a loop, sum not adding up correctly... Please help.

    Hello,
    The code below is suppose to add up numbers from part of an array and print out the sum... I got it to print it out correctly earlier, but somehow it does not work at all anymore, its printing out 4... it looks like its not even getting the updated sum from the inner most for loop....


    Can anyone help me fix this?

    Code:
    int Permute(int numGend, int couplemin[10][10]){
    	
    	int z=0, sum=0, n=0, i=0, r=0, v=0, q=0;
    	int maxMin[20];
    
    
    	for(n=0; n < numGend; n++){
    		for(v=0; v < 2; v++){ 
    			sum=couplemin[0][n];
    			q=n;
    			if(v==0){
    				for(i=1; i < numGend; i++){
    					if(i==q){
    						q--;
    						i=q;	
    					}
    					z++;
    					sum+=couplemin[z][i];
    				}
    			}	
    			else if(v==1){
    				for(i=numGend-1; i >= numGend; i--){
    					z++;
    					sum+=couplemin[z][i];
    				}
    			}
    			maxMin[v]=sum;
    			z=0;
    		}
    	}
    	printf("%d\n", sum);
    }

  2. #2
    Registered User
    Join Date
    Sep 2010
    Location
    Halesowen, England
    Posts
    30
    This loop:
    Code:
    for(i=numGend-1; i >= numGend; i--){
    will never run...
    read it as:
    Code:
    i = numGend-1;
    while (i >= numGend) {
      i--;
    }
    The while case is never TRUE, so the loop never executes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sum of the series - loop?
    By jacek in forum C Programming
    Replies: 20
    Last Post: 10-26-2009, 12:46 AM
  2. Adding several nodes to a linked list using a loop
    By budala in forum C Programming
    Replies: 3
    Last Post: 09-11-2009, 07:05 AM
  3. ?? with trying to sum numbers
    By Babs21 in forum C Programming
    Replies: 1
    Last Post: 09-16-2007, 04:58 PM
  4. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 05:13 AM
  5. Replies: 1
    Last Post: 11-19-2001, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21