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

Assistance with C++ project please: This programming assignment uses a C++ class

ID: 3864977 • Letter: A

Question

Assistance with C++ project please:

This programming assignment uses a C++ class to represent data in order to solve a significant problem.

Politicians...

There's nothing like politics to make you wonder how the human race has managed to exist for as long as it has..

In a pseudo-alternate future, The Ruling Party completely dismantles the forward-thinking Environmental Protection Agency opening the way for the destruction of all natural resources large and small. Shortly afterward, things begin to change... One change is that pigeons begin to mutate in several interesting ways! One strain of pigeons begins to increase in size. With the increased size comes an increase in the size of their digestive tract...and everything associated with that. Oddly, most of these mutants seem to exist in and around Washington, DC. Moreover, they seem to target the same spots whenever they do what pigeons are designed for. Moreover, they become extremely accurate!

As usual, the American Taxpayers have to pay the cost to clean up the mess in Washington, DC.

It turns out that the pigeons are so accurate and consistent that they've created circular messes. Each mess has a center and radius which are easily determined. So, it's possible to calculate the total area covered by all of the circular messes. So, in order to call for bids, the Bureau of Sidewalk Cleanup in Washington, DC needs that calculation done. The only problem is that The War On Science has been so effective that very few people remain who are capable of performing that calculation...

Late one night there's a knock on your door... Two big, thug-like guys wearing black suits and sunglasses burst into your room and "invite" you to write a computer program that will perform the area calculation as a service to your government.

Circle class

Begin by creating a Circle class, Circle.h. This class should have two constructors -- a default constructor and a 3-parameter constructor. The 3-parameter constructor constructs from the coordinates of the center point and the radius. The class should have three private doubles for the radius and the center point's x- and y-coordinates. It should also have a class variable that keeps track of how many Circles are in existance. Consequently, you'll need a copy constructor and a destructor to keep that count correct. Give the class a toString() function so you can see what a Circle looks like. Finally, you'll need to supply accessor functions (getters and setters) named getCenterX(), getCenterY(), setCenterX(), setCenterY(), getRadius(), and setRadius() as well as a getCircleCount()function to access the count.

Code up the Circle.h file separating the definition and the implementation parts as we did for the Point class.

Here is a small program which tests (some of) the basic functionality of your Circle class. You'll know when you've got things working correctly. Try changing the parameter on fiddle() to be a reference parameter to see what happens! Make sure your circle count remains consistent with the parameter types.

Loading Circles

Data for this project comes from files. Each file begins with a number which tells how many circles are in the file. Each circle then appears as x y r on a line where x and y are the center point's coordinates and r is the radius. The files may contain comment lines or blank lines anywhere. Blank lines have length 0. (That's a hint...) Comment lines begin with '%' (possibly preceded by spaces).

There are fewer than 500 circles in Washington, DC. Consequently, there are potentially 500 circles in a file (most likely many less). So, you can safely load each file into an array of Circles which has 500 slots.

Begin the project by writing a program which will load an array of Circles from a user-specified file. Here are several files to test with:

Add a dumpArray() function to print out the Circles in the array so that you can make sure you know how many array slots are occupied by Circles. (And where they are!!) Make your dumpArray()smart enough to stop at the end of the slots occupied by real Circles.

Area of circles

Add a function, double calcArea() which will calculate the total area of all of the circles (the sum of all of their areas). Its parameters are the array of Circles and the index of the last occupied slot in the array. It should return the total area of all circles in the array.

Intersecting circles

Since we are concerned with calculating area covered by the messes we have to be careful of circles that overlap. If two circles overlap and you just bill the government for the total area of the circles, you'll be counting the area of intersection twice. You can't charge for the area of their overlap twice or the Government will hunt you down!

The first step is to determine if two circles actually intersect. How do you know if they intersect? How far apart do their center points have to be for the circles NOT to intersect?

Add a function named bool circlesIntersect(Circle a, Circle b) which answers the question "Do circles a and b intersect?". Circles intersect even if they have one 1 point in common.. Add code to test your function.

Intersection

