2020 Lab-1

Elements of this lab...
  Create a menu program
  Various programming challenges
Before you begin...
Your 2020 Odin directory must be setup properly.
procedure is here

Lab instructions
Start here.

Create program file on Odin: 2020/1/lab1.cpp 

We will start this program together in class.
Get ready to follow along on the big-screen.
We will create most of the menu, and we will complete most of one menu item.
You will choose at least one menu item to do yourself during the lab.
This lab may become homework. That will be determined after the lab.

Sample of program opening screen...

Lab-1 Menu of options ---------------------- 1. Prime number digit 2. Summation - 1000 times 3. Coin flip 4. Hash table 5. Look for CSUB 6. Linked-list operations q. Quit the program

1. Prime number digit
Prompt the user to enter a single digit.
Display the first 10 prime numbers that end in the user's digit.

Start checking for prime numbers at 3, and continue. Check the right-most digit
of each prime number generated. List all prime numbers that meet the criteria,
until 10 prime numbers have been listed.

Allow for the user entering a digit that will yield no prime numbers,
and display an apporpriate message.

format of output:
List prime numbers whose rightmost digit is given by user --------------------------------------------------------- Please enter a single digit: 3 Primes: 3 13 23 43 ... Done.
Do not display the "..." More numbers go there.

2. Summation 1000 times
Ask the user to enter a positive number. The program will calculate the sum of
all whole numbers from 1 up to the number entered.

Calculate the sum 1000 times using a do-loop nested inside of a while-loop
nested inside of a for-loop. Each loop will iterate 10 times.

Pseudo-code...

   ask user for input
   declare a sum variable
   for-loop 10 iterations
       while-loop 10 iterations 
           do-loop 10 iterations
               calculate the sum
          end do
       end while
   end for
   display some results

note: Do not use a closed-form summation formula for this. Use brute force.

format of output:
Summation 1000 times -------------------- Summation to what number? 6 calculating now... calculation complete. The sum is: 21
Tips for summation loops... 1. Start by writing the summation from 1 to n. It can be a for-loop. 2. Write a do-while loop around the summation to make it happen 10 times. 3. Write a while-loop around those loops to make them happen 10 times. 4. Write a for-loop around the whole thing that iterates 10 times.

3. Coin flip
Write a function named coinToss. The function will accept an argument indicating
the number of coin tosses to execute.

Function coinToss will contain static integer variables named heads and tails
that keep a count of total heads or tails that are tossed.

Display total heads and tails tossed so far, at the end of each function call.

Get user input outside of your coinToss function.

From the menu...
Let the user enter the number of coin tosses to make, then call the function.
Let the user continue tossing coins until the number zero is entered.

format of output:
How many coin tosses: 4 heads: 1 tails: 3 How many coin tosses: 120 heads: 76 tails: 48 How many coin tosses: 0 Game over. Thanks.

4. Hash table
Declare an integer array that will hold at least 100 elements. Initialize all
elements to zero. Use a loop to generate random numbers in the range 20 to 80.
Use the random number as an index into the array, and increment the value at
that index using the pre-increment operator.

When the value of any element of the array exceeds 20, stop and display the
index of that element. Also display the index of all array elements that
contain a value of 1, 2, 3, 4, or 5.

format of output:
Hash table ---------- Generating randoms now... Finished. Indexes asked for: 12 25 67 ...
The numbers above show formatting only. Do not display the "..." More numbers go there, possibly.

5. Look for CSUB
Completed in class together.

Declare a character array large enough to hold a C-string of string-length 10.
Populate the C-string with random letters from 'A' to 'Z'.

After each population, use the strcmp function to see if the first four
letters of the C-string are "CSUB". If so, stop the program and display the
number of tries it took to find it.

Write the program as stated please.

format of output:
Find CSUB trying now... Found it in 21000 tries. Here is the string: CSUBABCDEF

6. Linked-list
Completed in class together.
Define a struct named Node. The struct has two member elements...

  1. an integer element named data
  2. a pointer of type Node named "next"

Declare 3 Node elements, with data values 1, 2, and 3.

Set the "next" pointer of the first Node to the address of the second Node,
and the "next" pointer of the second Node to the address of the third Node.

Set the "next" pointer of the third Node to NULL.

Use a loop to start at the first Node, display its data value, then move to
the next Node using the "next" pointer. The loop will end when a "next" pointer
contains NULL.

We did not do this in class.
Hint:
  Declare all 3 nodes as pointers and allocate them using the new operator.
Example:
  Node *n1 = new Node;


format of output:
Linked-list Declaring nodes now. Stepping through linked list now. data values are: 1 2 3 Done.