Write an interactive C++ program that uses a function to calculate the volume of
ID: 3539348 • Letter: W
Question
Write an interactive C++ program that uses a function to calculate the volume of a ball. The following code has numerous mistakes. Fix the mistakes and add any missing statements to get a working program. Test cases: ----------- 1. radius of 5, should give 523.599 */#include <iostream>
using namespace std;
int main()
{
// 1. input the radius of the ball
cout << "Enter the radius of the ball: ";
cin << radius;
// 2. call the function to compute the volume of the ball
volume_ball(double radius);
// 3. display the results, using the variables radius and volume
cout << "The volume of a ball with radius "
<< radius
<< " is "
<< volume
<< " ";
system("pause");
return 0;
}
//function is defined
char volume_ball(r);
{
double a = 4 / 3 * PI * r * r * r;
return volume_ball;
}
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
// 1. input the radius of the ball
cout << "Enter the radius of the ball: ";
// cin << radius; // Mistake 1
cin >> radius;
// 2. call the function to compute the volume of the ball
// volume_ball(double radius); // Mistake 2
double volume = volume_ball (radius);
// 3. display the results, using the variables radius and volume
cout << "The volume of a ball with radius "
<< radius
<< " is "
<< volume
// << " "; // Mistake 3
<< endl;
system("pause");
return 0;
}
//function is defined
// char volume_ball(r); // Mistake 4
double volume_ball (double r)
{
// double a = 4 / 3 * PI * r * r * r; Mistake 5 - PI not defined
double a = 4/3 * 3.14 * r * r * r;
// return volume_ball; //Mistake 6 - volume_ball is not the variable
return a;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.