Computing.Net > Forums > Programming > math problem in C

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

math problem in C

Reply to Message Icon

Name: tImmaY
Date: January 26, 2005 at 14:08:32 Pacific
OS: Windows XP Home SP2
CPU/Ram: AMD Athlon XP 2400+ / 512
Comment:

hey, i have to create a program that calculates the volume of a cylinder *please dont stop reading, i did the work and i dont want someone to do it for me - i need help* so i wrote the program and and i'm using floating point variables as is required.. but for some reason when the program does the calculation, it comes up with some huge, oddball number thats nowhere NEAR accurate. when i give it 10 for the height and 5 for the radius, with the .exe from MSVC++ i get 603979776 and in Dev-C++ i get 0. so what did i do wrong?? thx!

Source Code :
/* Cylinder.c by Tim Hansen 01/26/04 */

/* This program will be used to calculate
* the volume of a cylinder. The radius
* and height variables will be input by
* the user via the keyboard. Output will
* be displayed on the screen.
*/

#include <stdio.h>

int main()
{
/* variable declaration */
float height, radius, volume;

/* prompt and input section */
printf("Cylinder Volume Calculation\n\n\n");

printf("Enter the height ");
scanf("%d", &height);

printf("\n\nEnter the radius ");
scanf("%d", &radius);

/* for space between the last input statement and the output */
printf("\n\n\n");

/* calculation and storage of the cylinders volume */
volume = 3.14159f * radius * radius * height;

/* output cylinder volume */
printf("The cylinder's volume is %d\n", volume);
return 0;
}



Sponsored Link
Ads by Google

Response Number 1
Name: dtech10
Date: January 26, 2005 at 14:23:53 Pacific
Reply:

Hi
The only thing that I can see is this

scanf("%d", &height);
scanf("%d", &radius);

I think it should be as you are input a float and not an integer.
scanf("%f", &height);
scanf("%f", &radius);



0

Response Number 2
Name: godjy
Date: January 26, 2005 at 14:51:05 Pacific
Reply:

I'm not so familiar with C but I think that there is a mistake in "scanf" and "printf".
As I see you want to enter floating point variable. That's why you have to type

scanf("%f", &height);

instead

scanf("%d", &height);

The same thing is for "printf".

And you have to enter numbers in the folowing format:
10.0

It's good to initialize the variables before
using them.
i.e.

float height=0.0, radius=0.0, volume=0.0;

I think that this will work.



0

Response Number 3
Name: tImmaY
Date: January 26, 2005 at 15:55:20 Pacific
Reply:

hahahaha ^_^ *scratches head* ..oops..

it appears that in my haste, i made and overlooked a mistake. thanks guys.. lol


0

Response Number 4
Name: Dr. Nick
Date: January 26, 2005 at 23:01:42 Pacific
Reply:

I for one just want to say "thanks" for doing your own work before coming here. It's a nice change from the norm.

Good job :)


0

Response Number 5
Name: Mechanix2Go
Date: January 26, 2005 at 23:16:51 Pacific
Reply:

I'm learning here.

What is the revised code?

TIA

M2


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

FoxPro DOS Command help AddNew Problem in VB



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: math problem in C

problem in c www.computing.net/answers/programming/problem-in-c/12469.html

CScokets problem in C++ www.computing.net/answers/programming/cscokets-problem-in-c/13674.html

Problem in 'C'. www.computing.net/answers/programming/problem-in-c/2089.html