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.
//***********************************************************************
// TITLE: Formatted Output
// FILENAME: CS215Lab2.cpp
// PREPARED FOR: CS215, Section CSGA
// PROGRAMMER(S): Kris Armstrong
// DEVELOPMENT DATE: 10/20/2011
// COMPILER USED: Visual Studio 2010
// TARGET PLATFORM: Dell Core i7
//=======================================================================
// PROJECT FILES
// <LIST ALL PROGRAM AND HEADER FILES IN THE PROJECT HERE>
// CS215Lab2.cpp
// sphere.h
// sphere.cpp
// spherefunction.h
// spherefunction.cpp
//
//=======================================================================
// REVISION HISTORY
// List revisions made to the Program
//
// DATE PROGRAMMER DESCRIPTION OF CHANGES MADE
// 10/28/11 Kris Armstrong Original
//
//=======================================================================
//=======================================================================
// PROGRAM DESCRIPTION
// Objective - To exercise some of the C++ I/O stream formatting options
// and to compose, manipulate and format computed values of a class
// (Sphere).
//
// Specifications - Declare and define some global (free) functions that
// will take a float/int number and output it to the screen using decimal,
// hexadecimal, scientific, or binary notation depending on the
// characteristic being output (see sample output below). The values
// passed to these global functions will be values returned from member
// functions of a user-defined Sphere class. Given an object of a Sphere
// class with a single, Radius, data member, produce member functions that
// compute and return values for the Circumference and Volume of the
// object. Using this class and the global functions, print out the
// data in columns for spheres that have radii of (as a minimum) 20 to 30
// in increments of 1 unit. The output should look something like the
// following (NOTE the centering between the margins required for the
// first three lines of output):
//
// INPUTS: Radius Values 20 - 30
//
// OUTPUTS: Radius in Decimal & Binary. Circumference in HEX and
// Volume in Scientific Notation
//
//=======================================================================
// INCLUDE FILES
#include <iostream>
#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;
}
//***********************************************************************
// END OF PROGRAM CODE FILE
//***********************************************************************
//***********************************************************************
//***********************************************************************
// TITLE: Formatted Output
// FILENAME: CS215Lab2.cpp
// PREPARED FOR: CS215, Section CSGA
// PROGRAMMER(S): Kris Armstrong
// DEVELOPMENT DATE: 10/20/2011
// COMPILER USED: Visual Studio 2010
// TARGET PLATFORM: Dell Core i7
//=======================================================================
// PROJECT FILES
// <LIST ALL PROGRAM AND HEADER FILES IN THE PROJECT HERE>
// CS215Lab2.cpp
// sphere.h
// sphere.cpp
// spherefunction.h
// spherefunction.cpp
//
//=======================================================================
// REVISION HISTORY
// List revisions made to the Program
//
// DATE PROGRAMMER DESCRIPTION OF CHANGES MADE
// 10/28/11 Kris Armstrong Original
//
//=======================================================================
//
//=======================================================================
// STANDARD AND USER DEFINED INCLUDES
#include <iostream>
//***********************************************************************
// PROCESS THIS FILE ONLY ONCE PER PROJECT
//
#ifndef CS215Lab2_sphere_h //Preprocessor directives to prevent duplicate
#define CS215Lab2_sphere_h // declarations. Use the file name of the identifier
//substituting an _ underscore for the period
//=======================================================================
// CONSTANT DEFINITIONS
// <Constants which apply only to the file>
//
//=======================================================================
// GLOBAL CONSTANTS & OBJECTS
// <Variables and Constants exported to other files>
//=======================================================================
//***********************************************************************
// Class declaration for sphere
//***********************************************************************
class sphere {
int radius();
public:
sphere();
sphere(int InRadius);
void(SetRadius(int NewRadius));
int GetRadius();
float CalcCircumference();
float CalcVolume();
};
#endif
//***********************************************************************
// END OF PROGRAM CODE FILE
//***********************************************************************
//***********************************************************************
//***********************************************************************
// TITLE: Formatted Output
// FILENAME: CS215Lab2.cpp
// PREPARED FOR: CS215, Section CSGA
// PROGRAMMER(S): Kris Armstrong
// DEVELOPMENT DATE: 10/20/2011
// COMPILER USED: Visual Studio 2010
// TARGET PLATFORM: Dell Core i7
//=======================================================================
// PROJECT FILES
// <LIST ALL PROGRAM AND HEADER FILES IN THE PROJECT HERE>
// CS215Lab2.cpp
// sphere.h
// sphere.cpp
// spherefunction.h
// spherefunction.cpp
//
//=======================================================================
// REVISION HISTORY
// List revisions made to the Program
//
// DATE PROGRAMMER DESCRIPTION OF CHANGES MADE
// 10/28/11 Kris Armstrong Original
//
//=======================================================================
//=======================================================================
// CLASSES, FREE, AND FRIEND FUNCTIONS IMPLEMENTED
// class Entry
// Definition of class Entry for spheres
//
//***********************************************************************
// CONSTANTS
//
//***********************************************************************
// STANDARD AND USER DEFINED INCLUDES
#include "sphere.h"
#include <iostream>
using namespace std;
//=======================================================================
// CONSTANT DEFINITIONS
// <Constants which apply only to the file>
const double PI = 3.14159265;
//=======================================================================
// EXTERNAL CLASS VARIABLES
//
//=======================================================================
//***********************************************************************
// Constructor For sphere
//***********************************************************************
sphere::sphere()
{
int radius=0;
}
//***********************************************************************
// 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
};
//***********************************************************************
// END OF PROGRAM CODE FILE
//***********************************************************************
//***********************************************************************
//***********************************************************************
// TITLE: Formatted Output
// FILENAME: CS215Lab2.cpp
// PREPARED FOR: CS215, Section CSGA
// PROGRAMMER(S): Kris Armstrong
// DEVELOPMENT DATE: 10/20/2011
// COMPILER USED: Visual Studio 2010
// TARGET PLATFORM: Dell Core i7
//=======================================================================
// PROJECT FILES
// <LIST ALL PROGRAM AND HEADER FILES IN THE PROJECT HERE>
// CS215Lab2.cpp
// sphere.h
// sphere.cpp
// spherefunction.h
// spherefunction.cpp
//
//=======================================================================
// REVISION HISTORY
// List revisions made to the Program
//
// DATE PROGRAMMER DESCRIPTION OF CHANGES MADE
// 10/28/11 Kris Armstrong Original
//
//=======================================================================
//
//***********************************************************************
// PROCESS THIS FILE ONLY ONCE PER PROJECT
//
#ifndef CS215Lab2_spherefunctions_h //Preprocessor directives to prevent duplicate
#define CS215Lab2_spherefunctions_h // declarations. Use the file name of the identifier
//substituting an _ underscore for the period
//=======================================================================
// CONSTANT DEFINITIONS
// <Constants which apply only to the file>
//
//=======================================================================
// GLOBAL CONSTANTS & OBJECTS
// <Variables and Constants exported to other files>
//=======================================================================
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
//***********************************************************************
// END OF PROGRAM CODE FILE
//***********************************************************************
//***********************************************************************
//***********************************************************************
// TITLE: Formatted Output
// FILENAME: CS215Lab2.cpp
// PREPARED FOR: CS215, Section CSGA
// PROGRAMMER(S): Kris Armstrong
// DEVELOPMENT DATE: 10/20/2011
// COMPILER USED: Visual Studio 2010
// TARGET PLATFORM: Dell Core i7
//=======================================================================
// PROJECT FILES
// <LIST ALL PROGRAM AND HEADER FILES IN THE PROJECT HERE>
// CS215Lab2.cpp
// sphere.h
// sphere.cpp
// spherefunction.h
// spherefunction.cpp
//
//=======================================================================
// REVISION HISTORY
// List revisions made to the Program
//
// DATE PROGRAMMER DESCRIPTION OF CHANGES MADE
// 10/28/11 Kris Armstrong Original
//
//=======================================================================
//*****************************************************************************
// INCLUDE FILES
#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);
}
//***********************************************************************
// END OF PROGRAM CODE FILE
//***********************************************************************
//***********************************************************************