Formatted Output Questions on calculations

I am having a couple of problems. I think I got the formatting down for the most part. I am having issues in a few calculations and how to iterate through them. Basically I need to take the radius and calculate the circumference and the volume. Once I have the calculation I need to print them Radius in Decimal and Binary, then circumference in Hex and finally volume in scientific notation.

Questions.
1.) where would be best to declare circumference and volume?
2.) once they are declared whats the best place to do the calculations?
3.) I can't see an array for this assignment so how would I do this for radius values of 20 - 30.

Please see the attached code.
CS215LAB2.cpp
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

#include <iomanip>
#include <string>
#include "spherefunctions.h"
#include "sphere.h"

using namespace std;
//
//=======================================================================
//                        CONSTANT DEFINITIONS
//              <Constants which apply only to the file>
//
//=======================================================================
//                      EXTERNAL CLASS VARIABLES
//
//=======================================================================


//***********************************************************************
//                     BEGINNING OF PROGRAM CODE
//***********************************************************************
int main()
{
	double volume = 0;
	double circumference = 0;
//***********************************************************************
//                     Function Call to Center Text 
//***********************************************************************
	centerLine("Data On Spheres CS215 Sec ",80);
	centerLine("By,",80);
	centerLine("Kris Armstrong",80);
	
//***********************************************************************
//                     Function Call to Print Doulbe Horizontal Line
//***********************************************************************	
	printDoubleHorizontalLine(80);

//***********************************************************************
// Print values in decimal, binary, hex and scientific
//***********************************************************************	
		for(int i=20; i<31; i++)
			sphere::SetRadius(i); 
			printDecimal(i);
			printBinary (i);
	printHex(circumference);
	printScientific(volume);

//***********************************************************************
//                     Function Call to Print Doulbe Horizontal Line
//***********************************************************************	
	printDoubleHorizontalLine(80);
	
	return 0;
}



sphere.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#ifndef CS215Lab2_sphere_h //Preprocessor directives to prevent duplicate
#define CS215Lab2_sphere_h // declarations. Use the file name of the identifier

//***********************************************************************
//                     Class declaration for sphere
//***********************************************************************

class sphere {
	int radius();
	
public:
	sphere();
	sphere(int InRadius);
	void(SetRadius(int NewRadius));
	int GetRadius();
	float CalcCircumference();
	float CalcVolume();
};

#endif


sphere.cpp
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

#include "sphere.h"
#include <iostream>
using namespace std;
//=======================================================================
//                        CONSTANT DEFINITIONS
//              <Constants which apply only to the file>
const double PI = 3.14159265;

//***********************************************************************
// Constructor For sphere
//***********************************************************************
sphere::sphere()
{
	int radius=0;
}a

//***********************************************************************
// member function to set the radius
//***********************************************************************
void sphere::SetRadius(int newRadius)
{
	radius = newRadius <= 0 ? 1 : newRadius;
}    //to avoid the "0" issue and set a new radius

//***********************************************************************
// member function to get the radius
//***********************************************************************
int sphere::GetRadius()
{
	for(int i = 20; i < 31; i++)
		sphere::SetRadius(i);
}

//***********************************************************************
// member function to calculate circumference
//***********************************************************************
float sphere::CalcCircumference()
{
	double circumference = 0;
		circumference = PI * 2(radius)
};

//***********************************************************************
// member function to calculate volume
//***********************************************************************
float sphere::CalcVolume()
{
	double volume = 0;
	volume = radius*radius*radius*PI*4/3
};


spherefunctions.h
1
2
3
4
5
6
7
8
9
10
11
12
13

#ifndef	CS215Lab2_spherefunctions_h //Preprocessor directives to prevent duplicate
#define	CS215Lab2_spherefunctions_h // declarations. Use the file name of the identifier

extern void printDecimal( long inNum );
extern void printHex( long inNum );
extern void printBinary( int inNum, int numDigits = 8 );
extern void printScientific( float inNum );
extern void centerLine( const std::string &text, unsigned int lineLength );
extern void printDoubleHorizontalLine(unsigned int lineLength);

#endif


spherefunctions.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88

#include <iostream>				// for cin,cout
#include <iomanip>				// for formatting cout
#include <string>
#include "spherefunctions.h"	

using namespace std;
//***********************************************************************************

//***********************************************************************
// function is what fills in the '.'
//***********************************************************************
void printDecimal (long inNum)

	char currentFill = cout.fill('.');		
	long currentFlags = cout.flags();		

	cout << left << setw(20) << inNum;
	cout.fill(currentFill);					
	cout.flags(currentFlags);
}

//***********************************************************************
// function puts the value in base 16 format
//***********************************************************************
void printHex(long inNum)				
{
	char currentFill = cout.fill('.');
	long currentFlags = cout.flags();

	cout.setf(ios::hex, ios::basefield);
	cout.setf(ios::showbase);
	cout << left << setw(20) << inNum;
	cout.fill(currentFill);				
	cout.flags(currentFlags);			
}

//***********************************************************************
// function puts the value in base 2
//***********************************************************************
void printBinary(int inNum, int numDigits)		
{
	for(int shiftAmt = numDigits = 0; shiftAmt >=-1; shiftAmt--)
	{
		cout << ((inNum >> shiftAmt) & 01 );
	}
	char currentFill = cout.fill('.');
	
	cout << setw(20 - numDigits) << '.';
	cout.fill(currentFill);
}

//***********************************************************************
// function puts the value in Scientific Notation
//***********************************************************************
void printScientific(float inNum)		
{
	long currentFlags = cout.flags();
	int oldPrecision = cout.precision();
	
	cout.precision(2);
	cout.setf(ios::scientific, ios::floatfield);
	cout << inNum;
	cout.flags(currentFlags);
	cout.precision(oldPrecision);
}

//***********************************************************************
// function to center text
//***********************************************************************
void centerLine(const string &text, unsigned int lineLength)

{
	cout << setw((text.length() + lineLength) /2) << text << endl;
}

//***********************************************************************
// Function to print horizontal line
//***********************************************************************
void printDoubleHorizontalLine(unsigned int lineLength)

{
	char currentFill = cout.fill('=');
	
	cout << setw(lineLength) << '=' << endl;
	cout.fill(currentFill);
}
Topic archived. No new replies allowed.