Lab 08: loops

Jan 30, 2024  β”‚  m. Jan 29, 2024 by Charlotte Curtis

Objectives

Setup

Before starting on this lab, we need to fix the git configuration! Follow the instructions here if you haven’t done so already.

Update your labs repo by cding into it and running git pull. Then, cd into the loops directory to begin the lab.

Basic Exercises

  1. Design and write a C++ function for a loop which reads 10 integers, and returns the largest. Use a while loop. Call it from main and print the result.

    Hint: You will need to add the function declaration to loops.h and the function definition to loops.cpp. I know I’ve done this for you in the past, but it’s a good idea to get used to doing it yourself.

  2. Redesign your solution to problem 1 so that the loop reads all of an unspecified number, of integers in the input stream (a file), and writes the largest. Use input redirection to pass your inputs to the program, e.g.:

    $ ./ex1 < test_inputs.txt
    

    Hint: you will need the cin.eof() function as a sentinal value.

    Note: To indicate “end of file” from the keyboard, you can type “Enter” followed by “Ctrl-D”.

  1. Implement the function display_odds which takes integers min and max as parameters, and displays all odd integers between these bounds (inclusive). Use a while loop for this solution.

    A sample run of the program might look something like (with user input read from main and passed to display_odds):

     enter lower bound: 8
     enter upper bound: 15
     9
     11
     13
     15
    
     

    This is the function with the associated test. To run the test, cd up to your main labs directory and run make lab=loops. You may need to run make clean first if it just says it’s all up to date.

    Note: the test is rather fragile, and is expecting the function output to only consist of each number followed by a line break, including a new line after the final number. If there is other output, including whitespace, the test will fail.

  1. Re-write the previous solution using the for syntax. Which do you prefer? Your final solution can be submitted with whichever loop you prefer.

  2. Implement a function that takes a specific letter as a parameter, then reads a sequence of letters terminated by a period. It should count and return the number of times the letter appears in the sequence. A sample run of the program might look something like:

     enter letter: a
     enter a sequence of letters, terminated by a period: i am slowly going crazy.
    
     count of 'a' occurences = 2
     

    Note that the prompt to enter a letter should be done in main, not in count_letters, while the prompt and read for the sentence should be done in count_letters. The final output should also be done in main.

  3. Write a function that displays truth tables for the && and || operators. Use nested loops and an integer counter for the two arguments. (Yes, this could easily be hardcoded, but where’s the fun in that?) Sample run:

    Truth table for &&:
       0  1
    0  0  0
    1  0  1
    
    Truth table for ||:
       0  1
    0  0  1
    1  1  1
    

    Note: the first row of output can be hard-coded

Extra challenges

  1. Design and write a complete C++ program that prompts the user for an odd numbered base width of an isosceles triangle, then prints a triangle with that base width. If the input is negative or even, the program should terminate with an appropriate error message. For example:

     Enter positive and odd base width: 5
    
       *  
      ***
     *****
     
  2. Design and write a C++ program that reads in an integer and does the following:

    1. Starting from 1, prints out every number up to and including the integer, except:
    2. If the number is divisible by 3, print “Fizz” instead of the number
    3. If the number is divisible by 5, print “Buzz” instead of the number
    4. If the number is divisible by both 3 and 5, print “FizzBuzz” instead of the number

    For example:

     Enter an integer: 17
     1
     2
     Fizz
     4
     Buzz
     Fizz
     7
     8
     Fizz
     Buzz
     11
     Fizz
     13
     14
     FizzBuzz
     16
     17
     

Assessment

If you haven’t run the steps to fix your labs configuration, do it now before you push anything. Follow the instructions here .

When you are satisfied with your display_odds function, add and commit your changes to your labs repo, then git push to submit. Your instructor will be building and running the exact same tests to determine whether you passed the lab.

There’s been some confusion over which files need to be committed, and the short answer is anything that you’ve changed should be committed. An easy way to do this is to add all your modified files to the staging area with:

$ git add .

where the . is a shortcut for “current directory”.

Then commit and push as usual.



Previous: Lab 07: Decisions
Next: Lab 09: Arrays