Thread: loop question-new at C

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    loop question-new at C

    I'm in my second programming class at school, thing is the last time I took programming was a year ago and it was a mix of basics for C, matlab and java, so I'm having trouble remembering anything.

    Well anyways though I'm doing practice problems for our test and I'm having trouble with one... we're supposed to be using the bisection method to find roots of a polynomial. The algorithm for that isnt what im having trouble with, I have pseudocode for it in the book but in any case I'm not to that point yet. we're supposed to print a table of values for input and output angles and using a given polynomial print the "error" which is actually the function value (deviation from zero) Anyways we're supposed to be having the user enter a 10 degree long range of angles between 0 and 360 and so I'm doing a while statement to check if the range is valid... however I said if: (beta_u-beta_l==10) (where beta_u is the end of the range, and beta_l is the beginning) then I want it to call a function... however its not doing that and doesnt seem to even be executing my if statements... I'm guessing that its some issue with a break or something but I've tried adding one and its not helping. I'll post the whole code with a comment above the loop I'm having trouble with

    insert
    Code:
    #include "stdio.h"
    #include "math.h"
    
    
    double Freudenstein(double alpha, double beta);
    double bisection(double alpha, double beta_u, double beta_l);
    
    
    double alpha=45;
    
    
    int a=6;
    int b=18;
    int c=15;
    double d=15.5;
    
    
    void main () {
      double beta=0;
    	enum{rows=20,cols=3};	
    	double beta_l=-3; 
    	double beta_u= 370;
     
      
    
    
    
        
    	printf("The input angle alpha is 45 degrees, and we will be computing the error for various output angles: beta=[0:18:360]\n");
    
        printf("\nalpha(deg)\t, beta(deg)\t,error\n");
    	printf("\n");
    
    	while (beta<360)  {    //the minimum output angle under examination
    	                           // is 0, the max is 360, with increments of 18
    		
    	    beta=beta+18;
    		
    	printf("%lf\t,%lf\t, %lf\n",alpha,beta,Freudenstein(alpha,beta));
    	}
    	
    
       printf("\nPlease input a 10 degree interval of output angles (Beta) to be checked, between Beta= 0 degrees and Beta= 360 degrees, in the follwing form: Beta lower Beta upper:\n");
        scanf("%lf,%lf",&beta_l,&beta_u);
    	
    //HERES THE TROUBLE LOOP
    
    	while ((beta_u <0 || beta_l <0) || (beta_u>360 || beta_l>360) && beta_u-beta_l!=102 !=10 ) {
    	  
    	
    
    		if ((beta_u-beta_l != 10)) {
    		
    		printf("\n Please re-enter a valid 10 degree long interval for Beta between 0 and 360 degrees:\n");
        scanf("%lf,%lf",&beta_l,&beta_u);
    	
    		}
    		
    		 else if (beta_u-beta_l==10) {
    			printf("hi there! this is a cool number: %lf \n",bisection(alpha,beta_u,beta_l));
    			break;
            
    			}
    
    	}
        
    }
    		
    
    
    	
    
    double Freudenstein(double alpha,double beta) {
    
    	double error;
    
    	error= (pow(a,2)-pow(b,2)+pow(c,2)+pow(d,2)-(2*a*c*sin(alpha)*sin(beta))-(2*a*c*cos(alpha)*cos(beta))-(2*a*d*cos(alpha))+ (2*c*d*cos(beta)));
    
    	
        return(error);
    
    }
    
    //this function is where my root finding bisection method will be eventually
    
    double bisection(double alpha, double beta_u,double beta_l) {
    
     double m= 3.5+7.5;
    	
    	
    	
        return(m);
    
    }
    I know theres probably a better way to do this... I am pretty new at programming in general. another thing is is that because of my boolean operators if the (beta_u-beta_l !=10) it reprints
    the printf statement twice... I honestly cant think of any way around that, so if any of you can any ideas would be appreciated.

    thanks

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    oh and I just realized that the enum that I have at the beginning of the main block
    extraneous from an old version of the code and also that I have beta_u and beta_l assigned values there from when I had the print statement above my trouble loop inside the loop so ignore that. Sorry

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    please if anyone knows tonight I would really appreciate it I have a feeling problems like this may be covered on the test... I've been reading online and I think I have an infinite loop going with that while loop, I just am not sure what conditions I need to end it, or why the break isnt ending it.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure if this is a typo or what, but:
    Code:
    beta_u-beta_l!=102 !=10
    probably doesn't do what you want it to do - either you didn't really mean to have the second != part, or you meant something other than what you write, since this becomes
    Code:
    (beta_u-beta_l!=102) !=10
    This will ALWAYS be true: The first parenthesis will always give a value of 0 or 1 (false or true in C), which is not EVER equal to 10!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. while loop question
    By rayrayj52 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 05:13 PM