#include <stdio.h>
#include <math.h>
//acceleration due to gravity in m/s^2
#define GRAVITATIONAL_ACCELERATION 9.8
#define PI 3.14159265359
int main( void )
{
char ch;
float distance;
float speed;
float height;
float angle;
printf("Compute: distance or angle? ");
scanf("%c",&ch); // scan first non-space character
if( ch != '\n' ) { // if necessary, skip to end of line
while( getchar() != '\n' )
;
}
if( ch != 'a' && ch != 'A' ) {
printf("Enter: speed height angle: ");
scanf("%f %f %f", &speed, &height, &angle);
}
else {
printf("Enter: speed height distance: ");
// INSERT CODE TO COMPUTE ANGLE
printf("no solution\n");
}
if( sqrt(speed*speed*sin(angle*PI/180)*sin(angle*PI/180)-2*GRAVITATIONAL_ACCELERATION*height) <= 0 ) {
printf( "no solution.\n" );
}
else {
//Computes the distance in terms of speed, height and angle.
distance = (speed*cos(angle*PI/180)*speed*sin(angle*PI/180)+speed*cos(angle*PI/180)*sqrt(speed*speed*sin(angle*PI/180)*sin(angle*PI/180)-2*GRAVITATIONAL_ACCELERATION*height)) / GRAVITATIONAL_ACCELERATION;
printf("distance is %.2f metres\n", distance);
}
return 0;
}
cannot print error message
Page 1 of 12 Replies - 235 Views - Last Post: 09 August 2012 - 03:22 AM
#1
cannot print error message
Posted 09 August 2012 - 02:19 AM
Hello guys, I've written a code that computes the range of the projectory using the speed, initial height and angle. I wanted to print an error message when the square root part of the equation is negative. However when 30, 40, 50 is entered instead of 'no solution' the program prints 'distance is -nan metres'. The program is executable when valid figures are entered.
Replies To: cannot print error message
#2
Re: cannot print error message
Posted 09 August 2012 - 02:35 AM
Simple solution is to check your calculation before you try to use sqrt(). After all trying to use sqrt() on a negative value is the problem.
Also you may want to consider breaking your large calculation up into smaller bits to make troubleshooting a little easier. Use several variables to hold the results of intermediate calculations. You seem to be calculating several items several times in your calculations and this should actually speed up your calculations. For example you seem to use cos(angle*PI/180) and sin(angle*PI/180) multiple times in your calculation so these seem like a good candidate to compute once to a separate variables and do these calculations only once.
Jim
double test = speed*speed*sin(angle*PI/180)*sin(angle*PI/180)-2*GRAVITATIONAL_ACCELERATION*height;
if(test <= 0)
{
// Print your error message.
}
else
{
// Do your calculations.
Also you may want to consider breaking your large calculation up into smaller bits to make troubleshooting a little easier. Use several variables to hold the results of intermediate calculations. You seem to be calculating several items several times in your calculations and this should actually speed up your calculations. For example you seem to use cos(angle*PI/180) and sin(angle*PI/180) multiple times in your calculation so these seem like a good candidate to compute once to a separate variables and do these calculations only once.
Jim
This post has been edited by jimblumberg: 09 August 2012 - 02:36 AM
#3
Re: cannot print error message
Posted 09 August 2012 - 03:22 AM
Thank you! Worked it out
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|