Similarly, in the following program, you create an array
that can hold up to 100 cylinders. Then, you fill part or all of it using
a for
reading pattern, and you use another for
loop to add up
the volumes, producing the total volume of the oil storage tanks:
#includeconst double pi = 3.14159; // Define the cylinder class: class cylinder { public: double radius, length; double volume ( ) {return pi * radius * radius * length;} }; // Define cylinder array: cylinder oil_tanks[100]; main ( ) { // Declare various variables: int limit, counter; double radius, length, sum = 0.0; // Read numbers and write them into array: for (limit = 0; cin >> radius >> length; ++limit) { oil_tanks[limit].radius = radius; oil_tanks[limit].length = length; } // Compute volumes: for (counter = 0; counter < limit; ++counter) sum = sum + oil_tanks[counter].volume ( ); // Display sum: cout << "The total volume in the " << limit << " storage tanks is " << sum << endl; } --- Data --- 4.7 40.0 3.5 35.5 3.5 45.0 --- Result --- The total volume in the 3 storage tanks is 5873.91