Schedule‎ > ‎

Lab 15: Selection Sort, Pointers, break and continue


  • Prepare for the midterm.
  • Help other students study for the midterm.

^ top

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You are expected to work either by yourself.
  • You may NOT give a copy of your code to any other student nor post it online.
  • You must breadboard the circuits yourself.
  • You may NOT look at another student's code until you complete and submit this assignment.
  • You may get help from people if you get stuck, but only if they show you like examples and not show or tell you the exact code to type.

Part 1  Selection Sort  (SelectionSort.ino)  (7 pts)

Selection Sort is another simple sorting algorithm.  You search through the array for the minumum value and put it at the front of the array (swap it with whatever is already at the front of the array).  Then you search for the next minimum value and put it in the next spot in the array.  Continue until you've reached the end of the array.

Watch this animation of Selection Sort:

Here is some code to get you started:  (it is fine for you to implement it a different way if you prefer)
#include <ArduinoSTL.h>
using namespace std;

void swap(int &x, int &y)  //using reference variables
  int temp;
  temp = x;
  x = y;
  y = temp;

void print(int myArray[], int size)  {
  for (int i = 0; i < size; i++)
    cout << myArray[i] << " ";
  cout << endl;

void setup() {
  const int NUM_INTS = 10;
  int a[NUM_INTS] = {99, 22, 3, 0, 56, 101, 2, 23, 27, 22};
  // Print unsorted results
  cout << "Unsorted: ";
  print(a, NUM_INTS);
  int minIndex = 0;

  // Selection Sort
  for (int i = 0; i < (NUM_INTS - 1); i++)  {
    minIndex = i;
    // Find the index of the minimum element
    for (int j = i + 1; j < NUM_INTS; j++)  {
         /*ADD YOUR CODE HERE TO FIND THE INDEX OF THE MINIMUM VALUE AND PUT IT                      INTO minIndex  */
     }  //end inner for loop
   }  //end outer for loop

  // Print sorted results
  cout << "Sorted:  ";
  print(a, NUM_INTS);
}   //end function setup()

void loop()


Copy your results into your header comment of file SelectionSort.ino

Part 2  Pointers (pointers.ino)  (7 pts)

In setup() declare int variables x and y and int* pointer variables p and q. 

Set x to 5, y to 9, p to the address of x, and q to the address of y.  Then print the following information:

(1) The address of x and the value of x.

(2) The value of p and the value of *p.

(3) The address of y and the value of y.

(4) The value of q and the value of *q.

(5) The address of p (not its contents!).

(6) The address of q (not its contents!).

Your output should look like this:

The address of x is __  and the value of x is ___.

The value of p is ___  and the value of *p is ___.

The address of y is ___ and the value of y is ___.

The value of q is ___  and the value of *q is ____.

The address of p (not its contents!) is ___.

The address of q (not its contents!) is ___.

Copy your output into the header comment of your pointer.ino file.

^ top

^ top

Part 3:  break and continue (break_continue.ino) (7 pts)

1.  Start a new file and save it as break_continue.ino

2.   Design and implement a loop that uses break in it.  You can see an example of this in 10A, but you may not use that same loop in your program.

3.  Design and implement a loop that uses continue in it.  You can see an example of this in 10A, but you many not use that same loop in your program.

4.  Submit your break_continue.ino program with the other parts of this lab.

Grading Criteria (21 pts)

Note that function headers must have the @param and @return comments filled in.

Each Part:  7 points  (Header 1pt, Function Headers 1pt, Compiles 1 pts, Works as Specified 4 pts)

Part 1:  7 pts

Part 2:  7 pts

Part 3:  7 pts

^ top^ top