Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ The file below contains first and last names and the radii of circles. You\'

ID: 3586026 • Letter: C

Question

C++

The file below contains first and last names and the radii of circles. You'll need to cut and paste the data into a text file. Write a program to read the file and store the first names and the radii in two arrays. You'll need an array of type string and an another array of some numeric type. The arrays should each hold 12 elements. Then, print out the first names and the circle areas, each in a column of width 10. Make sure you match the implied output format and that you check for a successful file open. Include a function to calculate of the circle area, using the formula:

Area = PI * radius * radius

For PI, use a named constant equal to 3.14

Input data (copy to a text file):

LoriBeth Allen    79
Chachi Arcola      125
Richie Cunningham 171
Howard Cunningham 255
Marion Cunningham 252
Joanie Cunningham 234
Al Delvecchio      122
Arthur Fonzarelli 255
Ralph Malph        165
Roger Phillips 61
Jenny Piccalo 55
Potsie Weber       210

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

// declaring constant
const float PI = 3.14f;

// function that returns area of circle when taken radius
double area(int x)
{
return PI*x*x;
}

int main() {
  
// Declaring variables
int sum = 0, index;
string lname;
ifstream inF;
  
// declaring arrays
string firstns[12];
int radii[12];
  
// opening and checking if file exists
inF.open("radii.txt");
if (!inF) {
cout << "File doesn't exists";
exit(1);
}
  
// looping for 12 times
for(int i=0;i<12;i++)
{
// reading first name, last name and radii
inF >> firstns[i];
inF >> lname;
inF >> radii[i];
  
// printing name and area by calling function
cout << setw(10) << firstns[i];
cout << setw(10) << area(radii[i]) << endl;
}
  
// closing file
inF.close();
return 0;
}

/* SAMPLE OUTPUT
LoriBeth 19596.7
Chachi 49062.5
Richie 91816.7
Howard 204178
Marion 199403
Joanie 171934
Al 46735.8
Arthur 204178
Ralph 85486.5
Roger 11683.9
Jenny 9498.5
Potsie 138474
*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote