CSCI 159 Lab 5 exercises

With lab 5 we're back to two-week labs (plus the study break).
Note that since Dave was off sick during our labs Nov. 4th/6th he'll be available at the following times/locations for questions related to the labs/lecture content:

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 
#include 
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 be getting the user to pick a month of the year (as an integer 1-12 representing Jan-Dec) and then will get them to enter the number of lines of code they entered on each day of that month.

Once they have entered all the lines-of-code values, it will search their stored data and figure out which seven-day period involved the most code, and print both the total for that seven day period and the breakdown of how much code was written on each of those seven days.

Days of the month:

The program must use the correct number of days for the chosen month, but will always treat Feb. as 28 days (no leap year handling since it doesn't ask what year and you are not permitted to change it to add a question about what year - see the note below).

Months with 30 days: April, June, September, November
Months with 28 days: February
Months with 31 days: January, March, May, July, August, October, December

Output:

While your program is not required to precisely follow the sample output below, it should provide equivalent information in a clear and easy to follow format, including displaying the month names or abbreviations (rather than a number) after the user has completed their initial '1-12' selection of the month.

Error checking:

All the user input in this program is of integers in specific ranges, and your program is required to follow our usual 'try again' cycles until they provide valid values:

(The sample run below illustrates some cases where they enter invalid values.)

IMPORTANT NOTE:

Sample run

Below we show a sample run of lab5x, including a couple of example spots where the user entered invalid values. (Of course, the errors I'll use in testing will be in different spots with different values, and the programs will be tested with a variety of different months and code values.)

User input is shown in bold italics just for clarity.

 
Welcome to your coding nightmare analyzer! 
This program gets you to specify which month of coding to analyze, 
and how many lines of code you wrote on each day of the month. 
 
It then finds the seven-day stretch in which you wrote the most code, 
and reports on your coding stats for those seven days. 
 
Which month do you wish to analyze (1..12 where 1 is Jan, ..., 12 is Dec): -1
Sorry, that was not an integer in the range 1..12, please try again 
Which month do you wish to analyze (1..12 where 1 is Jan, ..., 12 is Dec): foo
Sorry, that was not an integer in the range 1..12, please try again 
Which month do you wish to analyze (1..12 where 1 is Jan, ..., 12 is Dec): 2
How many lines of code did you write on Feb. 1? 
93
How many lines of code did you write on Feb. 2? 
7
How many lines of code did you write on Feb. 3? 
18
How many lines of code did you write on Feb. 4? 
300
How many lines of code did you write on Feb. 5? 
foo
Sorry, this needs to be a non-negative integer, please try again 
How many lines of code did you write on Feb. 5? 
ick
Sorry, this needs to be a non-negative integer, please try again 
How many lines of code did you write on Feb. 5? 
-1
Sorry, this needs to be a non-negative integer, please try again 
How many lines of code did you write on Feb. 5? 
10
How many lines of code did you write on Feb. 6? 
100
How many lines of code did you write on Feb. 7? 
77
How many lines of code did you write on Feb. 8? 
92
How many lines of code did you write on Feb. 9? 
101
How many lines of code did you write on Feb. 10? 
83
How many lines of code did you write on Feb. 11? 
12
How many lines of code did you write on Feb. 12? 
100
How many lines of code did you write on Feb. 13? 
400
How many lines of code did you write on Feb. 14? 
70
How many lines of code did you write on Feb. 15? 
12
How many lines of code did you write on Feb. 16? 
0
How many lines of code did you write on Feb. 17? 
0
How many lines of code did you write on Feb. 18? 
18
How many lines of code did you write on Feb. 19? 
88
How many lines of code did you write on Feb. 20? 
77
How many lines of code did you write on Feb. 21? 
92
How many lines of code did you write on Feb. 22? 
12
How many lines of code did you write on Feb. 23? 
200
How many lines of code did you write on Feb. 24? 
101
How many lines of code did you write on Feb. 25? 
88
How many lines of code did you write on Feb. 26? 
14
How many lines of code did you write on Feb. 27? 
23
How many lines of code did you write on Feb. 28? 
10
 
Your busiest week started on Feb. 7 
That week you wrote: 
   77 lines of code on Feb. 7, 
   92 lines of code on Feb. 8, 
   101 lines of code on Feb. 9, 
   83 lines of code on Feb. 10, 
   12 lines of code on Feb. 11, 
   100 lines of code on Feb. 12, 
   400 lines of code on Feb. 13, 
For a grand total of 865 lines of code 
 

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. 18/20) to discuss designs if you're still struggling with it after the reading break.