Thread: While Loop not iterating

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    39

    While Loop not iterating

    Firstly, would like to thank those of you taking time out of your day to help me with my codes, I'm new to C and not very good at it, but I am slowly learning.

    My task here is to find the roots of a given polynomial on the interval [1,2] using the Binomial Bisection method.

    They gave us the hint that it would require an if, else if and else inside a while loop. The loop should iterate the print statement "iteration count = %d root = %d" until a point where the root is found.

    Not sure why my code isn't even initiating, I probably have an error somewhere or is something wrong with the flag condition?

    Thank you!









    Code:
    
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
    	double a = 1;
    	double b = 2;
    	double fCenter = 0;
    	double fB = 0;
    	double fA = 0;
    	double center = 0;
    	int iterCount = 1;
    	
    	int flag = 1;
    	
    	center = ((a+b)/2.0); // Center = 3/2 first time.
    	printf("Your center value is %lf\n", center);
    	
    	fCenter = pow(center,3.0) - (3.0 * pow(center,2)) - (5.0 * center) + 8.0; // Results in -2.875
    	printf("Your f(center) value is %lf\n", fCenter);
    	
    	fA = pow(a,3.0) - (3.0 * pow(a,2.0)) - (5.0 * a) + 8.0; // Results in 1 for iter2
    	printf("Your current f(a) is %lf\n", fA);
    	
    	fB = pow(b,3.0) - (3.0 * pow(b,2)) - (5.0 * b) + 8.0; // Results IN -6 for Iter1
    	printf("Your current f(b) is %lf\n", fB);
    	
    	
    	
    	while(flag=1)
    	{
    
    
    	center = (a+b)/2;
    	
    	
    		if(abs(fCenter) < 0.0001)
    		{
    			printf("Iteration Count = %d Root = %lf", iterCount, center);
    		}
    			else if((fA*fCenter)<0)
    			{
    				b = center;
    				fCenter = pow(center,3.0) - (3.0 * pow(center,2)) - (5.0 * center) + 8.0;
    				
    			}
    			else((fB*fCenter)<0);
    			{
    				a = center;
    				fCenter = pow(center,3.0) - (3.0 * pow(center,2)) - (5.0 * center) + 8.0;
    				
    			}
    			
    			iterCount = iterCount + 1;
    		
    		
    		return 0;
    	}
    	  
    	
    	
    }

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by csmith03 View Post
    Firstly, would like to thank those of you taking time out of your day to help me with my codes, I'm new to C and not very good at it, but I am slowly learning.

    My task here is to find the roots of a given polynomial on the interval [1,2] using the Binomial Bisection method.

    They gave us the hint that it would require an if, else if and else inside a while loop. The loop should iterate the print statement "iteration count = %d root = %d" until a point where the root is found.

    Not sure why my code isn't even initiating, I probably have an error somewhere or is something wrong with the flag condition?

    Thank you!
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    int main(void)
    {
        double a = 1;
        double b = 2;
        double fCenter = 0;
        double fB = 0;
        double fA = 0;
        double center = 0;
        int iterCount = 1;
       int flag = 1;
       center = ((a+b)/2.0); // Center = 3/2 first time.
        printf("Your center value is %lf\n", center);
        
        fCenter = pow(center,3.0) - (3.0 * pow(center,2)) - (5.0 * center) + 8.0; // Results in -2.875
        printf("Your f(center) value is %lf\n", fCenter);
        
        fA = pow(a,3.0) - (3.0 * pow(a,2.0)) - (5.0 * a) + 8.0; // Results in 1 for iter2
        printf("Your current f(a) is %lf\n", fA);
        
        fB = pow(b,3.0) - (3.0 * pow(b,2)) - (5.0 * b) + 8.0; // Results IN -6 for Iter1
        printf("Your current f(b) is %lf\n", fB);
        
        
           // you're assigning a value not checking it
           while(flag=1)
            {
                center = (a+b)/2;
              
                if(abs(fCenter) < 0.0001)
                {
                    printf("Iteration Count = %d Root = %lf", iterCount, center);
                }
                else if((fA*fCenter)<0)
                {
                    b = center;
                    fCenter = pow(center,3.0) - (3.0 * pow(center,2)) - (5.0 * center) + 8.0;
                }
                else((fB*fCenter)<0);
                {
                    a = center;
                    fCenter = pow(center,3.0) - (3.0 * pow(center,2)) - (5.0 * center) + 8.0;
                }
               // iterCount = iterCount + 1;
                iterCount++;
    // added so you see what is going on in your loop
      printf("in loop\n");     
          if (iterCount == 3)
             break;
          
            }
      return 0;
    }
    check your while loop condition
    big difference between these two
    Code:
    =
    and
    ==
    plus your brackets where off by one, or where you put your return 0; I fixed it in your code in this post.

    even with your logic on how to run your while loop you do not have a complete thought.
    How are you now going to stop the loop from running after you correct the equal signs?
    iterCount++; <-- is not going to work against (flag==1) , they have nothing to do with each other so your program is going to keep looping.

    key phrase to use for controlling your loop
    until a point where the root is found.

    loop until root is found
    Last edited by userxbw; 10-16-2017 at 08:53 AM.

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Quote Originally Posted by userxbw View Post
    check your while loop condition
    big difference between these two
    Code:
    =
    and
    ==
    plus your brackets where off by one, or where you put your return 0; I fixed it in your code in this post.

    even with your logic on how to run your while loop you do not have a complete thought.
    How are you now going to stop the loop from running after you correct the equal signs?
    iterCount++; <-- is not going to work against (flag==1) , they have nothing to do with each other so your program is going to hang.

    key phrase to use for controlling your loop
    until a point where the root is found.

    loop until root is found

    Would I be able to get rid of the flag statement and put
    Code:
     (while center != (root))
    , that way it stops iterating when it finds the root? The iterCount is more for the print statement
    Code:
     printf("Iteration Count = %d, Root = %d", iterCount, center);

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by csmith03 View Post
    Would I be able to get rid of the flag statement and put
    Code:
     (while center != (root))
    , that way it stops iterating when it finds the root? The iterCount is more for the print statement
    Code:
     printf("Iteration Count = %d, Root = %d", iterCount, center);
    go back and put that added bit I just put in your loop in your code then run that to see what is going on inside of your loop.

    as far as that piece of code you just gave me.
    Code:
    (while center != (root))
    how is it going to know what 'root' is? do you have that assigned or declared even as a variable?
    where the while ( center != root) would look at it like this
    Code:
    int center = 100;
    int root = 30;
    where ( value inside of center != value inside of root)
    // it sees it like this;
    while ( 100 != 30 )
    if you do not increment the root var it will never become equal to center to kick you out of your loop. my math sucks, but what I see in how this needs to be done using this as a guild;
    The loop should iterate the print statement "iteration count = %d root = %d" until a point where the root is found.
    Code:
        while ( flag == 0 )
        {
    
             
            do the math until you find the absolute root value;
            root =  whatever math needs to be done;
            printf(" the count of how many times it does the math on the numbers,
                        and the results of each completed equation each time it tries to figure it out\n", count, root) ;
            count++:
       
            if( root )
                flag = 1;
             // that stops the loop
         }
    that is the basic logic behind it. but you have to know when that root value is actually found or not. then use that for the root value, tricky isn't it?

    Mod:
    Code:
    #include <stdio.h>
    
    int main (void)
    {
    
    int b = 0, count = 0, flag = 0;
    
    while ( flag == 0 )
    {
    
        b = b + 1;
        printf("count = %d root = %d\n", count, b);
        count++;
        
            if ( b == 4)
             flag = 1;
        
     }
     return 0;
    }
    results
    Code:
    userx@void:~/bin
    $ gcc mathloop.c
    userx@void:~/bin
    $ ./a.out
    count = 0 root = 1
    count = 1 root = 2
    count = 2 root = 3
    count = 3 root = 4
    userx@void:~/bin
    but I bet you a small duck that @whiteflags can pinnacle on that logic.
    Last edited by userxbw; 10-16-2017 at 09:37 AM.

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    @User

    My professor told me to specifically use an if, if else and else inside of a while. Thanks for bringing up the flag, I added an if statement into the loop to break the condition.

    I still come across the problem that it's not iterating at all. It should print "Iteration 1 Root = 1.5" , "Iteration 2 Root = 1.125", etc... A= 1, B=2.... The equations and formulas are all correct, I don't know what I'm missing...

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
    	double a = 1;
    	double b = 2;
    	double fCenter = 0;
    	double fB = 0;
    	double fA = 0;
    	double center = 0;
    	int iterCount = 1;
    	
    	int flag = 0;
    	
    	center = ((a+b)/2.0); // Center = 3/2 first time.
    
    
    	
    	fCenter = pow(center,3.0) - (3.0 * pow(center,2)) - (5.0 * center) + 8.0; // Results in -2.875
    	
    	
    	fA = pow(a,3.0) - (3.0 * pow(a,2.0)) - (5.0 * a) + 8.0; // Results in 1 for iter2
    	
    	
    	fB = pow(b,3.0) - (3.0 * pow(b,2)) - (5.0 * b) + 8.0; // Results IN -6 for Iter1
    	
    	
    	
    	
    	while(flag == 0)
    	{
    
    
    		center = (a+b)/2; // Sets Center equal to  A + B / 2.
    	
    		fCenter = pow(center,3.0) - (3.0 * pow(center,2)) - (5.0 * center) + 8.0; // Uses calculated center value for f(center)
    	
    	
    		if(abs(fCenter) < 0.0001)
    		{
    			printf("Iteration Count = %d Root = %lf\n", iterCount, center);
    			iterCount++;
    			if(center == 1.125)
    			{
    				flag = 1;
    			}
    		}
    		
    			else if((fA*fCenter) < 0)
    			{
    				b = center;
    			}
    				else ((fB*fCenter) < 0);
    				{
    					a = center;
    				}
    		
    				
    				
    	}

  6. #6
    Registered User
    Join Date
    May 2015
    Posts
    90
    Line 18 and 22 which are the previous definitions of center and fcenter are redefined in lines 37 and 39.
    You are also missing an if in line 56.

    Do notice that:
    Code:
    if(expression_1)
    {
        // something
    
    } 
    else if(expression_2)
    {
        // something else, if and only if expression_1 was false.
    
    } 
    else
    {
        // something else, if and only if expression_1 and expression_2 were false.
    }
    I say this because in your case, once you enter the
    Code:
    if(abs(fCenter) < 0.0001)
    block, center and fCenter never change (because there are no changes to them nor to a and b that define them and the else blocks are never entered)

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by csmith03 View Post
    @User

    My professor told me to specifically use an if, if else and else inside of a while. Thanks for bringing up the flag, I added an if statement into the loop to break the condition.

    I still come across the problem that it's not iterating at all. It should print "Iteration 1 Root = 1.5" , "Iteration 2 Root = 1.125", etc... A= 1, B=2.... The equations and formulas are all correct, I don't know what I'm missing...
    so you're just tossing in an
    Code:
    if ( .. )
    {
    }
    else if (..)
    {
    }
    else
    just to meet the requirements without the logic?

    besides the mistakes @Fiskker pointed out.
    where do you do a printf? what needs to be met before that takes place?
    put a
    Code:
    printf(" hello I am really in the loop\n");
    in a place within your loop that will cause it to always print out. so you can see what it is doing.

    ok try this, it is without the fixes to the code Fiskker pointed out.
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        double a = 1;
        double b = 2;
        double fCenter = 0;
        double fB = 0;
        double fA = 0;
        double center = 0;
        int iterCount = 1;
        int flag = 0;
        
        center = ((a+b)/2.0); // Center = 3/2 first time.
        fCenter = pow(center,3.0) - (3.0 * pow(center,2)) - (5.0 * center) + 8.0; // Results in -2.875
        fA = pow(a,3.0) - (3.0 * pow(a,2.0)) - (5.0 * a) + 8.0; // Results in 1 for iter2
        fB = pow(b,3.0) - (3.0 * pow(b,2)) - (5.0 * b) + 8.0; // Results IN -6 for Iter1
        
        while(flag == 0)
        {
            center = (a+b)/2; // Sets Center equal to  A + B / 2.
            fCenter = pow(center,3.0) - (3.0 * pow(center,2)) - (5.0 * center) + 8.0; // Uses calculated center value for f(center)
        
        printf("%lf\n", (abs(fCenter) )  );
            if(abs(fCenter) < 0.0001)
            {
                printf("Iteration Count = %d Root = %lf\n", iterCount, center);
                iterCount++;
                printf("%lf\n", (center == 1.125) );
                if(center == 1.125)
                {
                    flag = 1;
                }
            }
                
                else if((fA*fCenter) < 0)
                {
                    b = center;
                }
                    else ((fB*fCenter) < 0);
                    {
                        a = center;
                    }
            
                printf(" hello I am really in the loop\n");    
        }
        return 0;
    }
    run that and see what you are working with.
    Last edited by userxbw; 10-16-2017 at 01:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Iterating List
    By Rahil Khan in forum C Programming
    Replies: 4
    Last Post: 10-10-2015, 04:25 AM
  2. Help with iterating the menu !
    By thebenman in forum C Programming
    Replies: 4
    Last Post: 10-21-2014, 08:06 PM
  3. iterating through hex numbers in c
    By f.g in forum C Programming
    Replies: 18
    Last Post: 08-24-2011, 06:35 PM
  4. Iterating through a map that contains a vector
    By AngryStyro in forum C++ Programming
    Replies: 2
    Last Post: 06-08-2008, 05:01 AM
  5. Iterating through an array
    By MikeyIckey in forum C Programming
    Replies: 15
    Last Post: 11-10-2007, 10:26 PM

Tags for this Thread