161 Lab 2 exercises

Remember we'll hold quiz 1 in the first half of the lab.

See the lab1 notes if you need any reminders/refreshers on our use of linux and git.

See the main lab page for submission deadlines and late penalties.

It is assumed you are keeping up to date with the lectures and labs.

This lab gives you more practice and reponsibilities in the handling of the separate header and .cpp files, and practice with the handling of dynamically allocated arrays (using new and delete[]).

The sequence for obtaining the lab will be much the same as lab1, and the commands are briefly summaried below (see the lab1 discussion for explanations and any troubleshooting needed):

(EDIT: the lab2 repo is now ready to be forked and cloned)
ssh -x csci fork csci161/lab2 csci161/$USER/lab2
cd csci161
git clone csci:csci161/$USER/lab2
cd lab2
At this point you should be able to see files
README, application.cpp, application.h, batchtest, lab2.cpp, lab2.h, makefile, and a subdirectory named testfiles that has a single example file in it.

You can use "make" to build the lab2x executable (this time it actually builds the executable in a way that is compatible with the gdb debugger) and "./lab2x" to run the executable after successful compilation.

You can follow your normal edit-compile-test cycle, using make to rebuild the executables as needed.

When you wish to submit your work, the command sequence is as follows:
git add lab2.cpp
git commit -m "some relevant description of the current snapshot"
git push


An overview of the lab2 program
The program itself is meant to allow the user to enter, sort, update, and display a series of integer values, and to also take "slices" of those values: effectively making copies of chunks of the current set of values. The set of values and any slices will be stored in dynamically allocated arrays, providing a chance to practice with the use of new, delete[], and pointers.

The code is divided across two .cpp files and two .h files, providing a chance to practice with the use of #includes and seperate compilation through the given makefile.

The program begins by getting the user to pick how many data values they wish to store, creating an array of exactly that size, and getting values from the user to fill it.

Once that has been done, the program repeatedly prompts the user to pick what action to carry out next (the user enters a single character to indicate their command choice), continuing until they decide to quit.

The commands available to the user include:


Design of the lab2 program
The program content has been divided across two pairs of files:


Student requirements for the lab2 program
Now we finally get into the actual programming details for the student portion of lab2.

The student's task is to finish the implementation of the remaining five functions in lab2.cpp, following the function specifications as described above and in lab2.h.

The only changes you are making are to the functions in the lab2.cpp file, the applications.cpp, lab2.h, and applications.h files should not be altered.

While the exact wording of the display/error messages in your lab2.cpp functions do not have to exactly follow those in the example provided below, they should be equally informative.

All lab content is, of course, expected to follow the course standards. Note that in the sort, display, and fillNums functions you'll need to check the nums parameter to see if a nullptr has been passed. If nums is null then the function should display an error or warning message instead of its normally-planned action, as attempting to access the array content will crash otherwise.


Observations on applications.cpp
It is recommended that students study the functions provided in applications.cpp to ensure they understand what is being handled there. In particular, note that


Sample run
Below we show a lengthy sample run of the program.
For clarity, it highlights the user input during the run of the program in bold italics (this is not, of course, something you could/would do in your actual program).

How many values do you wish to store? (1..1000) 
7 
Enter the next data value (1..4294967295) 
10 
Enter the next data value (1..4294967295) 
3 
Enter the next data value (1..4294967295) 
17 
Enter the next data value (1..4294967295) 
2 
Enter the next data value (1..4294967295) 
9 
Enter the next data value (1..4294967295) 
4 
Enter the next data value (1..4294967295) 
8 
 
Enter your command choice [NPRSHQ, H for help]: h 
Welcome to the data sorter/slicer 
A program to store, sort, slice, and print sets of integer values 
 
The available commands are: 
   P to print either the stored integer values or the current slice 
   R to refill part of the stored integer values or the current slice 
   S to sort a portion of the stored integer values or the current slice 
   N for a new slice of the current integer values 
   H to display this menu 
   Q to quit the program 
 
Enter your command choice [NPRSHQ, H for help]: p 
Enter the index of the low array print position (0..6) 
0 
Enter the index of the high array print position (0..6) 
6 
value in position 0 is 10 
value in position 1 is 3 
value in position 2 is 17 
value in position 3 is 2 
value in position 4 is 9 
value in position 5 is 4 
value in position 6 is 8 
 
Enter your command choice [NPRSHQ, H for help]: p 
Enter the index of the low array print position (0..6) 
1 
Enter the index of the high array print position (1..6) 
5 
value in position 1 is 3 
value in position 2 is 17 
value in position 3 is 2 
value in position 4 is 9 
value in position 5 is 4 
 
Enter your command choice [NPRSHQ, H for help]: p 
Enter the index of the low array print position (0..6) 
4 
Enter the index of the high array print position (4..6) 
4 
value in position 4 is 9 
 
Enter your command choice [NPRSHQ, H for help]: n 
Creating new slice from array of size 7 
Enter the index of the low slice position (0..6) 
2 
Enter the index of the high slice position (2..6) 
5 
 
Enter your command choice [NPRSHQ, H for help]: p 
Enter S to work on the current array slice, 
   or any other character to work on the main values array 
