
-
Write a function in c programming language that gets an array of points ( function is defined as :
typedef struct{
int x;
Int y;
} point;
) and sorts the array using qsort().
Given two points a=(ax,ay) and b=(bx,by) we compare them as follows: -
1) if (ax)2+(ay)2 < (bx)2+(by)2, then a should come before b in the sorted array.
-
2) if (ax)2+(ay)2 = (bx)2+(by)2, then we compare the points by the x coordinate.
Remark: For a point a=(ax,ay) the quantity ((ax)2+(ay)2)1⁄2 is the distance of a from the (0,0). That is, we sort the points according to their distance to (0,0), and if for points at the same distance, then we sort them according to the first coordinate.
You will need to implement the comparison function, and apply qsort() on the array with this comparison function.
For example:
-
- Input: [(3,2), (7,1), (1,1), (3,4), (5,0), (7,1)]
-
- Expected output: [(1,1), (3,2), (3,4), (5,0), (7,1), (7,1) ]
Explanation:
(1,1) is first because 12+12=2 is the smallest
(3,2) is next because 32+22 = 13 is the second smallest
Both (3,4) and (5,0) have the same sum of squares, 25, but 3<5 so (3,4) comes before (5,0) (7,1) is least because 72+12=50 is the largest**Note that we do allow some points in the array to be equal.
void sort_points(point* A, int length);

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 2 images

- Function PrintArray(integer array(?) dataList) returns nothing integer i for i = 0; i < dataList.size; i = i + 1 dataList[i] = Get next input Put dataList to output Put "_" to output // Your solution goes here. Modify as needed i = 0 Complete the PrintArray function to iterate over each element in dataList. Each iteration should put the element to output. Then, put "_" to output. Ex: If dataList's elements are 2 4 7, then output is: 2_4_7_ Function Main() returns nothing integer array(3) userNums integer i for i = 0; i < userNums.size; i = i + 1 userNums[i] = Get next input PrintArray(userNums)arrow_forwardWrite a definition for the following C++ function:bool up_then_down(const int* arr, int n);// REQUIRES: n >= 1; elements arr[0] ... a[n-1] exist.// PROMISES: Returns true if the sequence of element values is strictly increasing// from a[0] to the first appearance of the maximum value, then strictly decreasing// from the first appearance of the maximum value to a[n - 1].// Otherwise, returns false. EXAMPLES (maximum values are bold):// The return value would be true for all of these sequences ...// {10}, {10, 20}, {20, 10}, {10, 20, 30, 25}, {10, 20, 30, 25, -2}// But would be false for all of these ...// {10, 20, 10, 15}, {10, 10, 20, 15}, {10, 20, 20, 15}.arrow_forwardWrite a program in C to get the largest element of an array and average using the function. Input the number of elements to be stored in the array :5 array Input 5 elements in the array: ired in element - 0:1 element 12 element -2:3 I element - 3:4 element - 4:5 Expected Output: The largest element in the array is: 5 The average is: 3arrow_forward
- Consider the following functions: (4) int num2) int hidden (int numl, if (numl > 20) numl = num2 / 10; else if (num2 num2 = num1 / 20; > 20) else return num1 - num2; return numl * num2; int compute (int one, int two) { int secret = one; for (int i = one + 1; i <= two % 2; i++) secret = secret + i * i; return secret; What is the output of each of the following program segm a cout << hidden(15, 10) << endl;arrow_forwardHelp with writing a program a C program implements a bubble sort algorith on an array of integers, and use command line paramameters to populate an array with data. The program should follow below guidelines if possible: If there are no command-line arguments at all when the program is run, the program should print out instructions on its use (a "usage message"). There should be one common usage message (consider a method/function for printing the usage message) for any type of usage error. The program will accept an A or D as the second command line argument (after the program name). This letter will tell you whether the bubble sort should sort in ascending or descending fashion. Anything other than A or D in that position should display the usage message and terminate the program. The program will be able to accept up to 32 numbers (integers) on the command line. If there are more than 32 numbers on the command line, or no numbers at all, the program should print out the usage…arrow_forwardThe density of freshwater as a function of temperature (in Celsius) can be modeled: p = 5.53 x 10-873 – 8.50 x 10-67? + 6.56 x 10-5T. + 1.00 %3D Write a function with structure function rho density(T) that takes an array of temperatures T in Fahrenheit as an input and returns an array of corresponding densities as an output. An anonymous function that converts Fehrenheit to Celsius is provided.arrow_forward
- Write a C++ program that will dynamically allocate a 2D array of size MxN, where M and N will be entered by the user. Program should fill the MxN matrix according to the following criteria: Generate random numbers in the range 120 to 340 and fill the diagonal entries in the matrix. Each element in the upper triangular matrix will be calculated by adding its left and bottom neighbor elements. Each element in the lower triangular matrix will be calculated by subtracting the top element from the element on the right. Element in Upper Triangular Matrix = Left Element + Bottom Element Element in Lower Triangular Matrix Right Element - Top Element (A sample 5x5 matrix is filled for you ) 89 500 1100 1955 3886 322 411 600 855 1931 544 -222 189 255 1076 643 99 -123 66 821 70 713 812 689 755 Program will display the filled matrix in tabular form on the screen and delete the dynamically allocated 21 array before terminating.arrow_forwardint getMax(int arr[], int n) { intmx=arr[0]; for (inti=1; i<n; i++) if (arr[i] >mx) mx=arr[i]; returnmx; } Can u give me the code for this one as well....this is the first function and countsort is the secondarrow_forwardWrite a function using Java Function Name: winInRowParameters: board: 2D integer array, row: integer, piece: integerReturn: booleanAssume board is valid 2D int array, row is valid index in the board, piece is X==1/O==2Look at indicated row at given index in board. If that row has at least 3 consecutive entries withgiven type of piece (X/O) (3 in a row XXX, OOO), then return true, otherwise false.arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





