Contents

Instructions

The purpose of this lab is to give you practice with the basics of programming in C++. You should complete both parts in C++.

(Back to top)

Part 1: Skill-by-skill

Implement each of the following inside of a file named lab13-part1.cpp. Make sure it compiles and does each step.

  1. a while loop that prints out every third number from 1 to 100 (so 1, 4, 7, 10, ...)
  2. a do while loop that behaves the same way
  3. a for loop that behaves the same way
  4. open a file called output.txt for writing and use a loop of your choice to write every number from 300 to 400 (inclusive) to it, then close the file
  5. open that file back up for reading and read in every number; use an if statement with the mod operator to output the even ones to the terminal screen, then close the file when you're done
  6. create a function called writeFile and move the code from Step 4 to that function; this function should not return anything or take any parameters; make sure to invoke it in main in the spot where #4 used to be
  7. similarly, create a function for Step 5 called readFile and move the code for Step 5 to that function; invoke the function where Step 5 was located in main
  8. create an array to hold 51 numbers; use a for loop to add all of the even numbers between 900 and 1000 (inclusive) to the array
  9. create a static array with a maximum capacity of 10000 (use a constant for the capacity); prompt the user to give the number of strings they would like the program to read in up to the maximum capacity; use a for loop to prompt the user that many times, each time asking for a string and storing it in the array at the next spot
  10. print the array back out
(Back to top)

Part 2: FizzBuzz

This problem is based on the children's division game called FizzBuzz, where children shout out numbers in order, except they say "fizz" if the number is divisible by 3, "buzz" if it's divisible by five, or "fizz buzz" if it's divisible by both 3 and 5. As far as programming goes, implementing solutions to problems of this type demonstrates an ability to use loops and branching in a manner that's more complicated than simply iterating over an interval. This is a relatively simple problem that every CSC160 student should feel comfortable implementing in a relatively short amount of time.

Complete the following in a file named lab13-part2.cpp.

For every number between 1 and 500, print a "|" when a number is divisible by 4 but not 6, "-" when the number is divisible by 6 but not 4, and "+" when it is divisible by both. Nothing should be output for numbers that aren't divisible by either. Think carefully about the ordering of the conditions and don't check conditions unnecessarily. You should end up with three branches, and only one requires a complex boolean expression. The entirety of the output should be on one line, e.g.,: "|-|+|-|+" is what would be printed for the range 1–24.

(Back to top)

Submission

Submit your .cpp files to here. You can view the rubric here.

(Back to top)