CMPT 130 Lab 6 - Errors

 

In this lab you are going to fix some errors in a small program.  Use a text editor and the Terminal to create and run your program. The program is intended to calculate the volume of a sphere but contains a number of errors, both compilation errors and logic errors.  Fix all of the errors! 

 

Labs are assessed so make sure that the TA has seen, and marked, your finished work before you leave the lab.

 

Program

Copy and paste the program into your text editor. Then compile it and fix the errors.

 

/*

 * Calculates the  volume of a sphere (4/3 pi r^3)

 * e.g. volume of sphere of radius 1 = 4.19

 *      volume of sphere of radius 2 = 33.51

 *      volume of sphere of radius 13.2 = 9,634.08

 */

 

#include <iostream>

const int PI = "3.14159";

 

int main()

{

 

           float radius;

           float volume;

           char next = 'y'

           cout << "SPHERE VOLUME CALCULATOR" << endl;

           while (next = 'y')

                cout << endl "Please enter the radius of a sphere: ";

                cin >> volume;

                volume = sphere_volume();

                cout << "Radius = " << radius << endl << "Volume = " << volme << endl;

                cout << endl << "Enter y to continue, any other key to exit: ";

                cin << next;

           }

 

     return 0;

}

 

float sphere_volume(float rr)

{

     return 4/3 * PI * r * r * r;

 

 

Output

The program should produce output like this.

 

SPHERE VOLUME CALCULATOR

 

Please enter the radius of a sphere: 3

Radius = 3

Volume = 113.097

 

Enter y to continue, any other key to exit: y

 

Please enter the radius of a sphere: 23

Radius = 23

Volume = 50965

 

Enter y to continue, any other key to exit: y

 

Please enter the radius of a sphere: 1.11

Radius = 1.11

Volume = 5.72872

 

Enter y to continue, any other key to exit: q

 

Information You May Need to Know

The volume of a sphere is equal to 4/3 pr3. 

 

Assessment

1 mark for completion of the lab, or for getting the program to run – even if it returns an incorrect value.

 

 

CMPT 130 Home

 

John Edgar (johnwill@sfu.ca)