s 
Enter the index of the low slice print position (0..3) 
0 
Enter the index of the high slice print position (0..3) 
3 
value in position 0 is 17 
value in position 1 is 2 
value in position 2 is 9 
value in position 3 is 4 
 
Enter your command choice [NPRSHQ, H for help]: s 
Enter S to work on the current array slice, 
   or any other character to work on the main values array 
a 
Enter the index of the low array sort position (0..6) 
1 
Enter the index of the high array sort position (1..6) 
6 
Sorting array content for positions 1..6 
 
Enter your command choice [NPRSHQ, H for help]: p 
Enter S to work on the current array slice, 
   or any other character to work on the main values array 
a 
Enter the index of the low array print position (0..6) 
0 
Enter the index of the high array print position (0..6) 
6 
value in position 0 is 10 
value in position 1 is 2 
value in position 2 is 3 
value in position 3 is 4 
value in position 4 is 8 
value in position 5 is 9 
value in position 6 is 17 
 
Enter your command choice [NPRSHQ, H for help]: p 
Enter S to work on the current array slice, 
   or any other character to work on the main values array 
s 
Enter the index of the low slice print position (0..3) 
0 
Enter the index of the high slice print position (0..3) 
3 
value in position 0 is 17 
value in position 1 is 2 
value in position 2 is 9 
value in position 3 is 4 
 
Enter your command choice [NPRSHQ, H for help]: s 
Enter S to work on the current array slice, 
   or any other character to work on the main values array 
s 
Enter the index of the low slice sort position (0..3) 
0 
Enter the index of the high slice sort position (0..3) 
3 
Sorting slice content for positions 0..3 
 
Enter your command choice [NPRSHQ, H for help]: p 
Enter S to work on the current array slice, 
   or any other character to work on the main values array 
s 
Enter the index of the low slice print position (0..3) 
0 
Enter the index of the high slice print position (0..3) 
3 
value in position 0 is 2 
value in position 1 is 4 
value in position 2 is 9 
value in position 3 is 17 
 
Enter your command choice [NPRSHQ, H for help]: p 
Enter S to work on the current array slice, 
   or any other character to work on the main values array 
a 
Enter the index of the low array print position (0..6) 
0 
Enter the index of the high array print position (0..6) 
6 
value in position 0 is 10 
value in position 1 is 2 
value in position 2 is 3 
value in position 3 is 4 
value in position 4 is 8 
value in position 5 is 9 
value in position 6 is 17 
 
Enter your command choice [NPRSHQ, H for help]: r 
Enter S to work on the current array slice, 
   or any other character to work on the main values array 
a 
Enter the index of the low array fill position (0..6) 
3 
Enter the index of the high array fill position (3..6) 
5 
Enter the next data value (1..4294967295) 
5 
Enter the next data value (1..4294967295) 
15 
Enter the next data value (1..4294967295) 
25 
 
Enter your command choice [NPRSHQ, H for help]: p 
Enter S to work on the current array slice, 
   or any other character to work on the main values array 
a 
Enter the index of the low array print position (0..6) 
0 
Enter the index of the high array print position (0..6) 
6 
value in position 0 is 10 
value in position 1 is 2 
value in position 2 is 3 
value in position 3 is 5 
value in position 4 is 15 
value in position 5 is 25 
value in position 6 is 17 
 
Enter your command choice [NPRSHQ, H for help]: r 
Enter S to work on the current array slice, 
   or any other character to work on the main values array 
s 
Enter the index of the low slice fill position (0..3) 
1 
Enter the index of the high slice fill position (1..3) 
2 
Enter the next data value (1..4294967295) 
13 
Enter the next data value (1..4294967295) 
14 
 
Enter your command choice [NPRSHQ, H for help]: p 
Enter S to work on the current array slice, 
   or any other character to work on the main values array 
s 
Enter the index of the low slice print position (0..3) 
0 
Enter the index of the high slice print position (0..3) 
3 
value in position 0 is 2 
value in position 1 is 13 
value in position 2 is 14 
value in position 3 is 17 
 
Enter your command choice [NPRSHQ, H for help]: n 
Creating new slice from array of size 7 
...deallocating old slice first... 
Enter the index of the low slice position (0..6) 
1 
Enter the index of the high slice position (1..6) 
3 
 
Enter your command choice [NPRSHQ, H for help]: p 
Enter S to work on the current array slice, 
   or any other character to work on the main values array 
s 
Enter the index of the low slice print position (0..2) 
0 
Enter the index of the high slice print position (0..2) 
2 
value in position 0 is 2 
value in position 1 is 3 
value in position 2 is 5 
 
Enter your command choice [NPRSHQ, H for help]: q 
Goodbye! 


Testing/test cases
Once your lab2x has successfully compiled, you can try running the example above through the batchtester using the command
bash batchtest

You are encouraged to add additional test files to the testfiles directory, and to edit the batchtest file to include them. This can be done by cutting/pasting the 3 lines at the bottom of the file, with calls/info for your newly created testfile, e.g.
echo "---my own new test---"
read -p "test 2: my own testfile, enter any character to run the test", cmd
./lab2x < testfiles/nameofmyowntestfile | less