C Programming Homework Help Needed

Hi all,
I’m taking an course, “Structured Programming in C” and am having some issues with my homework for the week.
My assignment is to write a program that calculates various aspects of projectile motion, but I am running into a problem with an output(?) in my program.

When I compile the program I receive no errors or warnings, but when I try to enter values into the first input, it turns the value into ‘+NAN’. I don’t know what ‘+NAN’ is or how to fix it.
I’ve attached a screen capture of what happens when the program is run and values are entered.

HELP!

Please excuse the messiness of the code, as it is not completed or functional.


/*
 * Author:	Patrick McCarthy
 * Filename:	P34.c
 * Description:	Homework 2, Chapter 3:4, CS261
 * 	This program computes the duration of a projectile's flight
 * 	and its height above the ground when it reaches the target.
*/

#include <stdio.h>
#include <math.h>

/* Function prototype */
float find_radians(float theta);

/* Function prototype */
float calculate_time(float distance, float velocity, float theta);

/* Function prototype */
float calculate_height(float velocity, float theta, float time);


int main(void)
{
	float degree;
	float theta;
	float distance;
	float velocity;
	float time;
	float height;

	/* Gets the angle of elevation from the user */
	printf("Enter angle (in degrees) of elevation =>");
	scanf("%f",&degree);

printf("%f degrees
", &degree);

	theta = find_radians(degree);
	
	printf("%f radians
", &theta);

	/* Gets the distance to the target from the user */
	printf("Enter distance (in feet) to target =>");
	scanf("%f" ,&distance);

	/* Gets the projectile's velocity from the user */
	printf("Enter projectile's velocity (ft/sec) =>");
	scanf("%f", &velocity);

	calculate_time(distance,velocity,theta);

	/* Displays time of projectile flight to user */
	printf("Time of flight is %f seconds.
", &time);
	
	calculate_height(velocity,theta,time);

	/* Displays height of projectile at impact to user */
	printf("Height of projectile impact is %f feet from the ground.
", &height);

	return 0;
}


/* Converts degrees to radians */
float find_radians(float degree)
{
	float theta;	/* local variable */
	float Pi = 3.14159;
	
	theta = degree * (180/Pi);

	return (theta);
}


float calculate_time(float distance,float velocity,float theta)
{
	float time;	// local variable
	
	time = distance / ( velocity * cos(theta) );

	return (time);
}


float calculate_height(float velocity, float theta, float time)
{
	float height;		// local variable
	float g = 32.17;	// Declares acceleration due to gravity as -32 ft/s/s

	height = velocity * sin(theta) * time - ( (g * time*time) / 2 );

	return (height);
}

Thanks!

NaN.jpg


NaN.jpg

As one step, you can remove the & sign from your printf statements.

For example,

printf("Time of flight is %f seconds.
", &time);

becomes:

printf("Time of flight is %f seconds.
", time);

Joel is correct. Remove the & from your printf functions. The & means “address of”. Printf doesn’t want the address of the location where the number is kept, printf wants the number itself.

FYI, the & is correct in the scanf function. Scanf requires the address of the variable.

Btw NAN means “Not A Number”.

Flash.

I’m a bit confused as to exactly how this code compiled, and why it isn’t happy. I know exactly enough C to confuse myself.

I understand why the code is wrong. I don’t fully understand why this is the error that is given.

Since printf takes a variable number of untyped objects, I’ll believe that it can’t catch the type mismatch of address and float. This must be the case, since it did compile. Since C has no run-time environment to speak of, we must assume that the computer will do exactly what it is told: Take the address and interpret it as a float.

The code then loosely translates to:

float time;
int * backdoor = &time;
*backdoor = "integer value of the address of the variable time"
print time;

At this point we essentially have “take a random bit string and interpret it as a float”. Why is this float NaN? I know that the float space does have NaN gaps, but I thought the space was relatively dense. In otherwords, I thought it was so dense that the probability of this error should be rather small. Does it happen repeatably? Is there something about the addressing space that places variables in the realm of NaN?

I may be reading too far into this, but I’d really appreciate the assistance of someone with a deeper understanding of C.

Thanks
Eric

I just ran it in Code::Blocks (open-source IDE, it is quite good), and no matter what number I entered, I got 0.0000 degrees, 0.00000 radians.

Check the scanf documentation, there is probably something small you’re doing wrong.
Edit: I read the scanf documentation, and there doesn’t appear to be anything wrong. Good luck.

Thanks Joel, that did it.

To answer another question, I’m using the Borland C 5.5 compiler.

Program.jpg


Program.jpg

Back again…

I’m only concerned with a small portion of my program at the moment.
This part collects an integer value for use to set the color of an object later in the program, then checks to make sure it is within a certain range of numbers. If the number isn’t within that set of numbers, it is supposed to ask the user to enter again, then collects a value again.

Currently, whether the value entered is within the parameters that I want, this chunk of program gets stuck in the error checking loop. Oddly, when you enter a two digit integer, it breaks the loop.

	/* Collects shape selection from user and checks for invalid inputs */
	do
	{
		color_error = 0;
		
		/* Collects desired color from user */
		printf("
Select color:
");
		printf("BLACK (0), BLUE (1), GREEN (2)
");
		printf("CYAN (3), RED (4), MAGENTA (5)
=>");
		fflush(stdin);
		scanf("%d", &color);
		printf("Selected color: %d
", color);
		fflush(stdin);

		/* Valitdate entry */
		switch (color)
		{
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
				break;
				
			default:
				color_error = 1;
				printf("Please select one of the available colors
");
				break;
		}
	}
	while(color_error);

I’ve attached an image of what happens when the code is executed and the entire program’s code. It needs to be compiled with a special .lib file that my prof wrote. (something in the forums is keeping me from attaching it)

Color_input.jpg
shape.c (2.12 KB)


Color_input.jpg
shape.c (2.12 KB)

Pat,

It looks like you are not being consistant with the data type for “color”. You are inputting it as an integer (%d) so I’ll assume you’ve defined it as such, however you are comparing it in the switch statement as a character. The value ‘0’ will be converted to it’s ASCII value (48) for the compare. If ‘color’ is an integer, then I’d try removing the single quotes from around the numbers in the case statements and see if that works.

Mike

Thanks, that did it. I didn’t realize that single quotes changed things.

I think I need to make this the “Help Pat pass EGR 261” thread. :stuck_out_tongue:

My problem this time is that a switch statement again.
After the menu displays, the user can enter a selection, but when they hit enter, it just displays a blank line and doesn’t leave the switch statement.

Thanks for the continuing help guys.:slight_smile:


	int menu_error;
	int menu_selection;

	do
	{
		do
		{
			menu_error = 0;
		
			/* Displays main menu */
			printf("Please select from the following options:
");
			printf("(1) Draw a colored shape
");
			printf("(2) Clear graphics window
");
			printf("(3) Exit
=>");
			fflush(stdin);
			scanf("%d
", &menu_selection);

			switch(menu_selection)
			{
				case 1:
					draw_shape();
					break;
					
				case 2:
					clear_graphics();
					break;

				case 3:
					break;

				default:
					menu_error = 1;
					printf("Please enter a selection 1, 2, or 3");
			}
		}
		while(menu_error);
	}
	while(menu_selection != 3);

Pat,

I’m not sure the problem is in the switch statement. Try removing the
from the menu_selection scanf line ("%d" rather than "%d
")

Mike

That was it! Thanks again!

Back again!
I can’t figure out how to convert a float value to a char string.
Suggestions? Thanks!

Like take value 9.023 and turn it into ‘9’ ‘.’ ‘0’ ‘2’ ‘3’ in a char string?

Idk how you would get each number individually but to change the number into its ASCII code you would do

+ ‘0’ to get the ascii code of the number

Well, I have a float variable that I need to be able to display using WinBGI using the text function that only accepts char strings. So, I really don’t know exactly.

The function you are looking for is called “sprintf”.

Use it just like you would use printf to print a float. The trick is that it throws it into a string, rather than at the console.


char * buffer = new char[10];
float f = 12.4;
sprintf(buffer,"%f",f);

I think… I don’t have a C compiler in front of me.

Is there a header file you need to include?
I’m getting a ‘no prototype for function’ error for the sprintf function.

I believe you need to include stdio.h.

That made that error go away but I still don’t really understand how to use the function.
The first line:

char * buffer = new char[10];

is confusing me.

The use of the “new” operator is C++ speak, not C speak.

char buffer[10]; /* bigger than 10 is a good idea */

If a static or automatic array (depending on the location
of the declaration) is good enough, or

char *buffer;

buffer = malloc(10); /* again, bigger than 10 is a good idea */

to allocate the storage using the heap allocator in C.

Eugene