write one complete c++ program to calculate the circumference (c=2PIr) of a circ
ID: 3833551 • Letter: W
Question
write one complete c++ program to calculate the circumference (c=2PIr) of a circle and the area (a =PIr^2 ) of a circle. the radius should be entered from a input file an PI should be set up as a constant in the program. the radius, area, and circumference will be stored on an output file with headings. you do not have to include the 2 columns of information at the top of the program or the program description. however, do include any processor commands ( like include, etc). this program should include at leady 4 functions: reafFileData, PrintFileData, cakcArea and call circumference. Use your own end of file pricessing- a trailer of -1, a count in front of the data on the file, ora test for fin.eof().
Explanation / Answer
radiusdata.txt (Save this file under D Drive.Then the path of the file pointing to it is D: adiusdata,txt)
5.5
4.0
6.5
7.0
3.4
2.5
-1
______________
#include <fstream>
#include <iostream>
#include <iomanip>
#define PI 1.41459
using namespace std;
//Function Declarations
double * readFileData(ifstream& dataIn,double rad[]);
void printFileData(ofstream& dataOut,double rad[],int size);
double calArea(double radius);
double calCircum(double radius);
int main() {
//Declaring variables
double radius;
int count=0;
//defines an input stream for the data file
ifstream dataIn;
//Defines an output stream for the data file
ofstream dataOut;
//Opening the input file
dataIn.open("D: adiusData.txt");
if(dataIn.fail())
{
cout<<"** File not found **"<<endl;
return 1;
}
else
{
//This loop will counts the no of records in the inputfile
while(dataIn>>radius)
{
if(radius!=-1)
{
count++;
}
}
dataIn.close();
//creating an double arra based on count
double rad[count];
//calling the function to read the data
readFileData(dataIn,rad);
//calling the function to display the data
printFileData(dataOut,rad,count);
}
return 0;
}
//this function will read the data from the file
double * readFileData(ifstream& dataIn,double rad[])
{
//Declaring variables
double radius;
//Opening the input file
dataIn.open("D: adiusData.txt");
int i=0;
//reading the data till the end of the file
while(!dataIn.eof())
{
//Populating the radius values into an array
dataIn>>radius;
if(radius!=-1)
{
rad[i]=radius;
i++;
}
}
//Closing the input stream
dataIn.close();
return rad;
}
//This function will write the data to output file
void printFileData(ofstream& dataOut,double rad[],int size)
{
//creating and Opening the output file
dataOut.open("D:\circleData.txt");
//Setting the precision to two decimal places
dataOut << std::setprecision(2) << std::fixed;
/* This for loop will calculate the area
* , circumference of circle and
* write that data to the output file
*/
for(int i=0;i<size;i++)
{
dataOut<<"Radius"<<endl;
dataOut<<"------"<<endl;
dataOut<<rad[i]<<endl;
dataOut<<"Area Of Circle"<<endl;
dataOut<<"--------------"<<endl;
dataOut<<calArea(rad[i])<<endl;
dataOut<<"Circumference Of Circle"<<endl;
dataOut<<"--------------"<<endl;
dataOut<<calCircum(rad[i])<<endl;
dataOut<<endl;
}
dataOut.close();
}
//This function will calculate the area of the circle
double calArea(double radius)
{
return PI*radius*radius;
}
//This function will calculate the circumference of the circle
double calCircum(double radius)
{
return 2*PI*radius;
}
_____________________
Output:
circleData.txt (We can See this file under D Drive.As we specified the path of the output file as D:\circleData.txt)
Radius
------
5.50
Area Of Circle
--------------
42.79
Circumference Of Circle
--------------
15.56
Radius
------
4.00
Area Of Circle
--------------
22.63
Circumference Of Circle
--------------
11.32
Radius
------
6.50
Area Of Circle
--------------
59.77
Circumference Of Circle
--------------
18.39
Radius
------
7.00
Area Of Circle
--------------
69.31
Circumference Of Circle
--------------
19.80
Radius
------
3.40
Area Of Circle
--------------
16.35
Circumference Of Circle
--------------
9.62
Radius
------
2.50
Area Of Circle
--------------
8.84
Circumference Of Circle
--------------
7.07
____________________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.