Homework help

I am supposed to write a program that prompts the user to enter the radius of a circle then removes small increments of the circle so it forms a cone and then calculates the volumes of the cone till you get the maximum volume of the cone then output the max volume of the cone and the size of the total piece removed.


This is my third wack at this and this is what I have so far but it's still not right. The larger entries turn into gibberish.

Any hints?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 #include <iostream>
#include <cmath>

using namespace std;

int main() {
   
      double radius, circumference, circumferenceCone, height, volume,y, x, optimal, maxVolume, hypotenuse, coneRadius, counter, twoPI;
      const double pi = 3.141592654;

      
      
      cout << "Input radius of the circular waxed papaer :";
      cin >> radius;

      hypotenuse = radius;

      twoPI = pi * 2;
      circumference = 2 * pi * radius;
      x = 0.09;
      counter = 0;
      maxVolume = 0;
      optimal = 345.5751919;

     
    
      circumferenceCone = circumference;

       
      do 
      {

         
          maxVolume = volume;   //hold the value from the previous loop
          y = optimal - circumferenceCone;   // gets the circumference from of the piece removed.
          circumferenceCone = circumferenceCone - x;   // removes x of the circle
          coneRadius = circumferenceCone / twoPI;   // calulates the radius of the mouth of the cone
          height =(hypotenuse * hypotenuse) - (coneRadius * coneRadius); //calculates the height of the cone
          height = sqrt(height); // completes the height calculation 
          volume= (pi * height * coneRadius * coneRadius)/3; // calulates the volume of the cone     
          

        
       // cout <<"lth  "<< optimal - circumferenceCone<<"  conecir "<< circumferenceCone <<" conerad "<< coneRadius <<" x "<< x<<" height "<< height <<" vol "<< volume<< endl     // used to trouble shoot 
          

       x= x +.09; 
       counter++; 
   
      }       
     // while (counter < 100); // used to troubleshoot 
       while (maxVolume < volume); // if the maxVolume is less than the new volume loop continues till false
        
      cout << y<< endl;
       
        cout << maxVolume << endl;
        
        
       
        




      




    return 0;
}


I don't necessarily understand the problem, but here's how I rewrote your program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
#include <algorithm>

double const pi = 3.14159, two_pi = 2.0 * pi;

int main()
{
  double const cone_length = 10.0;
  double cone_circumference = 2.0 * pi * cone_length;
  double const cone_circumference_delta = 0.01;
  
  double max_volume = 0.0;
  for (; cone_circumference > 0.0; cone_circumference -= cone_circumference_delta)
  {
    double const cone_radius = cone_circumference / two_pi; 
    double const cone_height = std::sqrt(cone_length*cone_length - cone_radius*cone_radius);
    double const cone_volume = pi * cone_radius*cone_radius * cone_height / 3.0;
    max_volume = std::max(max_volume, cone_volume);
  }
  
  std::cout << max_volume;
}


The larger entries turn into gibberish.

Give us an example input.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cmath>
using namespace std;

const double PI = 4.0 * atan( 1.0 );

int main()
{
   double R;
   cout << "Enter circle radius: ";   cin >> R;
   cout << "Area of circle removed = " << PI * R * R * ( 1.0 - sqrt( 2.0 / 3.0 ) ) << '\n';
   cout << "Maximum volume of cone = " << ( 2.0 / 9.0 / sqrt( 3.0 ) )* PI * R * R * R << '\n';
}


Enter circle radius: 10
Area of circle removed = 57.6493
Maximum volume of cone = 403.067


(which coincides with @mbozzi's code's answer).
Last edited on
Topic archived. No new replies allowed.