|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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. Code:
/*
* 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",°ree);
printf("%f degrees\n", °ree);
theta = find_radians(degree);
printf("%f radians\n", &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.\n", &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.\n", &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);
}
|
|
#2
|
||||
|
||||
|
Re: C Programming Homework Help Needed
As one step, you can remove the & sign from your printf statements.
For example, printf("Time of flight is %f seconds.\n", &time); becomes: printf("Time of flight is %f seconds.\n", time); |
|
#3
|
|||
|
|||
|
Re: C Programming Homework Help Needed
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. |
|
#4
|
|||
|
|||
|
Re: C Programming Homework Help Needed
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: Code:
float time; int * backdoor = &time; *backdoor = "integer value of the address of the variable time" print time; 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 |
|
#5
|
||||
|
||||
|
Re: C Programming Homework Help Needed
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. Last edited by Bongle : 19-05-2007 at 09:02. |
|
#6
|
||||
|
||||
|
Re: C Programming Homework Help Needed
Quote:
To answer another question, I'm using the Borland C 5.5 compiler. |
|
#7
|
||||
|
||||
|
Re: C Programming Homework Help Needed
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. Code:
/* Collects shape selection from user and checks for invalid inputs */
do
{
color_error = 0;
/* Collects desired color from user */
printf("\nSelect color:\n");
printf("BLACK (0), BLUE (1), GREEN (2)\n");
printf("CYAN (3), RED (4), MAGENTA (5)\n=>");
fflush(stdin);
scanf("%d", &color);
printf("Selected color: %d\n", 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\n");
break;
}
}
while(color_error);
Last edited by Pat McCarthy : 09-06-2007 at 18:29. |
|
#8
|
|||
|
|||
|
Re: C Programming Homework Help Needed
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 |
|
#9
|
||||
|
||||
|
Re: C Programming Homework Help Needed
Quote:
|
|
#10
|
||||
|
||||
|
Re: C Programming Homework Help Needed
I think I need to make this the "Help Pat pass EGR 261" thread.
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. Code:
int menu_error;
int menu_selection;
do
{
do
{
menu_error = 0;
/* Displays main menu */
printf("Please select from the following options:\n");
printf("(1) Draw a colored shape\n");
printf("(2) Clear graphics window\n");
printf("(3) Exit\n=>");
fflush(stdin);
scanf("%d\n", &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);
|
|
#11
|
|||
|
|||
|
Re: C Programming Homework Help Needed
Pat,
I'm not sure the problem is in the switch statement. Try removing the \n from the menu_selection scanf line ("%d" rather than "%d\n") Mike |
|
#12
|
||||
|
||||
|
Re: C Programming Homework Help Needed
That was it! Thanks again!
|
|
#13
|
||||
|
||||
|
Re: C Programming Homework Help Needed
Back again!
I can't figure out how to convert a float value to a char string. Suggestions? Thanks! |
|
#14
|
||||
|
||||
|
Re: C Programming Homework Help Needed
Quote:
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 |
|
#15
|
||||
|
||||
|
Re: C Programming Homework Help Needed
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.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| programming help needed | krunal | Programming | 3 | 29-01-2007 00:18 |
| Help on a Homework Assignment. | Pavan Dave | General Forum | 1 | 13-09-2006 14:20 |
| HELP-URGENT PROGRAMMING HELP NEEDED | Rohith Surampudi | Lego Mindstorm Discussion | 1 | 24-03-2006 23:05 |
| URgent Programming Help needed | rcubes85 | Programming | 4 | 15-02-2005 23:21 |