// 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 |
struct Container {
string name;
float size;
};
Our main routine will thus just need a single array (whose elements are of type Container),
and our sorting function will just expect to be passed this one array.