Re-write Sample Program 11.2 so that it works for an array of structures. Write the program so that it compares 6 circles. You will need to come up with a new way of determining which circle’s center is closest to the origin.   Sample code 11.2

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Re-write Sample Program 11.2 so that it works for an array of structures. Write the program so that it compares 6 circles. You will need to come up with a new way of determining which circle’s center is closest to the origin.

 

Sample code 11.2

 

#include <iostream>
#include <cmath> // necessary for pow function
#include <iomanip>
using namespace std;
struct circle // declares the structure circle
{ // This structure has 6 members
float centerX; // x coordinate of center
float centerY; // y coordinate of center
float radius;
float area;
float circumference;
float distance_from_origin;
};
const float PI = 3.14159;
int main()
{
circle circ1, circ2; // defines 2 circle structure variables
cout << "Please enter the radius of the first circle: ";
cin >> circ1.radius;
cout << endl
<< "Please enter the x-coordinate of the center: ";
cin >> circ1.centerX;
cout << endl
<< "Please enter the y-coordinate of the center: ";
cin >> circ1.centerY;
circ1.area = PI * pow(circ1.radius, 2.0);
circ1.circumference = 2 * PI * circ1.radius;
circ1.distance_from_origin = sqrt(pow(circ1.centerX,2.0)
+ pow(circ1.centerY,2.0));
cout << endl << endl;
cout << "Please enter the radius of the second circle: ";
cin >> circ2.radius;
cout << endl
<< "Please enter the x-coordinate of the center: ";
cin >> circ2.centerX;
cout << endl
<< "Please enter the y-coordinate of the center: ";
cin >> circ2.centerY;
circ2.area = PI * pow(circ2.radius, 2.0);
circ2.circumference = 2 * PI * circ2.radius;
circ2.distance_from_origin = sqrt(pow(circ2.centerX,2.0)
+ pow(circ2.centerY,2.0));

cout << endl << endl;
// This next section determines which circle's center is
// closer to the origin
if (circ1.distance_from_origin > circ2.distance_from_origin)
{
cout << "The first circle is further from the origin"
<< endl << endl;
}
else if (circ1.distance_from_origin < circ2.distance_from_origin)
{
cout << "The first circle is closer to the origin"
<< endl << endl;
}
else
cout << "The two circles are equidistant from the origin";
cout << endl << endl;
cout << setprecision(2) << fixed << showpoint;
cout << "The area of the first circle is : ";
cout << circ1.area << endl;
cout << "The circumference of the first circle is: ";
cout << circ1.circumference << endl << endl;
cout << "The area of the second circle is : ";
cout << circ2.area << endl;
cout << "The circumference of the second circle is: ";
cout << circ2.circumference << endl << endl;
return 0;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education