If your code can correctly determine that two circles intersect the next question to deal with is what kind of intersection do they have? If they intersect in a single point, they're tangent to each other and the area of their overlap is 0. If one circle is totally contained in the other, then the area of their overlap is the area of the inner circle. Add a function (or two) that will determine if either of these cases happen for two intersecting circles.

A third case can happen for intersecting circles. Determining the area of overlap in this case isn't hard but it isn't trivial either. Here is a little document that gives directions for finding the area of the overlapping region of two circles for this third case.

Hack together a short program that uses functions to calculate the area of overlap for the third case. In order to get the inverse sine function, you need to include the cmath library (i.e., #include <cmath>). The name of the function is asin() (for "arcsin"). It's not hard to use...Google is your friend if you don't have access to section 4.2 of the textbook. Make sure it works correctly and then transfer your functions into your main program. Here are some example circles to test on

Once you're certain that you can correctly calculate the area covered by 2 overlapping circles, move those functions back into your main program. Add code which will calculate the total of all of the overlapping areas of all of the circles. (This is basically just a pair of nested loops.)

By a genetic quirk of fate, when the pigeons mutated, they became strategic! The messes occur in a pattern where the intersections only involve two circles or fewer. Consequently, the area covered by all of the circles in a file is the sum of all of their areas minus the area of overlap for all circles that overlap.

Add/adjust your code to display the area covered by all of the circles in the file.

Here are 3 files which contain 2 circles each to help you test your calculations.

Example output

Here is a file with lots of intersections. Good luck! (The circles in the file look like this.)

Sanity check!!

The file 2c.txt provides an easy check to see if you're doing stuff right. Here are some of the calculated values and other stuff for that file.

Here is what i came up with:.....

4circles.txt      4 non-overlapping circles tfile.txt 7 circles lotsaoverlap.txt 7 overlapping circles Input file t file txt Here are the circles that were loaded (211,90) 32 (2005, 146) 40 (249, 154) 17 (201, 197) 23 (154, 156) 20 (140, 180) 26 (199,283) 72 Here are their intersections (211,90) 32 intersects (205, 146) 40 (205,146) 40 intersects (249, 154) 17 (205, 146) 40 intersects (201, 197) 23 (205, 146) 40 intersects (154,156) 20 (201, 197) 23 intersects (199, 283) 72 (154, 156) 20 intersects 140, 180) 26

Explanation / Answer

#include<fstream>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<iomanip>
#include<iostream>
using namespace std;

class BOOK
{
public:
   char ID[6];
   char Content[50];
   char AUTHOR[20];
   char Book[20];
  
   void create_book()// create new BOOK
   {
       cout<<" NEW BOOK ENTRY... ";
       cout<<" Enter The BOOK ID.";
       gets(ID);
       cout<<" Enter BOOK NAME ";
       gets(Content);
       cout<<" Enter The AUTHOR's Name ";
       gets(AUTHOR);
       cout<<" Enter The Source Book ";
       gets(Book);
       cout<<" BOOK Created..";
       getch();
   }  
   void report()
   {cout<<ID<<setw(10)<<Content<<setw(30)<<AUTHOR<<setw(20)<<Book<<endl;} //setw--> set width

   void Copy(BOOK hd){ // this function copies whole record
       strcpy(ID,(hd.ID)); //copy BOOK ki id
       strcpy(AUTHOR,(hd.AUTHOR)); // copy BOOK ka AUTHOR
       strcpy(Book,(hd.Book));
       strcpy(Content,(hd.Content));
   }

}; //class ends here

void SwapBOOK(BOOK &hd1, BOOK &hd2){ //swaps position of BOOK1 with BOOK2

   BOOK Temp;

   Temp.Copy(hd1); //BOOK1 ko temp main copy kiya
   hd1.Copy(hd2); //BOOK2 ko BOOK1 main copy kiya (now BOOK2 has moved to the place of BOOK1)
   hd2.Copy(Temp); //BOOK1 ko finalyy BOOK2 ki jaga per copy ker diya
}

fstream fp; // fstream provides an interface to read and write data from files as input/output streams.
ofstream ofp; // ofstream provides an interface to write data to files as output streams
BOOK hd[1000]; //array of 1000 BOOK
int countBOOK=0; // initially there are zero aBOOK in the program

