Question

In: Computer Science

C++ Create a program which randomly generates 3 sets of x and y Integers and one...

C++

Create a program which randomly generates 3 sets of x and y Integers and one randomly generated Integer. Two of the sets of Integers will represent the end points of a line segment. The other set of Integers and the other Integer will represent the midpoint of a circle and its radius.

The coordinates should be randomly generated using a user defined function that returns an Integer value based on from and to parameters; see the function declaration in the Other section below. Your coordinates should be randomly selected from -99 to 99. The radius should be a randomly generated number (using the same function) from 1 to 200.

Two other functions should be created to determine if a line segment is wholly within a circle. One of the functions should return the length of a line segment and the other will return a Boolean indicating if the passed line segment is in the passed circle; again, see the function declarations below.

The program should display all of the generated data and one of the messages regarding the location of the line as shown below in Output Layout section. Try to make everything line up correctly.

Output Layout:

Coordinates of a Random Line Segment
  1st Point's x coordinate: -##
  1st Point's y coordinate: -##
  2nd Point's x coordinate: -##
  2nd Point's y coordinate: -##
    
Coordinates of a Random Circle
  coordinate: -##
  coordinate: -##
  Radius: ###
    
The line segment is within the circle.
  OR
The line segment is not within the circle.

Other: (Required Identifier Names, Prototypes & Random Ranges)

p0x    // an int from -99 to 99
p0y    // an int from -99 to 99
p1x    // an int from -99 to 99
p1y    // an int from -99 to 99
mpx    // an int from -99 to 99
mpy    // an int from -99 to 99
radius // an int from 1 to 200
    
int randomInteger(const int from, const int to);
int lineSegLength(const int p0x, const int p0y, const int p1x, const int p1y);
bool lineInCircle(const int p0x, const int p0y, const int p1x, const int p1y, const int mpx, const int mpy, const int radius);

Solutions

Expert Solution

THANKS FOR THE QUESTION. HERE IS THE COMPLETE CODE IN C++

=====================================================================================

#include<iostream>

#include<cstdlib>

#include<cmath>

using namespace std;

int randomInteger(const int from, const int to){

               

                return from + rand()%(to-from-1);

}

int lineSegLength(const int p0x, const int p0y, const int p1x, const int p1y){

               

                int squared_sum = (p0x-p1x)*(p0x-p1x) + (p0y-p1y)*(p0y-p1y);

                return static_cast<int>(pow(squared_sum,0.5));

               

}

bool lineInCircle(const int p0x, const int p0y, const int p1x, const int p1y, const int mpx, const int mpy, int radius){

               

                int end_point0_distance = lineSegLength(p0x,p0y,mpx,mpy);

               

                if(end_point0_distance>radius)return false;

               

                int end_point1_distance = lineSegLength(p1x,p1y,mpx,mpy);

               

                return end_point1_distance<=radius;

               

}

int main(){

               

                int x1,y1;

                int x2,y2;

                int midpoint_x, midpoint_y; int radius;

                x1=randomInteger(-99,99);

                y1=randomInteger(-99,99);

                x2=randomInteger(-99,99);

                y2=randomInteger(-99,99);

                midpoint_x=randomInteger(-99,99);

                midpoint_y=randomInteger(-99,99);

               

                radius = randomInteger(1,200);

               

                cout<<"1st Point\'s x coordinate: "<<x1<<endl;

                cout<<"1st Point\'s y coordinate: "<<y1<<endl;

                cout<<"2nd Point\'s x coordinate: "<<x2<<endl;

                cout<<"2nd Point\'s y coordinate: "<<y2<<endl;

                cout<<endl;

                cout<<"Coordinates of a Random Circle\n";

                cout<<"coordinate: "<<midpoint_x<<endl;

                cout<<"coordinate: "<<midpoint_y<<endl;

                cout<<"Radius: "<<radius<<endl<<endl;

               

                cout<<endl;

                if(lineInCircle(x1,y1,x2,y2,midpoint_x,midpoint_y,radius)){

                                cout<<"The line segment is within the circle.";

                }else{

                                cout<<"The line segment is not within the circle.";

                }

}

=============================================================================


Related Solutions

Write a program in C which randomly generates two integers X and Y between 10 and...
Write a program in C which randomly generates two integers X and Y between 10 and 50, and then creates a dynamic array which can hold as many numbers as there are between those two random numbers. Fill the array with numbers between X and Y and print it on screen. Do not forget to free the allocated memory locations whenever they are no longer needed. Example: Randomly generated numbers are: 43 23 Expected Output : 23 24 25 26...
IN C++: Create sets of 1 Million integers with the following characteristics; Sets where no numbers...
IN C++: Create sets of 1 Million integers with the following characteristics; Sets where no numbers repeat Sets where the range of numbers is 1% of the array size Sets where no numbers repeat and each integer has 20 digits
Write a Java program with comments that randomly generates an array of 500,000 integers between 0...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime =...
Consider two sets of integers, X = [x1, x2, . . . , xn] and Y...
Consider two sets of integers, X = [x1, x2, . . . , xn] and Y = [y1, y2, . . . , yn]. Write two versions of a FindUncommon(X, Y ) algorithm to find the uncommon elements in both sets. Each of your algorithms should return an array with the uncommon elements, or an empty array if there are no uncommon elements. You do not have to write the ‘standard’ algorithms – just use them. Therefore, you should be...
C++ program which partitions n positive integers into two disjoint sets with the same sum. Consider...
C++ program which partitions n positive integers into two disjoint sets with the same sum. Consider all possible subsets of the input numbers. All in one C++ file. This is the sample Input 1 6 3 5 20 7 1 14 Output 1 Equal Set: 1 3 7 14 This is the sample Input 2 5 10 8 6 4 2 Output 2 Equal Set: 0
Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
Using c++, 1: Create a vector of 20 randomly generated integers, then 2. Create a new...
Using c++, 1: Create a vector of 20 randomly generated integers, then 2. Create a new vector that will only store the even numbers from the original vector. 3. Display the original vector and the vector of even integers to the console.
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the...
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the first quarter of y consists of the value of the first element of x. The second quarter of y consists of the value of the second element of x. The third quarter of y consists of the value of the third element of x. The fourth quarter of y consists of the value of the fourth element of x?
Write a C++ program that 1) generates a vector containing 10 different random integers with values...
Write a C++ program that 1) generates a vector containing 10 different random integers with values between 1 and 100, then 2) calculates the average value in that vector in floating point format with 1 decimal place. Output the vector values and the average value to cout. Your program output should look like this: Vector values: 3, 78, 55, 37, 8, 17, 43, 60, 94, 1 Average value: 39.6
1) If x, y, z are consecutive integers in order then 9 | (x+y+z) ⟺ 3...
1) If x, y, z are consecutive integers in order then 9 | (x+y+z) ⟺ 3 | y. (Do proof) 2) Let x, y be consecutive even integers then (x+y) is not divisible by 4. (Show proof and state why it was used)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT