Write a complete program that have two functions named CylinderVolume() and CylinderArea(). Both functions should receive diameter and height of cylinder as parameters. Each function should return the volume and area respectively to main program. Given formula:
Cylinder volume, v=πr2h
Cylinder area, a=2π2r+2πrh
where π is pi, r is radius and h is height.

Dani AI

Generated

A small correction to the formula in the prompt: the total surface area of a right circular cylinder is a = 2pirr + 2pirh (the r is squared on the first term). Since the functions are specified to accept diameter and height, compute r = diameter / 2.0 and then apply the formulas. Also ensure you handle invalid input (negative dimensions) and avoid integer division by using 2.0 or 0.5.

#include <stdexcept>
// If you have C++20, prefer: #include <numbers> and use std::numbers::pi
constexpr double pi = 3.14159265358979323846;

double CylinderVolume(double diameter, double height) {
    if (diameter < 0 || height < 0) throw std::invalid_argument("Negative dimension");
    const double r = diameter * 0.5;
    return pi * r * r * height;           // v = pi*r^2*h
}

double CylinderArea(double diameter, double height) {
    if (diameter < 0 || height < 0) throw std::invalid_argument("Negative dimension");
    const double r = diameter * 0.5;
    return 2.0 * pi * r * (r + height);   // a = 2*pi*r^2 + 2*pi*r*h
}

Tips: keep units consistent (e.g., cm everywhere); prefer a precise pi (or std::numbers::pi in C++20) over ad-hoc constants; and test with simple cases (e.g., diameter 0 gives area 0 and volume 0). For the geometry, see the cylinder formulas at Wikipedia. For C++20 constants, see std::numbers::pi on cppreference.

Recommended Answers

All 12 Replies

please help me...

i wrote the CylinderVolume() for you so you can have an idea to write the other function

 #include <iostream>

    using namespace std;

    const double pi= 3.14159;

    double CylinderVolume(double diameter, double height){
        double radius = diameter/2.0;
        return pi * radius * radius * height;
    }

    int main(){

    cin.ignore();
    cin.get();
    return 0;
    }

: What have you tried? What problems have you encountered? There is plenty of information there to make an honest attempt at the assignment - you have the formulae; all you need to do is construct functions that implement them.

: It is obvious this is some sort of assignment and nobody benefits from giving out answers. The OP learns nothing about the assignment or its goals; the OP is less prepared to tackle later material; and the community is burdened by having to deal with the propagated request for free answers.

Well i know i was wrong but i just wanted to give him a hint.

No point of helping someone who has never tried...

commented: Nice! +15

write a c++ program to library management system that must can minimumly update,delete,add,search information.

commented: ? -2

, Go make your own thread and ask him...

, Have you tried to write any codes to date?

thanks for the help. sorry to all I just want to help my friend only.

, well... here have you and your friend read these:
https://answers.yahoo.com/question/index?qid=20111201192111AAyHYQr
http://www.cplusplus.com/forum/beginner/85642/
http://cplusplus-in.blogspot.com/2012/06/how-to-determine-volume-of-cylinder.html
http://www.cplusplus.com/forum/beginner/52102/

http://projectxplod.blogspot.com/2012/06/c-program-to-compute-surface-area-of.html

I want you to know this and for future reference... a lot of these simple homework questions have been answered in the past and have been placed on certain sites. You should try google before asking... you should only ask if you have a hard time with a certain problem but only if you have tried and made an effort.

ok thanks for your advice. and thanks for the reference's possible that you have given to me. I apologize for my mistake.

I apologize for my mistake.

It's okay.

thanks for the reference's possible that you have given to me.

Have you managed to solve your problem yet? Or do you still need help? If not, please mark the question solved and consider giving those that helped you upvotes + comments :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.