// specify the maximum number of circles the program is capable of handling const int MaxCircles = 20; struct Circle { double x,y; // (x,y) coords for the centre of the circle double rad; // radius of the circle (must be > 0) }; // get the desired number of circles from the user, 1..max int getNumCircs(int max); // get a real number from the user, optionally restricted to positive numbers only double getNumber(string promptInfo, bool mustBePositive = false); // fill all the fields of the circle struct (uses getNum for each) void fill(Circle& c); // fill all the circles (using the fill function) void fillAll(Circle circs[], int num); // see if two circles overlap bool overlap(Circle c1, Circle c2); // print one circle's information as (x,y:r) void print(Circle c); // print a list of all overlapping circles void printOverlaps(Circle circs[], int numCircs); |
int main() { // array of maximum number of circles Circle circles[MaxCircles]; // find out how many the user actually wants int numberCircles = getNumCircs(MaxCircles); // fill the circles fillAll(circles, numberCircles); // display all the overlaps printOverlaps(circles, numberCircles); } |
(Now that the programs are getting more complex and involve more data, a standardized batch of tests gets run on all student programs in addition to me reading your code. The test program provides fixed sets of test values in fixed orders, following the patterns in the code design below. If your program expects different information or in a different order then it won't work and will fail the testing.) |
// get the desired number of circles from the user, 1..max int getNumCircs(int max) { // get the number of circles from the user int numCs; cout << "Please enter the desired number of circles, 1.." << max << ": "; cin >> numCs; // error check and recurse if needed if (cin.fail()) { cin.clear(); cin.ignore(LineLen, '\n'); cerr << "The value must be an integer, please try again" << endl; numCs = getNumCircs(max); } else if ((numCs < 1) || (numCs > max)) { cerr << "The value must be in the range 1.. " << max << ", please try again" << endl; numCs = getNumCircs(max); } // return the validated result return numCs; } // get a real number from the user, optionally restricted to positive numbers only double getNumber(string promptInfo, bool mustBePositive) { // prompt the user, adjusting for the must-be-positive restriction if needed cout << "Please enter a number"; if (mustBePositive) { cout << " greater than zero"; } cout << " for the " << promptInfo << ": "; // get the user's input double num; cin >> num; // error check and recurse if needed if (cin.fail()) { cin.clear(); cin.ignore(LineLen, '\n'); cerr << "The value must be a number, please try again" << endl; num = getNumber(promptInfo, mustBePositive); } else if (mustBePositive && (num <= 0)) { cerr << "The value must be greater than 0, please try again" << endl; num = getNumber(promptInfo, mustBePositive); } // return the validated result return num; } |
// fill all the fields of the circle struct (uses getNum for each) void fill(Circle& c) { c.x = getNumber("x coord"); c.y = getNumber("y coord"); c.rad = getNumber("radius", true); } |
// fill all the circles (using the fill function) void fillAll(Circle circs[], int num) { // for each of positions 0 through num-1, // call fill on circs[c] } |
// see if two circles overlap bool overlap(Circle c1, Circle c2) { // calculate the distance between the two centres double xdist = c1.x - c2.x; double ydist = c1.y - c2.y; double distance = sqrt(xdist*xdist + ydist*ydist); // if the distance is more than the combined radii then they do not overlap if (distance > (c1.rad + c2.rad)) { return false; } // otherwise they overlap (or at least touch) return true; } // print one circle's information as (x,y:r) void print(Circle c) { cout << "(" << c.x << "," << c.y << ":" << c.rad << ")"; } // print a list of all overlapping circles void printOverlaps(Circle circs[], int numCircs) { // we'll need a variable to track how many overlaps we've detected, initialized to 0 // we'll use nested for loops that compares each circle with every circle later in the list // (to avoid printing overlapping pairs twice) // for c1 is 0 to numCircs-2 // for c2 is c1+1 to numCircs-1 // call overlap on circs[c1] and circs[c2], and if it // says they overlap then print the two of them // (and increment our overlap counter) } |
Please enter the desired number of circles, 1..20: 3 Now filling circle number 0 Please enter a number for the x coord: 1 Please enter a number for the y coord: 1 Please enter a number greater than zero for the radius: 1 Now filling circle number 1 Please enter a number for the x coord: -1.5 Please enter a number for the y coord: 0.99 Please enter a number greater than zero for the radius: 0.85 Now filling circle number 2 Please enter a number for the x coord: 0 Please enter a number for the y coord: 0.1 Please enter a number greater than zero for the radius: 0.99 Detecting overlapping circles... Overlapping pair: (1,1:1) and (0,0.1:0.99) Overlapping pair: (-1.5,0.99:0.85) and (0,0.1:0.99) Number of overlaps detected: 2 |
Welcome to the BunnySighting program! You'll be asked to enter information about your recent bunny sightings, giving the date, bunny colour, and bunny size for each. First, how many sightings do you wish to enter (1-50)? 4 For sighting 1: a) please enter the year of the sighting (2000-2050): 2024 b) please enter the month of the sighting (1-12): 10 c) please enter the day of the sighting (1-31): 21 d) please enter the bunny colour as a single word (e.g. brown): white e) please enter the estimated bunny size in kilograms (e.g. 1.2): 1 For sighting 2: a) please enter the year of the sighting (2000-2050): 2023 b) please enter the month of the sighting (1-12): 4 c) please enter the day of the sighting (1-31): 1 d) please enter the bunny colour as a single word (e.g. brown): green e) please enter the estimated bunny size in kilograms (e.g. 1.2): 56 For sighting 3: a) please enter the year of the sighting (2000-2050): 2024 b) please enter the month of the sighting (1-12): 8 c) please enter the day of the sighting (1-31): 3 d) please enter the bunny colour as a single word (e.g. brown): grey e) please enter the estimated bunny size in kilograms (e.g. 1.2): 2 For sighting 4: a) please enter the year of the sighting (2000-2050): 2024 b) please enter the month of the sighting (1-12): 6 c) please enter the day of the sighting (1-31): 12 d) please enter the bunny colour as a single word (e.g. brown): black e) please enter the estimated bunny size in kilograms (e.g. 1.2): 0.5 Please select the type of report you wish: 1) print in order entered 2) print sorted by bunny size (small to large) 3) print sorted by date of sighting (earliest to latest) Your choice: 3 The sightings sorted by date were: 2023/4/1: green bunny, 56kg 2024/6/12: black bunny, 0.5kg 2024/8/3: grey bunny, 2kg 2024/10/12: white bunny, 1kg |