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, but have three weeks (Nov 4/6 to Nov 25/27, including the study week) to do so.

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 in general will be reading a sequence of grades and finding the average value of a subset of those (ignoring some of the lowest and some of the highest grades entered).

The specifics are as follows:

IMPORTANT NOTE:

Sample run

Below we show two sample runs of lab5x: in the first one there are no user input errors (just to show a clean run, uncluttered with error messages), while in the second run a variety of user input errors are shown (with the desired corresponding program behaviour).
(Of course, the errors I'll use in actual testing/grading will be in different spots with different values and with different numbers of errors.)

User input is shown in bold italics just for clarity.

Welcome to AAGGH (Adjusted Average Grade Getting Helper)
 - the program that allows you to compute a truncated average
   of grades from an assignment or course

Please identify how many grades you'll be entering
   (an integer in the range 3..500)
5

Please provide grade entry 1 (a number in the range 0..100):
27.5

Please provide grade entry 2 (a number in the range 0..100):
0.5

Please provide grade entry 4 (a number in the range 0..100):
100

Please provide grade entry 5 (a number in the range 0..100):
93.01

Please provide grade entry 3 (a number in the range 0..100):
77.25

Please identify how many of the top and bottom grades you wish to ignore
(a number in the range 0..2):
1

The sorted grades (other than the ignored ones) are:
27.5
77.25
93.01

The average of those grades is:
65.92

Thank you for using AAGGH!

Welcome to AAGGH (Adjusted Average Grade Getting Helper) - the program that allows you to compute a truncated average of grades from an assignment or course Please identify how many grades you'll be entering (an integer in the range 3..500) 2 Sorry, that is not an integer in the range 3..500, please try again: foo Sorry, that is not an integer in the range 3..500, please try again: 530 Sorry, that is not an integer in the range 3..500, please try again: 4 Please provide grade entry 1 (a number in the range 0..100): 100.1 Sorry, that is not a number in the range 0..100, please try again: -34 Sorry, that is not a number in the range 0..100, please try again: yuck Sorry, that is not a number in the range 0..100, please try again: 84.123 Please provide grade entry 2 (a number in the range 0..100): why Sorry, that is not a number in the range 0..100, please try again: 0 Please provide grade entry 3 (a number in the range 0..100): 100 Please provide grade entry 4 (a number in the range 0..100): -0.001 Sorry, that is not a number in the range 0..100, please try again: 17.7 Please identify how many of the top and bottom grades you wish to ignore (a number in the range 0..2): 3 Sorry, that is not an integer in the range 3..500, please try again: 0 The sorted grades (other than the ignored ones) are: 0 17.7 84.123 100 The average of those grades is: 50.45575 Thank you for using AAGGH!

Design

It is expected that you'll make extensive use of functions in your design, and your program will be graded on it's correctness, it's design, and how well it adheres to the course code standards.

We'll use the lab time after quiz 4 (Nov. 17/19) to discuss designs if you're still struggling with it after the reading break.