CSCI 160 Lab exercise 4, F21N01/N02

This lab requires more design and more functions than your previous labs, plus the use of arrays. With the study break you have three weeks to complete the lab, and I strongly recommend you start early!

In lab exercise 4 we'll be working on loops and arrays.

As with previous labs, there are several key syntax elements you'll need to be familiar with. The core syntax elements can be found here.

The process for obtaining, working on, and submitting labex3 is the same as for past labs (except of course it's labex4 rather than labex0/1/2/3).

Refer back to the lab1 part 1 instructions if you forget the specifics of different steps.


labex4.cpp objectives

In this lab exercise we'll continue using our past work with functions and parameters, if statements, loops, etc, but this time we'll also be adding the use of arrays for storing data.

You may use either the iostream library or the cstdio library for input/output.

The applied objective for this exercise is to create and submit a complete and correct C++ program, adhering the the course code standards, with the following behaviour and restrictions:

  1. The general intent of the program is to allow the user to store and search a set of up to 20 numbers, adding new numbers over time.

  2. The program begins by displaying a brief command summary for the user.

    It then repeatedly prompts the user to enter a character corresponding to an available command: either 'H' for help, 'Q' to quit, 'A' to add a new value to the data set, 'P' to print the currently stored values, or 'C' to count how often a value appears in the data set.

    The user can enter the command character in upper or lower case (e.g. 'a' and 'A' are both valid for adding a new value).

    Based on the character the user enters, the program does one of the following:

    In cases where the user is asked to enter a number (e.g. in response to 'A' or 'C'), the user should be repeatedly prompted until they do enter a number. There is no restriction on the value of the number itself (i.e. positive numbers, negative numbers, or 0 are all to be treated as valid).

    After completing processing of the command, the program again prompts the user to enter a command (unless they chose to quit of course).

    A sample run of the program is shown below.


Sample run

The table below shows a sample run of the program. Note that you do not have to use the exact same prompts or error messages as those shown below, but be sure your prompts and messages are clear and informative.

Just to make this display a little clearer, I've highlighted what the user entered in bold italics. (That's not something you could/would do in your program.)

Welcome to the Data Store!

You will repeatedly be given the opportunity to add numeric values to storage,
print the currently stored values, search the storage for specific values,
get help, or quit the program.

We'll get started by displaying the available commands:

Available commands:
  H - print this help menu
  P - print the values currently in storage
  A - add a new number to storage
      (you'll be given another prompt to enter your number)
  C - count how often a given number appears in storage
      (you'll be given another prompt to enter your number)
  Q - quit the program

Please enter a command [ H P A C or Q ]: P
There is currently no data stored

Please enter a command [ H P A C or Q ]: A
Please enter the number: 27
27 added to the data store

Please enter a command [ H P A C or Q ]: A
Please enter the number: -1.2
-1.2 added to the data store

Please enter a command [ H P A C or Q ]: p
Currently stored:
27
-1.2

Please enter a command [ H P A C or Q ]: X
That is not a valid command choice

Please enter a command [ H P A C or Q ]: a
Please enter the number: foo
That was not a number
Please enter the number: 27
27 added to the data store

Please enter a command [ H P A C or Q ]: P
Currently stored:
27
-1.2
27

Please enter a command [ H P A C or Q ]: C
Please enter the number: 27
27 appears 2 times in the data store

Please enter a command [ H P A C or Q ]: H
Available commands:
  H - print this help menu
  P - print the values currently in storage
  A - add a new number to storage
      (you'll be given another prompt to enter your number)
  C - count how often a given number appears in storage
      (you'll be given another prompt to enter your number)
  Q - quit the program

Please enter a command [ H P A C or Q ]: Q
Shutting down, bye!


Design thoughts

This lab is meant to provide practice with arrays, so an array must be used to hold the values entered. This will need to be declared in the main routine, along with a variable to hold the number of values currently stored (initially 0).

Otherwise the design is left largely up to you, but you are expected to make appropriate use of functions (and hence parameters) in developing your solution.

It would seem reasonable to have functions for (at least) the following:


Deadlines and late penalties

Submission deadlines and late penalties for all lab exercises are provided on the main labs page.

Reminders

Don't forget to do your "make submit" when finished, and don't forget about the tools/tips from week 2 of labex1 (ssh'ing to the pups, getting your lab feedback, and use of X2Go).

Don't forget to follow the code standards, be sure to check your feedback from previous labs to see if there were concerns with your previous work with respect to the code standards.

Suggested (iterative) development process

As with previous labs, I strongly recommend coding and testing small parts of the program at a time. Get basic versions of each function working first (without error checking, loops, or recursion) then when those appear to be working start adding the error checking (still without getting the user to try again), then when that appears to be working add the repetition.