Art Show | Course Home | Homework | Labs | Programming | Schedule & Lecture Notes | Submit

Saint Louis University

Computer Science P125
Introduction to Computer Science

Michael Goldwasser

Spring 2005

Dept. of Math & Computer Science

Lab 12

Sorting an Array


Overview

Our goal for this lab is to write a program which reads in a list of words and alphabetizes them. There are many different algorithms for sorting data. For this lab, we ask that you use a method known as selection sort, described as follows.

Select sort is based upon making repeated passes through the data as follows. During the first past, the goal is to select which word should be first in the final alphabetized result. Recall that the < operator can be used to compare two strings, with the "lesser" being that which comes first in alphabetical order.

Once this 'smallest' word has been located, that word should switch places with the word which was originally in the first entry of the array. In subsequent passes, the goal is to located the next "smallest" word out of the remaining (unsorted) words and to place it into the next spot of the array. After doing N-1 such passes, the data should be fully sorted.

The user interface for this program should involve first asking the user how many words will be given. Then the user should enter each of the words. Then the program should sort the words and display the list of words in alphabetical order

To get you going, we offer the following code which reads the data from the user, but does not sort or print the data.

#include <iostream>
#include <string>
using namespace std;

int main() {
  int numWords;
  cout << "How many words are there? ";
  cin  >> numWords;

  string word[numWords];   // declare array to hold data

  for (int i=0; i<numWords; i++) {
    cin >> word[i];   // read in one such word
  }

  // now is the time to sort the list and then to print the list

}  


Recommended Progress


When you are finished please submit your source code.


Michael Goldwasser
CS-P125, Spring 2005
Last modified: Saturday, 16 April 2005
Art Show | Course Home | Homework | Labs | Programming | Schedule & Lecture Notes | Submit