Thread: Need help with program

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Need help with program

    Hello I wrote this program but it's not working properly.

    The value of xv1 and xp1 is given I want to use those values to work out the values of xp2...xp100 and xv2...xv100. The program is meant to use the value of xp and xv to figure out which formula to use. After doing that it is meant to workout "a" using the values derived from the formula. After determining the value of "a" it uses another formula to workout the next xp and xv. It is meant to repeat this process 100 times. I need help badly with this please somebody help me with this program.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define CALCULATIONSXp 100
    #define CALCULATIONSXv 100
    
    int main()
    {
    	//float x1=-0.12; // xp
    	//float x2=-0.3; // xv
    	float xp[CALCULATIONSXp];
    	float xv[CALCULATIONSXv];
    	float xp[0]=-0.12;
    	float xv[0]=-0.3;
    	float ypNS;
    	float ypNM;
    	float ypNS2;
    	float ypZE;
    	float ypZE2;
    	float ypPS;
    	float ypPS2;
    	float ypPM;
    	float yvNS;
    	float yvNM;
    	float yvNS2;
    	float yvZE;
    	float yvZE2;
    	float yvPS;
    	float yvPS2;
    	float yvPM;
    //	float y1;
    //	float state1;
    //	float stateX2;
    	float a;
    	float a1=0.0455;
    	float a2=0.9091;
    	float b1=0.6667;
    	float b2=0.0455;
    	float T=0.02;
    	float g=9.8;
    //	float x1=-0.12;
    //	float x2=-0.3;
    //	float F=5.87804878;
    //	float x2k;
    //	float x1k;
    	int i;
    	int j;
    	int k;
    
    	for(i=0; i<=100;i++)
    {
    	// Information on graph x1
    
    	if((xp >=-0.2) && (xp <= -0.1))
    	{
    		ypNS=10*xp+2;
    
    		ypNM=-10*xp-1;
    	}
    
    	if((xp>=-0.1) && (xp <= 0))
    	{
    		ypNS2=-10*xp;
    
    		ypZE=10*xp+1;
    	}
    
    	
    	if((xp>=0) && (xp <= 0.1))
    	{
    		ypZE2=-10*xp+1;
    
    		ypPS=10*xp;
    	}
    
    	if((xp>=0.1) && (xp <= 0.2))
    	{
    		ypPS2=-10*xp+2;
    
    		ypPM=10*xp-1;
    	}
    
    	// Information on graph x2
    
    	if((xv >=-1) && (xv <= -0.5))
    	{
    		yvNS=2*xv+2;
    
    		yvNM=-2*xv-1;
    	}
    
    	if((xv>=-0.5) && (xv <= 0))
    	{
    		yvNS2=-10*xv;
    
    		yvZE=2*xv+1;
    	}
    
    	
    	if((xv >=0) && (xv <= 0.5))
    	{
    		yvZE2=-10*xv+1;
    
    		yvPS=10*xv;
    	}
    
    	if((xv>=0.5) && (xv <= 1))
    	{
    		yvPS2=-2*xv+2;
    
    		yvPM=2*xv-1;
    	}
    
    	if(((xp >=-0.2) && (xp <= -0.1)) && ((xv >=-1) && (xv <= -0.5)))
    	{
    		a=((ypNS*yvNS*10)+(ypNS*yvNM*10)+(ypNM*yvNM*10)+(ypNM*yvNS*10))/((ypNS*yvNS)+(ypNS*yvNM)+(ypNM*yvNM)+(ypNM*yvNS));
    	}
    
    	else if(((xp >=-0.2) && (xp <= -0.1)) && ((xv>=-0.5) && (xv <= 0)))
    	{
    		a=((ypNS*yvNS2*10)+(ypNS*yvZE*5)+(ypNM*yvZE*10)+(ypNM*yvNS2*10))/((ypNS*yvNS2)+(ypNS*yvZE)+(ypNM*yvZE)+(ypNM*yvNS2));
    	}
    
    	else if(((xp >=-0.2) && (xp <= -0.1)) && ((xv >=0) && (xv <= 0.5)))
    	{
    		a=((ypNS*yvPS*0)+(ypNS*yvZE2*5)+(ypNM*yvZE2*10)+(ypNM*yvPS*5))/((ypNS*yvPS)+(ypNS*yvZE2)+(ypNM*yvZE2)+(ypNM*yvPS));
    	}
    
    	else if(((xp >=-0.2) && (xp <= -0.1)) && ((xv>=0.5) && (xv <= 1)))
    
    	{
    		a=((ypNS*yvPS2*0)+(ypNS*yvPM*-5)+(ypNM*yvPM*0)+(ypNM*yvPS2*5))/((ypNS*yvPS2)+(ypNS*yvPM)+(ypNM*yvPM)+(ypNM*yvPS2));
    	}
    
    	if(((xp>=-0.1) && (xp <= 0)) && ((xv >=-1) && (xv <= -0.5)))
    	{
    		a=((ypNS2*yvNM*10)+(ypNS2*yvNS*10)+(ypZE*yvNM*10)+(ypZE*yvNS*5))/((ypNS2*yvNM)+(ypNS2*yvNS)+(ypZE*yvNM)+(ypZE*yvNM));
    	}
    
    	else if(((xp>=-0.1) && (xp <= 0)) &&  ((xv>=-0.5) && (xv <= 0)))
    	{
    		a=((ypNS2*yvNS2*10)+(ypNS2*yvZE*5)+(ypZE*yvNS2*5)+(ypZE*yvZE*0))/((ypNS2*yvNS2)+(ypNS2*yvZE)+(ypZE*yvNS2)+(ypZE*yvZE));
    	}
    
    	else if(((xp>=-0.1) && (xp <= 0)) &&  ((xv >=0) && (xv <= 0.5)))
    	{
    		a=((ypNS2*yvZE2*5)+(ypNS2*yvPS*0)+(ypZE*yvZE2*0)+(ypZE*yvPS*-5))/((ypNS2*yvZE2)+(ypNS2*yvPS)+(ypZE*yvZE2)+(ypZE*yvPS));
    	}
    
    	else if(((xp >=-0.2) && (xp <= -0.1)) && ((xv>=0.5) && (xv <= 1)))
    	{
    		a=((ypNS2*yvPS2*0)+(ypNS2*yvPM*-5)+(ypZE*yvPS2*-5)+(ypZE*yvPM*-10))/((ypNS2*yvPS2)+(ypNS2*yvPM)+(ypZE*yvPS2)+(ypZE*yvPM));
    	}
    
    	if(((xp>=0) && (xp <= 0.1)) && ((xv >=-1) && (xv <= -0.5)))
    	{
    		a=((ypZE2*yvNM*10)+(ypZE2*yvNS*5)+(ypPS*yvNM*5)+(ypPS*yvNS*0))/((ypZE2*yvNM)+(ypZE2*yvNS)+(ypPS*yvNM)+(ypPS*yvNS));
    	}
    
    	else if(((xp>=0) && (xp <= 0.1)) && ((xv>=-0.5) && (xv <= 0)))
    	{
    		a=((ypZE2*yvNS2*5)+(ypZE2*yvZE*0)+(ypPS*yvNS2*0)+(ypPS*yvZE*-5))/((ypZE2*yvNS2)+(ypZE2*yvZE)+(ypPS*yvNS2)+(ypPS*yvZE));
    	}
    
    	else if(((xp>=0) && (xp <= 0.1))  &&  ((xv >=0) && (xv <= 0.5)))
    	{
    		a=((ypZE2*yvZE2*0)+(ypZE2*yvPS*-5)+(ypPS*yvZE2*-5)+(ypPS*yvPS*-10))/((ypZE2*yvZE2)+(ypZE2*yvPS)+(ypPS*yvZE2)+(ypPS*yvPS));
    	}
    
    	else if(((xp>=0) && (xp <= 0.1)) && ((xv>=0.5) && (xv <= 1)))
    	{
    		a=((ypZE2*yvPS2*-5)+(ypZE2*yvPM*-10)+(ypPS*yvPM*-10)+(ypPS*yvPS2*-10))/((ypZE2*yvPS2)+(ypZE2*yvPM)+(ypPS*yvPM)+(ypPS*yvPS2));
    	}
    
    	if(((xp>=0.1) && (xp <= 0.2)) &&  ((xv >=-1) && (xv <= -0.5)))
    	{
    		a=((ypPS2*yvNM*5)+(ypPS2*yvNS*0)+(ypPM*yvNM*0)+(ypPM*yvNS*-5))/((ypPS2*yvNM)+(ypPS2*yvNS)+(ypPM*yvNM)+(ypPM*yvNS));
    	}
    
    	else if(((xp>=0.1) && (xp <= 0.2)) && ((xv>=-0.5) && (xv <= 0)))
    	{
    		a=((ypPS2*yvNS2*0)+(ypPS2*yvZE*-5)+(ypPM*yvNS2*-5)+(ypPM*yvZE*-10))/((ypPS2*yvNS2)+(ypPS2*yvZE)+(ypPM*yvNS2)+(ypPM*yvZE));
    	}
    
    	else if(((xp>=0.1) && (xp <= 0.2)) &&  ((xv >=0) && (xv <= 0.5)))
    	{
    		a=((ypPS2*yvZE2*-5)+(ypPS2*yvPS*-10)+(ypPM*yvZE2-10)+(ypPM*yvPS-10))/((ypPS2*yvZE2)+(ypPS2*yvPS)+(ypPM*yvZE2)+(ypPM*yvPS));
    	}
    
    	else if(((xp>=0.1) && (xp <= 0.2)) && ((xv>=0.5) && (xv <= 1)))
    	{
    		a=((ypPS2*yvPS2*-10)+(ypPS2*yvPM*-10)+(ypPM*yvPS2*-10)+(ypPM*yvPM*-10))/((ypPS2*yvPS2)+(ypPS2*yvPM)+(ypPM*yvPS2)+(ypPM*yvPM));
    	}
    
    	xv[j]= xv[j] + T*(((g*sin(xp[k]) + (cos(xp[k]))*(-a1*pow(xv[j],2)*sin(xp[k])) + a2*a))/(b1 - b2*pow(cos(xp[j]),2)));
    	xp[k]= xp[k] + T*xv[j];
    
    	printf("The value of a is %f while xp=%f and xv=%f\n",a,xp,xv);
    }
    
    return 0;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Wow...... Congratulations on this debugging nightmare.....

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Don't you have any warnings? integer to pointer comparison?

    1. xp and xv are arrays... You are using them mostly as integers - fix it.
    2. J and k - are they initialized anyware? I'm too lazy to search
    3. MAKE functions - at least two
    For example this part:
    Code:
    if((xp >=-0.2) && (xp <= -0.1))
    	{
    		ypNS=10*xp+2;
    
    		ypNM=-10*xp-1;
    	}
    
    	if((xp>=-0.1) && (xp <= 0))
    	{
    		ypNS2=-10*xp;
    
    		ypZE=10*xp+1;
    	}
    
    	
    	if((xp>=0) && (xp <= 0.1))
    	{
    		ypZE2=-10*xp+1;
    
    		ypPS=10*xp;
    	}
    
    	if((xp>=0.1) && (xp <= 0.2))
    	{
    		ypPS2=-10*xp+2;
    
    		ypPM=10*xp-1;
    	}
    
    	// Information on graph x2
    
    	if((xv >=-1) && (xv <= -0.5))
    	{
    		yvNS=2*xv+2;
    
    		yvNM=-2*xv-1;
    	}
    
    	if((xv>=-0.5) && (xv <= 0))
    	{
    		yvNS2=-10*xv;
    
    		yvZE=2*xv+1;
    	}
    
    	
    	if((xv >=0) && (xv <= 0.5))
    	{
    		yvZE2=-10*xv+1;
    
    		yvPS=10*xv;
    	}
    
    	if((xv>=0.5) && (xv <= 1))
    	{
    		yvPS2=-2*xv+2;
    
    		yvPM=2*xv-1;
    	}
    should be definitely a function with 2 in and 2 out parameters...
    returning error if the input parameters are out of range.

    Better yet two functions - one depending on xp, second on xv
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    HAssan, tell us just *what* the program is doing wrong.

    When you have something wrong with you, you *tell* the doctor what it is. Same thing here. Why not tell us? I certainly don't know what the expected and correct output should be for your program.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Not using array's anymore but still not working

    I thought about it and by using arrays it added extra complexity to the program that is not necessary so i took it out. But the program is not working. The program is meant to calculate xv,xp and "a" after it calculated those values it is meant to use those values to calculate the next set of xv, xp and "a" but the program it not doing that. when xv=-0.12 and xp=-0.3 "a=8.4" i can'nt figure out where the problem is. The new code for the program is

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    
    	float xp;
    	float xv;
    	xp=-0.12;
    	xv=-0.3;
    	float ypNS;
    	float ypNM;
    	float ypNS2;
    	float ypZE;
    	float ypZE2;
    	float ypPS;
    	float ypPS2;
    	float ypPM;
    	float yvNS;
    	float yvNM;
    	float yvNS2;
    	float yvZE;
    	float yvZE2;
    	float yvPS;
    	float yvPS2;
    	float yvPM;
    	float a;
    	float a1=0.0455;
    	float a2=0.9091;
    	float b1=0.6667;
    	float b2=0.0455;
    	float T=0.02;
    	float g=9.8;
    	int i;
    	//float xv1;
    	//float xp1;
    
    
    	for(i=0; i<=100;i++)
    {
    	// Information on graph x1
    
    	if((xp >=-0.2) && (xp <= -0.1))
    	{
    		ypNS=10*xp+2;
    
    		ypNM=-10*xp-1;
    	}
    
    	if((xp>=-0.1) && (xp <= 0))
    	{
    		ypNS2=-10*xp;
    
    		ypZE=10*xp+1;
    	}
    
    	
    	if((xp>=0) && (xp <= 0.1))
    	{
    		ypZE2=-10*xp+1;
    
    		ypPS=10*xp;
    	}
    
    	if((xp>=0.1) && (xp <= 0.2))
    	{
    		ypPS2=-10*xp+2;
    
    		ypPM=10*xp-1;
    	}
    
    	// Information on graph x2
    
    	if((xv >=-1) && (xv <= -0.5))
    	{
    		yvNS=2*xv+2;
    
    		yvNM=-2*xv-1;
    	}
    
    	if((xv>=-0.5) && (xv <= 0))
    	{
    		yvNS2=-10*xv;
    
    		yvZE=2*xv+1;
    	}
    
    	
    	if((xv >=0) && (xv <= 0.5))
    	{
    		yvZE2=-10*xv+1;
    
    		yvPS=10*xv;
    	}
    
    	if((xv>=0.5) && (xv <= 1))
    	{
    		yvPS2=-2*xv+2;
    
    		yvPM=2*xv-1;
    	}
    
    	if(((xp >=-0.2) && (xp <= -0.1)) && ((xv >=-1) && (xv <= -0.5)))
    	{
    		a=((ypNS*yvNS*10)+(ypNS*yvNM*10)+(ypNM*yvNM*10)+(ypNM*yvNS*10))/((ypNS*yvNS)+(ypNS*yvNM)+(ypNM*yvNM)+(ypNM*yvNS));
    	}
    
    	else if(((xp >=-0.2) && (xp <= -0.1)) && ((xv>=-0.5) && (xv <= 0)))
    	{
    		a=((ypNS*yvNS2*10)+(ypNS*yvZE*5)+(ypNM*yvZE*10)+(ypNM*yvNS2*10))/((ypNS*yvNS2)+(ypNS*yvZE)+(ypNM*yvZE)+(ypNM*yvNS2));
    	}
    
    	else if(((xp >=-0.2) && (xp <= -0.1)) && ((xv >=0) && (xv <= 0.5)))
    	{
    		a=((ypNS*yvPS*0)+(ypNS*yvZE2*5)+(ypNM*yvZE2*10)+(ypNM*yvPS*5))/((ypNS*yvPS)+(ypNS*yvZE2)+(ypNM*yvZE2)+(ypNM*yvPS));
    	}
    
    	else if(((xp >=-0.2) && (xp <= -0.1)) && ((xv>=0.5) && (xv <= 1)))
    
    	{
    		a=((ypNS*yvPS2*0)+(ypNS*yvPM*-5)+(ypNM*yvPM*0)+(ypNM*yvPS2*5))/((ypNS*yvPS2)+(ypNS*yvPM)+(ypNM*yvPM)+(ypNM*yvPS2));
    	}
    
    	if(((xp>=-0.1) && (xp <= 0)) && ((xv >=-1) && (xv <= -0.5)))
    	{
    		a=((ypNS2*yvNM*10)+(ypNS2*yvNS*10)+(ypZE*yvNM*10)+(ypZE*yvNS*5))/((ypNS2*yvNM)+(ypNS2*yvNS)+(ypZE*yvNM)+(ypZE*yvNM));
    	}
    
    	else if(((xp>=-0.1) && (xp <= 0)) &&  ((xv>=-0.5) && (xv <= 0)))
    	{
    		a=((ypNS2*yvNS2*10)+(ypNS2*yvZE*5)+(ypZE*yvNS2*5)+(ypZE*yvZE*0))/((ypNS2*yvNS2)+(ypNS2*yvZE)+(ypZE*yvNS2)+(ypZE*yvZE));
    	}
    
    	else if(((xp>=-0.1) && (xp <= 0)) &&  ((xv >=0) && (xv <= 0.5)))
    	{
    		a=((ypNS2*yvZE2*5)+(ypNS2*yvPS*0)+(ypZE*yvZE2*0)+(ypZE*yvPS*-5))/((ypNS2*yvZE2)+(ypNS2*yvPS)+(ypZE*yvZE2)+(ypZE*yvPS));
    	}
    
    	else if(((xp >=-0.2) && (xp <= -0.1)) && ((xv>=0.5) && (xv <= 1)))
    	{
    		a=((ypNS2*yvPS2*0)+(ypNS2*yvPM*-5)+(ypZE*yvPS2*-5)+(ypZE*yvPM*-10))/((ypNS2*yvPS2)+(ypNS2*yvPM)+(ypZE*yvPS2)+(ypZE*yvPM));
    	}
    
    	if(((xp>=0) && (xp <= 0.1)) && ((xv >=-1) && (xv <= -0.5)))
    	{
    		a=((ypZE2*yvNM*10)+(ypZE2*yvNS*5)+(ypPS*yvNM*5)+(ypPS*yvNS*0))/((ypZE2*yvNM)+(ypZE2*yvNS)+(ypPS*yvNM)+(ypPS*yvNS));
    	}
    
    	else if(((xp>=0) && (xp <= 0.1)) && ((xv>=-0.5) && (xv <= 0)))
    	{
    		a=((ypZE2*yvNS2*5)+(ypZE2*yvZE*0)+(ypPS*yvNS2*0)+(ypPS*yvZE*-5))/((ypZE2*yvNS2)+(ypZE2*yvZE)+(ypPS*yvNS2)+(ypPS*yvZE));
    	}
    
    	else if(((xp>=0) && (xp <= 0.1))  &&  ((xv >=0) && (xv <= 0.5)))
    	{
    		a=((ypZE2*yvZE2*0)+(ypZE2*yvPS*-5)+(ypPS*yvZE2*-5)+(ypPS*yvPS*-10))/((ypZE2*yvZE2)+(ypZE2*yvPS)+(ypPS*yvZE2)+(ypPS*yvPS));
    	}
    
    	else if(((xp>=0) && (xp <= 0.1)) && ((xv>=0.5) && (xv <= 1)))
    	{
    		a=((ypZE2*yvPS2*-5)+(ypZE2*yvPM*-10)+(ypPS*yvPM*-10)+(ypPS*yvPS2*-10))/((ypZE2*yvPS2)+(ypZE2*yvPM)+(ypPS*yvPM)+(ypPS*yvPS2));
    	}
    
    	if(((xp>=0.1) && (xp <= 0.2)) &&  ((xv >=-1) && (xv <= -0.5)))
    	{
    		a=((ypPS2*yvNM*5)+(ypPS2*yvNS*0)+(ypPM*yvNM*0)+(ypPM*yvNS*-5))/((ypPS2*yvNM)+(ypPS2*yvNS)+(ypPM*yvNM)+(ypPM*yvNS));
    	}
    
    	else if(((xp>=0.1) && (xp <= 0.2)) && ((xv>=-0.5) && (xv <= 0)))
    	{
    		a=((ypPS2*yvNS2*0)+(ypPS2*yvZE*-5)+(ypPM*yvNS2*-5)+(ypPM*yvZE*-10))/((ypPS2*yvNS2)+(ypPS2*yvZE)+(ypPM*yvNS2)+(ypPM*yvZE));
    	}
    
    	else if(((xp>=0.1) && (xp <= 0.2)) &&  ((xv >=0) && (xv <= 0.5)))
    	{
    		a=((ypPS2*yvZE2*-5)+(ypPS2*yvPS*-10)+(ypPM*yvZE2-10)+(ypPM*yvPS-10))/((ypPS2*yvZE2)+(ypPS2*yvPS)+(ypPM*yvZE2)+(ypPM*yvPS));
    	}
    
    	else if(((xp>=0.1) && (xp <= 0.2)) && ((xv>=0.5) && (xv <= 1)))
    	{
    		a=((ypPS2*yvPS2*-10)+(ypPS2*yvPM*-10)+(ypPM*yvPS2*-10)+(ypPM*yvPM*-10))/((ypPS2*yvPS2)+(ypPS2*yvPM)+(ypPM*yvPS2)+(ypPM*yvPM));
    	}
    
    	xv= xv + T*(((g*sin(xp) + (cos(xp))*(-a1*pow(xv,2)*sin(xp)) + a2*a))/(b1 - b2*pow(cos(xp),2)));
    	xp= xp + T*xv;
    
    	
    	printf("The value of a is %f while xp=%f and xv=%f\n",a,xp,xv);
    }
    
    return 0;
    }

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    You are trying to loop 100 times with the same XP and XV values, and the comparing float is not the right way.

    Goolge on topic Comparing float values. You will get a ser of web site which explains why.

    ssharish2005

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Surely this could be refined. Also why not declare all the float variables at once?

    Code:
    float	xp = -0.12,
    	xv = -0.3,
    	ypNS,
    	ypNM,
    	ypNS2,
    	ypZE, 
    	ypZE2,
    	ypPS,
    	ypPS2,
    	ypPM, 
    	yvNS,
    	yvNM,
    	yvNS2,
    	yvZE,
    	yvZE2,
    	yvPS,
    	yvPS2,
    	yvPM,
    	a,
    	a1 = 0.0455,
    	a2 = 0.9091,
    	b1 = 0.6667,
    	b2 = 0.0455,
    	T = 0.02, 
    	g = 9.8;
    And no, I don't think adding arrays would make it *more* complex.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    How to update xp and xv

    That's exactly what the problem is the xp and xv is not updating. for the program to work xv and xp must update. initially xv and xp is given but I want to update it throughout the program and I can'nt seem to do that. About the comparision I think it is right and "ssharish2005" if you think I made a mistake please point it out where I'am open to criticism I consider it as free feedback. Please point out to me how to update the values of xv and xp i think this is where i'am making my mistake.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    	float xp;
    	float xv;
    	xp=-0.12;
    	xv=-0.3;
    	float ypNS;
    	float ypNM;
        /* ... */
    Declaring variables in the middle of a block -- that is, after some executable code like x=1 -- is C99, and you should probably avoid it if possible. In this case it's very simple to avoid. Just try something like
    Code:
    	float xp = -0.12;
    	float xv = -0.3;
    	float ypNS;
    	float ypNM;
        /* ... */
    And here:
    Code:
    	if(((xp>=0) && (xp <= 0.1)) && ((xv >=-1) && (xv <= -0.5)))
    	{
    		a=((ypZE2*yvNM*10)+(ypZE2*yvNS*5)+(ypPS*yvNM*5)+(ypPS*yvNS*0))/((ypZE2*yvNM)+(ypZE2*yvNS)+(ypPS*yvNM)+(ypPS*yvNS));
    	}
    
    	else if(((xp>=0) && (xp <= 0.1)) && ((xv>=-0.5) && (xv <= 0)))
    	{
    		a=((ypZE2*yvNS2*5)+(ypZE2*yvZE*0)+(ypPS*yvNS2*0)+(ypPS*yvZE*-5))/((ypZE2*yvNS2)+(ypZE2*yvZE)+(ypPS*yvNS2)+(ypPS*yvZE));
    	}
    
    	else if(((xp>=0) && (xp <= 0.1))  &&  ((xv >=0) && (xv <= 0.5)))
    	{
    		a=((ypZE2*yvZE2*0)+(ypZE2*yvPS*-5)+(ypPS*yvZE2*-5)+(ypPS*yvPS*-10))/((ypZE2*yvZE2)+(ypZE2*yvPS)+(ypPS*yvZE2)+(ypPS*yvPS));
    	}
    
    	else if(((xp>=0) && (xp <= 0.1)) && ((xv>=0.5) && (xv <= 1)))
    	{
    		a=((ypZE2*yvPS2*-5)+(ypZE2*yvPM*-10)+(ypPS*yvPM*-10)+(ypPS*yvPS2*-10))/((ypZE2*yvPS2)+(ypZE2*yvPM)+(ypPS*yvPM)+(ypPS*yvPS2));
    	}
    
    	if(((xp>=0.1) && (xp <= 0.2)) &&  ((xv >=-1) && (xv <= -0.5)))
    	{
    		a=((ypPS2*yvNM*5)+(ypPS2*yvNS*0)+(ypPM*yvNM*0)+(ypPM*yvNS*-5))/((ypPS2*yvNM)+(ypPS2*yvNS)+(ypPM*yvNM)+(ypPM*yvNS));
    	}
    
    	else if(((xp>=0.1) && (xp <= 0.2)) && ((xv>=-0.5) && (xv <= 0)))
    	{
    		a=((ypPS2*yvNS2*0)+(ypPS2*yvZE*-5)+(ypPM*yvNS2*-5)+(ypPM*yvZE*-10))/((ypPS2*yvNS2)+(ypPS2*yvZE)+(ypPM*yvNS2)+(ypPM*yvZE));
    	}
    
    	else if(((xp>=0.1) && (xp <= 0.2)) &&  ((xv >=0) && (xv <= 0.5)))
    	{
    		a=((ypPS2*yvZE2*-5)+(ypPS2*yvPS*-10)+(ypPM*yvZE2-10)+(ypPM*yvPS-10))/((ypPS2*yvZE2)+(ypPS2*yvPS)+(ypPM*yvZE2)+(ypPM*yvPS));
    	}
    
    	else if(((xp>=0.1) && (xp <= 0.2)) && ((xv>=0.5) && (xv <= 1)))
    	{
    		a=((ypPS2*yvPS2*-10)+(ypPS2*yvPM*-10)+(ypPM*yvPS2*-10)+(ypPM*yvPM*-10))/((ypPS2*yvPS2)+(ypPS2*yvPM)+(ypPM*yvPS2)+(ypPM*yvPM));
    	}
    First of all, you have a lot of duplicate code there. I'm sure you could wrap a
    Code:
    if((xp>=0) && (xp <= 0.1))
    around a lot of that and make it much easier to read.

    Also, if xp==0.1, then the first if will execute, so why not change this >= to >?
    Code:
    ((xp>=0.1) && (xp <= 0.2))
    And ssharish2005 meant that many numbers cannot be represented exactly in floating point variable types (especially with floats), 0.1 included. Or 0.1 will be represented more accurately in a double than in a float, which is just as problematic, because your variable is a float, but "0.1" is a double. Use "0.1f" to make it a float. This program prints "No":
    Code:
    #include <stdio.h>
    
    int main() {
        float d = 0.1;
        
        if(d == 0.1) puts("Yes");
        else puts("No");
        
        getchar();
        return 0;
    }
    Strange, but true.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM