943,983 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 13200
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 16th, 2005
0

C++ homework

Expand Post »
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, 331 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sutty8303 is offline Offline
4 posts
since Feb 2005
Feb 16th, 2005
0

Re: C++ homework

Man you've done most of the work, just call your function in main, here is the code with some changes:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Light Poster
Intel is offline Offline
30 posts
since Feb 2005
Feb 16th, 2005
0

Re: C++ homework

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)
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Feb 16th, 2005
0

Re: C++ homework

You need both
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
I/O ops defined in STD namespace .... I/O manipulators are deined in iomanip
Reputation Points: 10
Solved Threads: 1
Light Poster
Intel is offline Offline
30 posts
since Feb 2005
Feb 16th, 2005
0

Re: C++ homework

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 16th, 2005
0

Re: C++ homework

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]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 17th, 2005
0

Re: C++ homework

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);
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jan 25th, 2010
-2
Re: C++ homework
#include <iostream>
using namespace std;

int main()
{
Reputation Points: 6
Solved Threads: 0
Newbie Poster
PZY_DAVE is offline Offline
1 posts
since Jan 2010
Jan 25th, 2010
0
Re: C++ homework
Click to Expand / Collapse  Quote originally posted by Intel ...
Man you've done most of the work, just call your function in main, here is the code with some changes:
C++ Syntax (Toggle Plain Text)
  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
That will work but a function should really stick to only doin one thing... either calculation or cout, since this is not a complex task.
C++ Syntax (Toggle Plain Text)
  1. float circle_area(float radius)
  2. {
  3. return PI * radius * radius;
  4. }
Then do the cout's in main
Reputation Points: 34
Solved Threads: 28
Junior Poster
hag++ is offline Offline
189 posts
since Jan 2010
Jan 26th, 2010
0
Re: C++ homework
Click to Expand / Collapse  Quote originally posted by hag++ ...
That will work but a function should really stick to only doin one thing... either calculation or cout, since this is not a complex task.
C++ Syntax (Toggle Plain Text)
  1. float circle_area(float radius)
  2. {
  3. return PI * radius * radius;
  4. }
Then do the cout's in main
This thread needs no more help. The thread ended in 2005. I'm sure the OP is no longer waiting...
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: Assignment
Next Thread in C++ Forum Timeline: How to enable "just-in-time (JIT) debugging"





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC