Program — Simple looping and testing
This programming assignment introduces you to C++: variables. computation, and control structures.
The C++ compiler available for use on your class account is called g++. It comes with a debugger gdb, described in the document "GDB Manual"; gdb also has an extensive online help facility. Names of source files intended for use with g++ should end with one of the following: ".C", ".cpp", ".cpp", "c++", or ".cxx". (An obscure message from the ld program results if you try to compile a file whose name has a different ending.) To compile the program in the file prog1.cpp, give the following command to the UNIX shell:
g++ -g -Wall prog1.cppThis creates an executable file called a.out, which you run by giving
a.outas a command to the UNIX shell. A command that produces an executable file called prog1 is
g++ -g -Wall -o prog1 prog1.cppYour CS 9F computer account may be set up to supply the -g -Wall automatically. To find out, give the alias command to the UNIX shell. If you see a line
g++ g++ -g -Wall -I somethingin the resulting output, you may merely give the command
g++ prog1.cppto compile the program in prog1.cpp. (In case you're wondering, -Wall means "give all relevant warnings" and -g stores information in the executable file for use by the debugger.)
More information about running these programs may be found in the excerpted versions of the manual pages for g++ and gdb that appear later in this document. To produce output suitable for evaluation by a tutor, use the script program. If you haven't used it already, it's described in the document "Before you begin ..." available for downloading at the self-paced course web site.
Readings
A Computer Science Tapestry, chapters 1 through 5 except sections 2.4-2.6, 3.4, 4.5, and 5.4 (these cover functions and classes, which appear in the next programming assignment).
C++ Program Design, chapters 1 through 4. For some reason they use #include <string> where it's not necessary. Also, they present an example in section 3.6 a use of the increment operator within a larger expression; you're not allowed to do this in CS 9F.
Computing Fundamentals with C++, chapters 1, 2, 3.4, 3.5, 7, and 8. (Intervening chapters cover functions and classes, which appear in the next programming assignment.)
Students who already know C should read the section "C constructs to avoid" in this study guide, which describes "C-isms" that you aren't allowed to use in CS 9F. The rest of you should skim this section just to make sure you're not doing anything you shouldn't.
The sections of this document titled "Style guidelines" and "Comparing C++ and non-C-based programming languages".
If you are unfamiliar with the EECS instructional systems, you should also download and read the document "Before you begin ..." available at the self-paced course web site.
Related quizzes
CS 9F programming assignment 1
Simple looping and testingProblem
Write a C++ program that simulates an adding machine. Input to the program will be integers, submitted one per line. Input should be handled as follows: a nonzero value should be added into a subtotal; a zero value should cause the subtotal to be printed and reset to zero; two consecutive zeroes should cause the total of all values input to be printed and the program to be terminated.
Here's an example of how the program should work.
user input program's output 5 6 0 Subtotal 11 -3 -2 5 0 Subtotal 0 13 -13 8 0 Subtotal 8 0 Total 19You may but need not use functions. However, if your main function is more than 24 lines long, you are required to split the program into more than one function to satisfy CS 9F style standards. Test your program on input that consists only of two zeroes—the output should be a 0 subtotal and a 0 total—as well as on the example above.
Checklist
- Correctly working code.
- Test cases as specified.
- Printed listings of program text, tests, and test results.
- Adherence to CS 9F style standards:
- appropriate use of indenting and white space;
- avoidance of "forbidden C++";
- variable names that reflect their use;
- informative comments;
- no function more than 24 lines long.
- Clean case analysis and simple loop structuring.