Quiz 1 preparation material

Quiz 1 will be 50 minutes long, handwritten, and conducted in the first half of the weekly lab session.

The quiz will be closed book, closed notes, and no electronics permitted, but students will be permitted one double-sided 8.5x11" 'cheat sheet'. (These do not need to be handwritten.)

Some questions on the quiz will ask you to write short explanations/descriptions of different aspects of C++, some will ask you to write short C++ code segments to achieve specific results, some will ask you to show and/or explain the results of short C++ code segments, and at least one will ask you to write an entire small C++ program to achieve a specific result.

The topics and suggested preparation activities for the quiz are listed below.

The best way to practice coding in general is to write and debug as many small programs as you have time for. The best way to practice for hand-written quizzes and exams is to try writing small programs on paper then take your answers and type them in/debug them to see where the bugs are.

Writing/debugging your own practice programs
When you're writing your own practice programs from scratch,
   you won't have the 'make whatever' option available.
You can still follow the edit/compile/execute cycle,
   but by using a g++ command for the compile part.

Suppose you decide you want to create a C++ file named practice.cpp
   and a corresponding executable named practicex.

The edit/compile/execute cycle would look like:
   pico practice.cpp   (or pluma practice.cpp &)
   g++ -Wall -Wextra practice.cpp -o practicex
   ./practicex

Note that pico/pluma will create a new empty file for practice.cpp
   if you don't already have one.
That g++ command is the style of command that make ordinarily runs
   for you in our lab exercises.

The edit/compile/execute cycle and the basic structure of a C++ program
Be sure you are familiar with the three core steps in developing a program and how we're following them in labs. This should be close to second nature by this point if you've completed lab1 and made good progress on lab2. Be sure you know the syntax for including libraries (e.g. iostream, iomanip, cmath, string) and creating a main routine. (Other specific aspects of C++ are covered separately below.)

Input and output using cin, cout, endl, setw, setprecision
Know the correct syntax for the use of cin, cout, endl, setw, and setprecision, and be able to use them to achieve specific goals.

A typical sample question might be something like

Variables and our key data types (so far)
Be able to declare and use variables of type int, float, and string, and know the differences between related data types such as short/int/long, float/double.

You will certainly be asked to declare/use variables of the various types as parts of other questions, and may be asked to provide an intuitive explanation of variables in general and/or of the different data types.

Computation and assignment
Be able to perform specific requested computations using C++ constants, variables, and literal values, and to store the results in a specified variable.

Understand the basic math operations, +, -, *, /, and %, and how they behave on integer arguments, floating point arguments, and mixes of the two.

Be able to understand the results of sequences of calculations and assignments, and show/explain those results.

Calling functions and using/storing their returned values
Be able to write code that calls the math functions we have used most frequently (sqrt, pow, floor) and uses the returned value appropriately (e.g. assigns the value to a variable or uses the value within a larger computation).

The use of constants
Be able to declare and initialize constants of the various types (int, float, string in particular) and how to use them within the program. Be able to explain the purpose of constants in improving code maintainability.

The importance of code standards: layouts, comments, naming
Be able to explain the significance of the code standards we have introduced so far in 159: layout/indentation, the appropriate use of comments, and the choice of meaningful variable/constant names. Be able to point out where some of those standards have been violated if given a section of sample code, and suggest ways that code could be improved.


A set of practice/warmup exercises

Try each of the exercises below on paper first without any reference material, and make note of each C++ feature/syntax rule you have to look up (these would be good things to put on your cheat sheet!).

For each of them you'll try it on paper first, then type it into an actual program and compile/debug it until it does what you intended.

Part 1: try writing (on paper) a small standalone program that just prints "Practicing!" when it runs. If you get stuck, this is just like the program we began with in lab 1 but with a different cout message.

To check your code, follow the instructions at the top of this page for editing/compiling/running a program of your own, type in the code you wrote on paper, and keep debugging until it works.

Part 2: like part 1, try this on paper first and then check it by actually following the edit/compile/run cycle.
This time in the main routine:

Part 3: now suppose we already have values for ints X, Y, and Z.
(i) write three cout statements that use setw with a column width of 9 to display X, Y, and Z right-aligned on three separate lines (like we used setw in lab2).
(ii) supposing X, Y, and Z were actually floats: write code that tells cout we want to use fixed precision (setiosflags and setprecision) with 3 digits of precision after the decimal place.

Now to check your work edit the program from lab 2: change the int to float for X,Y,Z and add your answers for (i) and (ii). Debug until you get it working.

Part 4: now suppose we had four floats, W, X, Y, and Z, and that X and Y already had values in them. Use pow to compute XY and store the result in W, then use sqrt to compute the square root of W, and store the result in Z.

As usual, modify your practice code to add the declaration of W and the new calculations, and cout them so you can see the results. Debug until it works correctly.

Part 5: finally, try writing (on paper again), a complete program that does all of the following:

Check your program by writing the program and debugging it.