Reinforcement

This lecture is to be used to reinforce the previous lecture on sorting and searching. We'll go over the homework very thoroughly, and try and really internalize the following:

Problems

  1. Write a program that reads in a list of 3D points (x,y,z) and prints them out in increasing distance from the origin. The points should be represented as arrays of length 3. Here's my solution. Below is a sample run of the program:

    How many points? 3
    Enter points (x,y,z): (1,0,1) (1,0,-1.2) (-1,1,-1)
    (1,0,1) (1,0,-1.2) (-1,1,-1) 
    • Try the same thing, but sort by x-values, breaking ties by y-values, breaking those ties by z-values.
    • Try the same thing, but sort in increasing order of distance from a point given by the user.
  2. Write a program that reads in a list of non-negative integers from the user and sorts them by their last digit. Numbers with the same last digit should appear smallest to largest. So, 678 32 67 102 7 18 would appear as 32 102 7 67 18 678.
    • Next, try allowing the user to enter a modulus M along with the numbers, and sort the numbers according to their mod M value - smaller mod M values first. Numbers with the same mod M values should appear smallest to largest.
  3. Write a program that reads in a list of 3D points (x,y,z), then reads in a point p and a distance d from the user, and then prints a point from the list that lies within distance d of point p ... if it exists.
  4. Write a program that reads in a list of 2D points (x,y), then reads in a point p prints a point from the list that lies in the same quadrant as point p ... if it exists.