Translation to C++

This assignment is to provide practice in translating pseudocode to C++. There are three problems. In each case, you are given the pseudocode of algorithms that you have previously seen, and you are expected to produce the C++ programs and to test your program thoroughly.

Getting Started

First, you need to make a directory to place all of your assignments. This directory will be called cis121 and will be located in your home directory. If you have not done this, then do the following 2 steps. If you have it, then skip these 2 commands:

 

% cd

% mkdir cis121

 

Now you will make a subdirectory of cis121 called prog1. This is where you will put all of the C++ programs for assignment #1. Do the following:

 

% cd cis121

% mkdir prog1

% cd prog1

 

Now you are ready to start. You will need to use xcoral to edit your programs and g++ to compile your programs. If you have forgotten how to do this, refer to lab where editing and compiling were covered (Intro to  C++ lab). These first few steps should be used in all of your design assignments, where you should create prog2, prog3, prog4, etc. directories.

 

The error messages for the G++ Compiler are explained on the following webpage:

http://www.spelman.edu/~compsci/general/Compiler-Errors.html

 

Use your intro lab to remind you how to edit, compile, and execute your programs.


Problem #1

The following algorithm computes the average miles per gallon (found in the Chapter 2 slides from Schneider)

 

  1. Get values for gallons used, starting mileage, and ending mileage
  2. Set value of distance driven to (ending mileage - starting mileage)
  3. Set value of average miles per gallon to (distance driven / gallons used)
  4. Print the value of average miles per gallon
  5. if average miles per gallon is greater than 25.0 then
  6. Print the message You are getting good gas mileage
  7. Else
  8. Print the message You are NOT getting good gas mileage
  9. STOP

 

Write the C++ program for this algorithm, and test your program. Your testing should try to execute both the TRUE and FALSE cases of the if statement. Therefore, you should be able to identify one set of input that will execute line 6 and another input that executes line 8.

 

Include your input values and output values for each test as comments after your main function.

 

Save your program file to your cis121/prog1 directory. You may call this program gallons.cpp

Problem #2

The following algorithm is based on the algorithm above, but allows the user to continuously compute average miles per gallon for different input values until the user is finished and enters no.

 

  1. Set the value of response to yes
  2. While response is equal to yes do steps 3 - 12
  3. Get values for gallons used, starting mileage, and ending mileage
  4. Set value of distance driven to (ending mileage - starting mileage)
  5. Set value of average miles per gallon to (distance driven / gallons used)
  6. Print the value of average miles per gallon
  7. if average miles per gallon is greater than 25.0  then
  8. Print the message You are getting good gas mileage
  9. Else
  10. Print the message You are NOT getting good gas mileage
  11. Print the message Do you want to do this again? (enter yes or no)
  12. Get value of response from the user
  13. STOP

 

Write the C++ program for this algorithm, and test your program. Your testing should try to execute both the TRUE and FALSE cases of the if statement as in the previous problem, but should also test the different responses from the user. What happens if the user inputs no? What happens is the user inputs something other than yes or no, like Y or apple?

Include your input values and the output results for each test as comments after your main function.

 

Save your program file to your cis121/prog1 directory. You may call this program moregallons.cpp

Problem #3

The following algorithm is designed to compute the won-loss record of a team. The user is asked to input scores for CSU and its opponents. After all 10 scores are entered, then the program displays the CSU record and if they are 10-0, it prints a special message for being undefeated.

 

1.         Set the value of i to 1

2.         Set the values of Won, Lost, and Tied all to 0

3.         While i <=10 do

4.                 Get the value of CSUi and OPPi

5.                 If CSUi > OPPi  then

6.                       Set the value of Won to Won - 1

7.                 Else if CSUi < OPPi then

8.                       Set the value of Lost to Lost + 1

9.                 Else Set the value of Tied to Tied + 1

                 End of the While loop

10.       Print the values of Won, Lost, and Tied

11.       If Won = 10, then

12.           Print the message, Congratulations on your undefeated season.

13.       STOP

 

This program has errors in the logic, and as you find/fix errors make a note of them because you will be asked to submit your fixes. The idea is that you correct the errors after you implement the program; however, if you make modifications as you translate the algorithm to C++ then make note of those changes. First, write the C++ program for this algorithm, and test your program. As you test the program, you should discover the errors (you may find them as you type the code in). Use the previous examples of tests to determine what you should test, and which test is appropriate. Recall, you should try to test all possible paths through the program. You should have a variety of inputs to produce a variety of outputs. Your goal in testing is to prove that the program behaves properly under all circumstances.

As a bonus, enhance your program to produce additional statistics at the end:

  1. Total points scored by CSU in the season
  2. Total points allowed by CSU in the season
  3. Average points scored per game by CSU
  4. Maximum points scored in a game by CSU
  5. Minimum points scored in a game by CSU

 

Include your input values and the output results for each test as comments after your main function. In addition, include the errors you found to make the program work.

 

Save your program file to your cis121/prog1 directory. You may call this program season.cpp

Evaluation

You will be evaluated on the style of your program, use of comments, the accuracy of your programs and the thoroughness of your testing.

Submission

Submit your 3 .cpp files (program source files) and 3 .txt files (script-recorded test files) via WebCT.

           

NOTE: PLEASE MAKE SURE YOU ARE USING THE CORRECT COMMAND AND CHECK THE FILES BEING SUBMITTED.