CS 121

Programming Assignment 2

Assignment Date: Tuesday, February 26
Due Date: Thursday, March 7
DDD: Thursday, March 14


Instructions

  Below you will find descriptions of a number of simple programs. Each of these programs assumes you have already had some experience with each of the following C++ programming techniques or commands in addition to those listed for programming assignment 1:

  • Program input using cin with the extraction operator (>>) and with getline().
  • Simple input from a file (ifstream object) and output to a file (ofstream object) using the >> and << operators.
  • Using the cout command to output information to the screen.
  • Performing flow of control operations using conditional statements with if and switch.
  • Performing looping operations with for, while, and do-while loops.

You are to select one of the following programs to write. Follow this basic procedure:
  1. Create a new project in Visual Studio. You may call the program anything you wish. Many students like to keep it simple and just call the project something like Program_2 or ProgAssignment_2.

  2. Add a .CPP source code file to the project. You can name the source file something like Prog2Main.cpp. Place your main() function in this file.

  3. When you have completed writing the code, compile the program. If the program does not compile look carefully at the errors listed by the compiler and fix those errors. Also pay attention to the warnings the compiler gives you.

  4. When your program compiles, run it and test it thoroughly to make sure it is working correctly. Do not turn in your program till it is fully tested and you know the output is correct based on the inputs. Just because you get an output does NOT mean the program is working correctly. The instructor will compare your program's output to the known correct output. If it does not match you will get no credit.

  5. When you are sure the program is working correctly e-mail your program file (the .cpp file) to the instructor. Make sure you are sending the .cpp file and not another file. See the Submit Projects link on the web page for details.

Remember you must include the following comment at the top of your source file. If you do not place this at the top of your file your program will not be accepted.
	/*******************************************************************
	*   CS 121 Programming Assignment 2
	*   File: Source Code File Name
	*   Author: Your Name
	*   Desc: A brief description of what the program does.
	*   Date: Date file was submitted to the instructor
	*
	*   I attest that this program is entirely my own work
	*******************************************************************/
			

Program Options

Select one of the following programs to complete.


 
  1. Use functional decomposition to write a C++ program that asks the user to enter his or her weight and the name of a planet. The program then outputs how much the user would weigh on that planet. The following table gives the factor by which the weight must be multiplied for each planet.

    Planet Weight Factor
    Mercury 0.4155
    Venus 0.8975
    Earth 1.0
    Moon* 0.166
    Mars 0.3507
    Jupiter 2.5374
    Saturn 1.0677
    Uranus 0.8947
    Neptune 1.1794
    Pluto* 0.0899
    The moon and Pluto have been temporarily promoted to full "Planet" status.

    The program should output an error message if the user doesn't type a correct planet name. The prompt and the error message should make it clear to the user how a planet name must be entered. Be sure to use proper formatting and appropriate comments in your code.

    The output should be labeled clearly and formatted neatly. After displaying the user's weight on the chosen planet the program should ask the user if they want to check another weight. This should repeat until the user indicates they do not want to check any more weights.




  2. Use functional decomposition to write a C++ program that takes a number in the range of 0 to 6 and a second number in the range of 1 to 365 as input. The first number represents the day of the week on which the year begins, where 0 is Sunday, and so on. The second number indicates the day of the year.

    The program then outputs the name of the day of the week corresponding to the day of the year. The number of the day of the week can be computed as follows:

    day_of_week = (start_day + day_of_year - 1) % 7

    The program should output an error message if the numbers entered are not in the required ranges. The prompt and the error message should make it clear to the user how the numbers must be entered.

    Be sure to use proper formatting and appropriate comments in your code. The output should be labeled clearly and formatted neatly. After displaying the day of the week the program should ask the user if they want to check another day. This should repeat until the user indicates they do not want to check any more days.




  3. Design and write a C++ program that inputs a series of 24 hourly temperatures from a file called TempsTest.txt, and outputs a bar chart (using stars) of the temperatures for the day. The temperature should be printed to the left of the corresponding bar, and there should be a heading that gives the scale of the chart.

    The range of temperatures should be from -30 to 120. Because it is hard to display 150 characters on the screen, you should have each star represent a range of 3 degrees. That way, the bars will be at most 50 characters wide. Here is a partial example, showing the heading, the output for 10 of 24 temperatures: a negative temperature, and the output for 9 positive temperatures. Note how the temperatures are rounded to the appropriate number of stars.

    	
          Temperatures for 24 hours:
          -30        0          30          60          90          120
       -20    *******|
         0           |
         1           |
         2           |
         3           |*
         4           |*
         5           |**
        10           |***
        50           |*****************
       100           |*********************************
    
    
    Use meaningful variable names, proper indention, and appropriate comments. Thoroughly test the program using your own data sets.

    Note: the file TempsTest.txt provided for you to test this program is NOT the same one that will be used by the instructor for grading this program, nor does it have the same number of temperatures in the file. Do not hard-code these values.


A word of caution:

  • Your programming assignment must be your own work. Do not ask someone else to help you or to do the assignment for you. If you are having trouble please see the instructor and he will be glad to help.

  • If you copy someone else's code or let someone else copy your code you will both receive a zero for this assignment and could receive an F for the course as well as having the incident of cheating reported to the Dean.


Demonstration executables for each of these programs can be found in Prog2Demos.zip on the Downloads page.