Wednesday, March 7, 2012

Can someone look at my code and tell whats wrong because it runs but it doesnt run right.?

include %26lt;stdio.h%26gt;

#define N 100



double f( double x ){ return x*x+(2*x)+2; }



void main (void)

{



double a = -1.0; /* Lower Limit of integration. */

double b = 1.0; /* Upper limit of integration. */

double h = (b-a)/N; /* Width of one of the N rectangles summed. */



double x; /* Current function argument; a %26lt;=x %26lt; b. */

double sum; /* Total area of rectangles. */

int k; /* Loop counter. */



printf( " Integrate x*x+(2*x)+2 from %g to %g. \n", a, b );



sum = -1.0;

k = 0;



while(k %26lt; N){ /* Add up N rectangles. */

x = a + k * h; /* Lower left corner of kth rectangle. */

sum += h * f( x ); /* Area of rectangle at x. */

++k;

}



printf( " The area is %g\n", sum );







//END MAIN





float T( float P, float R );



float P;

float R; /* .06 */

float X;



printf("Purchase Price");

scanf("%f",%26amp;P);



X = T(P,R);

printf( " The After-tax Price: %.2f\n",X);



//END MAIN



double A, n, p, i;



puts( " find the annual interest rate " );

printf( " Enter the values of A, n, p, and i " );

scanf( "%lg%lg%lg%lg", %26amp;A, %26amp;n, %26amp;p, %26amp;i );



A = p*(1+i)*n;

printf( "\n the answer is %g A = P*(1+i)*n %g\n", A, n, p, i );



//END MAIN



double radius(double r);



double r,d;

float s;

float c;

float v;



puts( "\n enter the value of d with input greater than or equal to 0.0 \n" );

printf( "enter in d:" );

scanf( "%lg", %26amp;d );



r = d/2;



puts( "\n enter the value of r with input greater than or equal to 0.0 \n" );

printf( "enter in r:" );

scanf( "%f", %26amp;r );



s = 4*3.14*r*r;



puts( "\n enter the value of d with input greater than or equal to 0.0 \n" );

printf( "enter in d:" );

scanf( "%f", %26amp;d );



c = 3.14*d;



puts( "\n enter the value of r with input greater than or equal to 0.0 \n" );

printf( "enter in r:" );

scanf( "%f", %26amp;r );



v = 4*3.14/3*r*r*r;



printf( "\n print out four results %g r = d/2 %f s = 4*3.14*r*r %f c = 3.14*d %f v = 4*3.14/3*r*r*r \n", r, s, c, v );



}

















Can someone look at my code and tell whats wrong because it runs but it doesnt run right.?
I see a few things. Try and describe what is going wrong, though, so we can identify it. The first part (integration) looks good.



You use R without initializing it:

float R; /* .06 */

should be

float R = 0.06f;





Then there is a problem with this line:



printf( "\n the answer is %g A = P*(1+i)*n %g\n", A, n, p, i );



Since you pass 4 parameters but you will only use the first 2. This will print out A and n, which might not be what you want.



Then here, you mix float and double in a way you shouldn't:

scanf( "%f", %26amp;r );



It will write a float value. Use %g to get a double since r is declared as double.



Also, you are reusing r so much that I think you are losing values that you wanted to keep.Can someone look at my code and tell whats wrong because it runs but it doesnt run right.?
WOA very confusing.

First, You just keep saying end main and you aren't.

It needs to be cleaned up.



main

{





Do stuff



]//end main



Do Stuff

{



}



Do more stuff

{



}

No comments:

Post a Comment