Assignment 3

Back Up Next

Home
Assignment 1
Assignment 2
Assignment 3
Assignment 4
Assignment 5
Assignment 6
Assignment 7

 

Note: If you have problem printing and seeing the entire document on your print out, 
just open this document http://spot.pcc.edu/~wmorales/cs161/hw3.htm using MS Word program. Click file, open.. and type the URL above. The document will be opened in Word and you can format it for proper output. 

CS 161 Walter Morales
50 points

Due date announced in class, check email

Purpose: The purpose of this homework assignment is to become familiar with the programming environment provided by the Borland C++ compiler or MS Visual C++ .

Suppose you want to write a program that does simple addition, subtraction, multiplication and division for a seven-year-old child, The following program prompts her/him for two numbers. The program then waits for the user to type an answer. When she/he gives her/his answer, the program displays the correct result so she can compare the response.

The program below executes a simple addition problem. Your task is to implement the other three missing functions to subtract, multiply and divide.

The user will only input two numbers for each of the calculations and the output will be basically the same as you see for the addition.

You will separate each section with:

*** Math Practice DIVISION ***, etc.

At the end of the program, you will display a formatted table similar to the one below using the manipulators for output setw and setprecision. The box surrounding the output is made up of ---- and | on your keyboard.

Some variables will have to be declared as floats, please be aware of the problems that we can encounter when dividing int/int  int/float, and other unexpected results by mixing data types.

You will need to create several variables to hold the value for each number entered .

---------------------------------------------------------------------------
| num1 + num2=     | num3 - num4=     | num5 * num6=     | num7 / num8=   | |
| correct_ans      | correct_ans      | correct_ans      | correct_ans    | |
---------------------------------------------------------------------------

Where you see num1 + num2, num1 and num2 you will be replaced with the actual values for those variables entered by the user.

1) Start the C++ compiler (Borland or Turbo). If you already know Visual C++ you can use it instead. Note that if you use Borland you will have to follow the old standard of C++. Appendix D on our text book shows the differences.

2) Enter the attached program exactly as shown.

// Filename: assign2.CPP
// Your Name, class, term
// Compiler used (in this case was Borland)
// Program to help children with simple math.
// Prompt child for two values after printing
// a title message.

#include <iostream.h>
#include <iomanip.h>

main()
{

int num1_add; // put comments
int num2_add; // put comments
int ans_add; // put comments
int her_ans; // put comments

cout << "This program is a math drill program that will include" << endl;
cout << "Addition, Subtraction, Multiplication, Division" << endl;
cout << "You will enter two numbers and the number will be << endl;
cout << "evaluated. Please make sure not to type character or" << endl;
cout << "other symbols. Have fun!" << end;

cout << "*** Math Practice ADDITION ***\n\n\n";
cout << "What is the first number? " ;
cin >> num1_add;
cout << "What is the second number? ";
cin >> num2_add;

// Compute answer and give user a chance to
// answer it.

ans_add = num1_add + num2_add;

cout << "\nWhat do you think the answer is? ";
cin >> her_ans; 

// Prints answer after a blank line.
cout << "\n" << num1_ans << " plus " << num2_ans << " is "
    << ans_add << "\n\nHope you got it right!";

continue with the rest..........subtraction, multiplication, division

return 0;

}

3) Save the program in a file on you work disk (do this frequently and not just at the end of program entry process).

4) Compile the program (use the compile pull-down menu), then run it.

5) At this point you should try playing with various options provided in the pull-down menus. In particular try to set a few "watches" (Borland) and run the program in step-over mode (Borland) or use the Debug option in Visual C++.

6) Make sure that you have an opening screen where it explains exactly what the program does before starting the program. Remember, this is a child and need all the help possible. Also tell the user of all things to be avoided when data is entered, such as 0s in the division section.

            The partial output:

Try different types of input and answer the following questions and submit your answers on a separate typed paper:
  1. Under what circumstances your program failed?
  2. How would you be able to fix it?
 You will submit:
  1. A typed algorithm of the program above (be complete!).
  2. A hard copy of your program, using proper style, well documented (with lots of comments on the sections of the programs, variables, etc.).
  3. A floppy disk with the source code and write on the outside what compiler was used. NOTE: If you are using Visual C++ you will not be able to start and compile a program on the floppy disk. Create the program in a Zip disk or hard drive. Submit only the .cpp file in floppy.
  4. A sample run similar to the one provided (try different numbers for input) *See notes.
  5. The answers to the questions above.

*Notes:

To get a printout of the your program execution do one of the following after compiling, linking, and running your program in the IDE.

(Using Borland)

a) Press the keys ALT-Enter so your compiler window becomes part of the windows environment, then use the mark option on the top of the window, then choose copy.

b) Open some text editing program, such as Word or Notepad and choose the Paste option. 

Unfortunately you can not print the entire output using this method, so you will have to do one screen at a time. (we may do this only for the first two assignments to get used to the process)

You must include your full name on every thing you hand in (including your disk). 

You must also put everything you submit in a large envelope.

You must keep all originals with yourself, just hand in copies.

Hold on to your graded assignments until the end of this term (at least).

Any questions, let me know...

 

 

Back to CS 161 Homepage
This page was last modified April 19, 2001
wmorales@pcc.edu