The selection sort is generic. That is, the sorting algorithm is the same:
|
|
Write a program that reads in a list of 3D points (x,y,z) and prints
them out in increasing distance from the origin. Each 3D-point should be
represented as an array of size 3.
Below is a sample run of the program:
How many points? 5 Enter points (x,y,z): (3.6, 0.0, -2.1) (1.1, -1.1, -1.1) (3.0, 0.0, 0.0) (-2.6, -1.8, 2.7) (3.0, 0.5, 0.5) (1.1,-1.1,-1.1) (3,0,0) (3,0.5,0.5) (-2.6,-1.8,2.7) (3.6,0,-2.1)
Q: What should be double* -- each object is a 3D point (an array of size 3) Here's a solution. Hopefully you see that this doesn't require too many changes to the above program. |
|
a and
b are pointers. Hopefully the above picture will give you
an idea:
int search(string *A, int N, string x)
{
for(int i = 0; i < N; i++ )
if( match(A[i], x) )
return i;
return N;
}
If you apply the search algorithm in a different scenario:
string with whatever type you have in mind
match function with
whatever code suits your purpose.
~/$ ./prog Name (or quit): John John Adams John Tyler Name (or quit): James James Madison James Monroe Name (or quit): Steve Name (or quit): George George Washington Name (or quit): quitHere's a solution