void ReportAll(){ // this function outputs a
   system("cls");
   for(int i=0;i<countbook;i++){>
       hd[i].report();

   }
   getche();  
}
void writeAllBOOK()
{
   char ch;
   ofp.open("BOOK.dat",ios::trunc); // purani file (BOOK.dat) del ker k poora naya content overwrite ker do in new file
   //If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one.
       system("cls");
       for(int i=0; i < countBOOK;i++){

           ofp.write((char*)&(hd[i]),sizeof(BOOK)); // hd[i] ko char samaj k write ker do BOOK
  
       }
       ofp.flush(); // abhi write ker do ... buffer istimal mat kero
ofp.close();// close file
}
BOOK temp;
void LoadAllBOOK()
{
   system("cls");
   fp.open("BOOK.dat",ios::in);// read file
   if(!fp) // if file doesnt opens
   {
       cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
        getch(); // character lo
           countBOOK =0;
        return; // or waapis peechhay walay menu main chal do
    }

   int i=0;
       while(fp.read((char*)&temp,sizeof(BOOK))) // file say BOOK k siz ko as a string read ker raha hai and loading to memory
   {
       hd[i++].Copy( temp); // copying BOOK to main array
   }
       countBOOK=i; // jitni dafa ye kaam ho 'count BOOK' ko utni dafa increment day do
    fp.close(); // close file
    getch();
}

void SortByAUTHOR() // bubble sort
{
   for(int i=0; i< countBOOK-1; i++){
       for(int j=0; j <countbook-1;>           if(strcmp(hd[j].AUTHOR,hd[j+1].AUTHOR)>0)
               SwapBOOK(hd[j],hd[j+1]); // if above condition is satisfied, then call 'swapBOOK' function which is defined by us
       }
   }
}
void SearchBetweenIndex(int IndexA, int IndexB, char* AUTHOR)
{
   if(IndexA > IndexB){
       int temp;
       temp = IndexA;
       IndexA = IndexB;
       IndexB= temp;
   }
   for(int i = IndexA; i <= IndexB;i++){
       if(strcmp(hd[i].AUTHOR,AUTHOR)==0)
           hd[i].report();
   }
   getch();
}
void ListByAUTHOR(char* AUTHOR){ //search by AUTHOR   
   int PreviousIndex = 0; // first
   int StartIndex =0 , EndIndex = countBOOK-1;  
   int i=2;
   while(1==1){
       int CurrentIndex=(EndIndex+StartIndex)/2; //start searching from the mid position
       if(strcmp(hd[CurrentIndex].AUTHOR, AUTHOR) > 0){
           PreviousIndex = EndIndex;
           EndIndex = CurrentIndex;
       }else if(strcmp(hd[CurrentIndex].AUTHOR, AUTHOR) < 0){
           PreviousIndex = StartIndex;
           StartIndex = CurrentIndex;
       }else
           {
               SearchBetweenIndex(StartIndex, EndIndex, AUTHOR);
               break;
           }
       if(CurrentIndex == PreviousIndex)
           break;
   }

}
void SortAndSearchByAUTHOR(){ // INPUT AUTHOR SEARCH CRITERIA
   system("cls");
   char str[50];
   cout << "Enter the Search Criteria for AUTHOR ";
   gets(str);
   SortByAUTHOR(); // CALL THIS FUNCTION
   ListByAUTHOR(str); // CALLL THIS FUNCTION

void main()
{
   char ch;
   LoadAllBOOK();
   do
   {
       system("cls");
       cout<<" MAIN MENU";
       cout<<" 01. Create BOOK";
       cout<<" 02. SORT And Search By AUTHOR";
       cout<<" 03. lIST ALL ABOOK";
       cout<<" 04. EXIT";
       cout<<" Please Select Your Option (1-4) ";
       ch=getche();
       switch(ch)
       {
   case '1': system("cls");
               hd[countBOOK].create_book();
               countBOOK++;
              break;
       case '2':
               SortAndSearchByAUTHOR();
               break;
       case '3':
               ReportAll();
               break;
       case '4':
               writeAllBOOK();
               exit(0);
           default :cout<<"";
       }
   }while(ch!='4');
}

</iostream></iomanip></string.h></process.h></stdio.h></conio.h></fstream>

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