C++ homework

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 4
Reputation: sutty8303 is an unknown quantity at this point 
Solved Threads: 0
sutty8303 sutty8303 is offline Offline
Newbie Poster

C++ homework

 
0
  #1
Feb 16th, 2005
I need help with this problem. I have tried writting the program, i just can not get it to run properly.

Problem is:

Create a function named 'circle_area' that will find the area of a circle. This function should have the following prototype:

float circle_area(float radius)

circle_area should return PI*r^2. If passed 3.25 it will return 33.18. If passed 5.0 it should return 78.53, etc.

The main function will call circle_area and pass it a float that you stored in a variable. The value of Pi should be stored in a constant. The main program should display the variable and the area. For example, if radius is 1.0, the main function should display 'The area of a circle with radius 1.0 is: 3.14'. Use the techniques in section 3.7 of the textbook to format your output to 2 decimal places.

Attached is what i have.
Attached Files
File Type: cpp Lab3.cpp (477 Bytes, 18 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 30
Reputation: Intel is an unknown quantity at this point 
Solved Threads: 1
Intel Intel is offline Offline
Light Poster

Re: C++ homework

 
0
  #2
Feb 16th, 2005
Man you've done most of the work, just call your function in main, here is the code with some changes:
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. const float PI= 3.14;
  5.  
  6. float circle_area(float &radius);
  7.  
  8. float circle_area(float &radius)
  9. {
  10. using namespace std;
  11.  
  12. float area= PI*radius*radius;
  13. cout<< "The area of the circle with radius "<<radius<< " is: "<< area << setprecision(2) << endl;
  14. return (area);
  15. }
  16.  
  17. int main()
  18. {
  19. using namespace std;
  20.  
  21. float radius;
  22. cout<< "What is the radius of the circle:> ";
  23. cin >> radius;
  24. circle_area(radius);
  25. return 0;
  26. }
Intel
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: C++ homework

 
0
  #3
Feb 16th, 2005
As your code is so small, you should post it betwen code tags.
Some errors I noticed:
- at the end of include directives you do not have to put a semicolon(;)
- main allways returns int
- you have to declare the function circle_area before main
- you don't call the function circle_area inside the main, as you should
- you don't need all those headers (iostream and iomanip will suffice)
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 30
Reputation: Intel is an unknown quantity at this point 
Solved Threads: 1
Intel Intel is offline Offline
Light Poster

Re: C++ homework

 
0
  #4
Feb 16th, 2005
You need both
  1. #include <iostream>
  2. #include <iomanip>
I/O ops defined in STD namespace .... I/O manipulators are deined in iomanip
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,602
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C++ homework

 
0
  #5
Feb 16th, 2005
>I/O ops defined in STD namespace .... I/O manipulators are deined in iomanip
They're all in the std namespace. The streams and manipulators without arguments are declared in iostream, manipulators that take arguments are declared in iomanip. So to use setw and the like, you need to include iomanip, but to use left or hex and friends, you only need iostream.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,971
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: C++ homework

 
0
  #6
Feb 16th, 2005
You are almost there, but you have to fix your precision and perhaps make the console wait so you can see the result. Thoroughly check the code and learn ...
[php]
// program to calculate area of circle (tested with Dev-C++)

#include <iostream>
#include <iomanip> // setprecision(), setiosflags()

const float PI= 3.14;

float circle_area(float radius);

using namespace std; // std::cout, std::endl, std::cin

// calculate area of circle given the radius and display result
float circle_area(float radius)
{
using namespace std;

float area = PI*radius*radius;
cout << setiosflags(ios::fixed) << setprecision(2);
cout << "The area of the circle with radius " << radius << " is: " << area << endl;
return (area);
}

int main()
{
float radius;

cout << "What is the radius of the circle:> ";
cin >> radius;
// call the function
circle_area(radius);

cin.sync(); // purge enter
cin.get(); // console wait
return 0;
}
[/php]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,971
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: C++ homework

 
0
  #7
Feb 17th, 2005
Not an error, but a little goofy, the line
using namespace std;
should be removed from the function, don't know how it snuck in.

Just to make things perfect, your function doesn't need to return anything so it could be rewritten as ...
[php]
void circle_area(float radius)
{
float area = PI*radius*radius;
cout << setiosflags(ios::fixed) << setprecision(2);
cout << "The area of the circle with radius " << radius << " is: " << area << endl;
}
[/php]
Then you also have to change the optional but recommended prototype to: void circle_area(float radius);
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC