What is the equation for computing the volume of a sphere using C++?

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Sphere Volume
Click For Summary

Discussion Overview

The discussion revolves around computing the volume of a sphere using C++ programming. Participants are exploring the correct implementation of the mathematical formula for the volume of a sphere in code, addressing issues related to syntax, mathematical operations, and programming practices.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant states the equation for the volume of a sphere is $\frac{4}{3} \pi r^2$ and emphasizes the need to use floating-point division with (4.0 / 3.0).
  • Another participant corrects a misunderstanding about the units of volume, noting it should be in meters cubed, not meters squared.
  • A participant expresses ongoing difficulty in implementing the code correctly and asks for specific code lines to use.
  • There is a suggestion to use repeated multiplication for computing $r^3$ instead of relying on a power function, along with a reminder to include the cmath file for such functions.
  • One participant realizes their mistake in the code syntax, correcting their approach to calculating the volume by using repeated multiplication instead of the power operator.

Areas of Agreement / Disagreement

Participants generally agree on the formula for the volume of a sphere and the need for proper syntax in C++. However, there are unresolved issues regarding the correct implementation in code, and no consensus is reached on the best approach to compute $r^3$.

Contextual Notes

Participants have not fully resolved the implementation details, such as the best method for calculating powers in C++. There are also indications of confusion regarding the use of operators and the inclusion of necessary libraries.

ineedhelpnow
Messages
649
Reaction score
0
Given sphereRadius and piVal, compute the volume of a sphere and assign to sphereVolume. Look up the equation online. Use (4.0 / 3.0) to perform floating-point division, instead of (4 / 3) which performs integer division.

Sample program:

#include <iostream>
using namespace std;

int main() {
const double piVal = 3.14159;
double sphereVolume = 0.0;
double sphereRadius = 0.0;

sphereRadius = 1.0;
<STUDENT CODE>
count << "Sphere volume: " << sphereVolume << endl;

return 0;

ive tried EVERYTHING for this but i still can't figure out how to compute the volume. equation for the volume of a sphere is $\frac{4}{3} \pi r^2$. oh and instead of 4 and 3 i have to use 4.0 and 3.0.
 
Technology news on Phys.org
ineedhelpnow said:
equation for the volume of a sphere is $\frac{4}{3} \pi r^2$.
Volume is measured in meters cubed, not meters squared.

Hint: There is a tag [CODE]...[/CODE] designed specifically for code segments. It provides monospaced font and preserves alignment.
 
oops i missed that. I am still not getting it right though.
 
ineedhelpnow said:
im still not getting it right though.
What code line do you propose? Remember to use sphereRadius and piVal, not their values. Also, use repeated multiplication to compute $r^3$. In order to use the built-in power function, you need to include the cmath file, which the code you've been given does not do.
 
oh thank u thank u thank u thank u thank u! i was doing sphereVolume = (4.0 / 3.0) * piVal * (sphereRadius ^3);

instead of sphereVolume = (4.0 / 3.0) * piVal * (sphereRadius * sphereRadius * sphereRadius)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
89
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
53
Views
5K