Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Language is C++, modify the example codes to do the following: Generate an array of structs with 50 points, which are randomly initiated with x

Language is C++, modify the example codes to do the following:

Generate an array of structs with 50 points, which are randomly initiated with x (0.0-30.0) and y (0.0-20). Then modify the Quick Sort algorithm to sort points. If two points has same x values, then compares y values.. Assume that the struct is

struct point { double x; double y; }; 

example code:

#include  #include  #include  #include  #define N 20 using namespace std; void swap(int A[], int i, int min); bool small(int a, int b); void print(int A[]); void fillRandom(int A[], int size); void sort(int A[], int lo, int hi); int partition(int A[], int lo, int hi); int main(int argc, const char * argv[]) { int A[N]={0}; fillRandom(A,N); print(A); //Shuffle(A); shuffle the array. commentted out for now sort(A, 0, N-1); print(A); } void sort(int A[], int lo, int hi) { if (hi <= lo) return; int j = partition(A, lo, hi); sort(A, lo, j-1); sort(A, j+1, hi); } int partition(int A[], int lo, int hi) { int i = lo, j = hi+1; while (true) { while (small(A[++i], A[lo])) // find item on left to swap if (i == hi) break; while (small(A[lo], A[--j])) // find item on right to swap if (j == lo) break; if (i >= j) break; //check if pointers cross swap(A, i, j); } swap(A, lo, j); return j; // return index of item now known to be in place } void swap(int A[], int i, int min) { int tmp= A[i]; A[i]=A[min]; A[min]=tmp; } void fillRandom(int A[], int size) { for (int i = 0; i < N; i++) A[i]=rand() % 50; } void print(int A[]) { for (int i = 0; i < N; i++) cout << A[i] << ", "; cout << endl; } bool small(int a, int b) { if (a <= b) return true; return false; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

3rd Edition

0471418455, 978-0471418450

More Books

Students also viewed these Databases questions

Question

Prepare an ID card of the continent Antarctica?

Answered: 1 week ago

Question

What do you understand by Mendeleev's periodic table

Answered: 1 week ago

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago