CSCI 159 Lab 5 exercises

With lab 5 we've again got a two-week lab (plus the study break).

There are again two halves, a warm-up exercise working with arrays, searching and sorting, and null-terminated character arrays (warm-up exercise done in basic.cpp) and a design/implementation exercise (done in lab5.cpp):

Here is the collection of new C++ syntax elements we'll be using for lab5.


Follow our usual setup process

  1. Log in, open a browser, go to the lab5 page, open a terminal window:
    (see lab 2 if you need a refresher on any of the steps)

  2. get lab 5:
    make -f make159 csci159/lab5

  3. Go into your lab5 directory and begin the edit/compile/test/submit routine: As with previous labs, you'll see that the two .cpp files are nearly empty to start with.


First half: arrays, searching and sorting, and null-terminated character arrays (to be done in basic.cpp)

In the first half of the lab, our usual 'warm-up exercise' practicing with our new language features, we'll be:

As usual for our warm-up exercises, there is a set of specific functions we'll set up and call from main:

#include <iostream>
#include <cstring>
using std::cout;
using std::cerr;
using std::cin;
using std::endl;

// exchange the values in the two parameters
void swap(char &c1, char &c2);

// sort the letters in the array by their ascii value (i.e. comparing using <)
// (sorts using a bubblesort)
void sortLetters(char text[]);

// use a linear search to find the first appearance of the target char in the text
// and return its position (returns -1 if the target is never found)
int linearSearch(char text[], char target);

// use a binary search to find an appearance of the target char in the text
// and return its position (returns -1 if the target is never found)
int binarySearch(char text[], char target);

// display the text and summarize the search results
void report(char text[], char target, int pos, bool wasItOrig);

I'm assuming folks' programming abilities are getting ever-stronger, so this time I'll simply provide an outline of what each function should be doing then let you choose an appropriate sequence to implement the functions.

Remember the syntax examples show how to use things like cin.getline to read in a line of text, strncpy to copy one character array into another, and strlen to figure out how many characters of text are currently stored in an array.

The function (and main) outlines are provided below.

int main()
{
   // declare a constant for the array size, say 50
   // and declare two character arrays of that size
   // (one for the original text and one for the sorted copy)

   // prompt the user to enter a line of text and read it into the array
   // (see the cin.getline example in the syntax examples)

   // copy the content into the second array
   // (see the strncpy example in the syntax examples)
   // then pass the second array to the sorting function to be sorted

   // declare a character variable to store the user's chosen search character,
   // ask them what character they wish to search for, and store it in the variable
   // (a simple cin >> works here)

   // declare two int variables to hold the location where the target character is found,
   // then call linearSearch on the original array and store the returned value in one of
   //    the int variables and call binary search on the sorted array (and store the
   //    returned value in your other int variable)

   // call the report function twice:
   //    once passing the original array, the user char, the found position from linear search,
   //        and true (indicating this is the original),
   //    once passing the sorted array, user char, found position from binary search, false
}

void sortLetters(char text[]) { // bubblesort will need to know how many characters we have stored, // we can get this using strlen(text) // I recommend using two for loops for the sorting, one nested inside the other // our outer loop needs to make N-1 passes for an array holding N chars // our inner loop can go from positions 1 to N-1 // at each position it compares the character in the current position // to the character before it, swapping them if they are out of order // (not doing anything if the order is ok) }
int linearSearch(char text[], char target) { // search each position from 0 to the end, // we can stop either at strlen(text) or when we reach a position in text // that contains the null terminator, '\0' // at each position check if the character there matches the target, // if so then we found it, return the current position // if we get out of the loop it means we never found the target value // so return -1 }
int binarySearch(char text[], char target) { // set up the bounds for the array section we're searching (initially the whole thing) // note that strlen(text) will tell you the position of the last relevant character // repeat as long as lower is still <= upper: // compute the midpoint between lower and upper // if the character at the midpoint is less than the target // then set lower to midpoint+1 // otherwise if the character at the midpoint is greater than the target // then change upper to midpoint-1 // otherwise we found it, return midpoint // after the loop return false (since getting there means we didn't find it) }
void report(char text[], char target, int pos, bool wasItOrig) { // display the text, telling the user which it was (original or sorted) // cout << text works fine for displaying null-terminated char array content // tell them if we found the target or not, // and what position we found it in (if it was found) }
void swap(char &c1, char &c2) { // our usual swap, just with chars this time instead of ints or floats }

Sample run

Below we show two sample runs of basicx, once where the user's selected target character isn't in their text and once where it is. (User input is shown in bold italics just for clarity.)

 
Please enter a line of text (max 50 chars): this may be the text I want, maybe!
Enter a (non-whitespace) letter you would like to search for: I
 
The original content was: 'this may be the text I want, maybe!' 
We found 'I' in position 21 in the original text 
 
 
The sorted content is: '       !,Iaaabbeeeehhimmnstttttwxyy' 
We found 'I' in position 9 in the sorted text 
 

Please enter a line of text (max 50 chars): blah blah blah Enter a (non-whitespace) letter you would like to search for: k The original content was: 'blah blah blah' We did not find 'k' in the original text The sorted content is: ' aaabbbhhhlll' We did not find 'k' in the sorted text


Second half: design and implementation problem (to be done in lab5.cpp)

You'll be designing and implementing this program yourself.

The program will focus on the use of arrays and sorting, but will also rely heavily on loops. (Recursion is not prohibited, but not recommended.)

The program is another extension of previous labs: this time we'll have an array of strings into which we'll be reading the container names and an array of floats into which we'll be reading the container sizes.

To accommodate our new arrays, we'll place a fixed upper limit on how many containers the user is permitted to select, using 50 as an upper bound (be sure to declare and use this as a constant).

As with lab4, the user will specify how many containers they wish to process and then go through and enter the name/size of each container in turn. (We will still be error checking their inputs as they go, getting them to re-enter invalid entries before moving on.)

Your main routine will thus likely consist of two for loops with a call to a sorting function in between:

Once all the data has been collected, we'll sort the containers by size from smallest to largest, but rearranging the names array in unison. (E.g. if we realize we need to swap positions i and j in the sizes array then we also need to swap those positions in the names array, to keep the names in synch with the correct container size.)

Once the arrays have been properly rearranged we will then go through and run our display function on each container, from smallest to largest.

Running our program would thus be just like lab4 (other than the upper limit on the number of containers), but the resulting how-many-of-x-fit output would show the containers in increasing order of size.

The run below shows an example with no errors in user input, but don't forget your error checking!

Welcome to HowManyFit! 
 
We estimate how many (small) marbles, ping pong balls, and tennis balls 
fit in a container whose size you specify. 
 
Please enter the number of containers you wish to process:  
3 
 
Please enter a name for the container (as a single word): firstbox 
Please enter the size of a(n) firstbox in litres (e.g. 4.6): 10 
 
Please enter a name for the container (as a single word): secondbox 
Please enter the size of a(n) secondbox in litres (e.g. 4.6): 4 
 
Please enter a name for the container (as a single word): thirdbox 
Please enter the size of a(n) thirdbox in litres (e.g. 4.6): 1.1 
 
A thirdbox of size 1.1 litres holds 
  77.2 to   94.4 small marbles 
  20.3 to   24.8 ping pong balls 
   3.7 to    4.5 tennis balls 
 
A secondbox of size 4.0 litres holds 
 280.8 to  343.2 small marbles 
  73.8 to   90.2 ping pong balls 
  13.3 to   16.3 tennis balls 
 
A firstbox of size 10.0 litres holds 
 702.0 to  858.0 small marbles 
 184.5 to  225.5 ping pong balls 
  33.3 to   40.7 tennis balls 
 
Thanks for using HowManyFit! 

As usual, remember to adhere to standards and do your make submit before the deadline.