CMPSC 101

Programming Assignments

assignment 1 | assignment 2 | assignment 3 | assignment 4

Since the main course objective of CMPSC 101 is to use certain computer programming techniques to solve problems, you will have to complete a number of programs throughout the semester. It is essential that you follow the Programming Process. It is also important that you follow the course Documentation and Coding Standards when you write each program.

The Programming Process - Read Ch. 2, Section 3 of your textbook carefully. You must conscientiously follow these steps in order to be successful in writing programs efficiently. Students who do not have the patience to follow the steps WILL have difficulty completing the assignments.

1. Define the problem and understand the given specifications. Create a test plan.
2. Develop an algorithm by writing pseudocode.
3. Code the program by writing it out on paper first.
4. Test and debug the program.
5. Document and maintain the program.

Documentation and Coding Standards - Hopefully, you will become a better problem-solver throughout this course. However, even if you write programs that follow the given specifications perfectly and work to the degree that they display the proper results, you may miss points if you do not follow the Documentation and Coding Standards. Read these guidelines thoroughly. If you have any questions, be sure to consult the instructor and your textbook. Take the time to check your program and code thoroughly before submitting it so that you do not lose points for violating these guidelines.


TENTATIVE Assignment #1:

A framing shop charges 25 cents for each inch of frame and 10 cents for each square inch of backing material. All frames are one inch wide. Write a program that allows the user to input the length and width of the picture itself (not the frame) and calculates the total cost of framing the picture (besides labor). The backing material is a cloth material that covers the back of the picture and extends to the outside edges of the frame. For example, if a width of 4 inches and a length of 6 inches is inputted by the user, the total cost will be $10.80 (24 total inches of frame & 48 square inches of backing material.) If a width of 10 inches and a length of 15 inches in inputted, the total cost will be $33.90 (54 total inches of frame & 204 square inches of backing material.)

Save the source code file as "Assignment1.cpp". If the file is not named "Assignment1.cpp" exactly, you will lose points. You must pseudocode this assignment before you begin to write the code or type the code into C++. Write a test plan as well before you write out or type the code. You must choose the proper variable data types so that the final answer is accurate while using memory (RAM) efficiently. Include explanatory input prompt messages so that the user knows exactly what to type when you ask him/her for the length and width of the picture. Direct the user to input the length and width measurements in whole inches. Make sure that your answer is explained with a clearly worded, explanatory output message as well. Present the final cost as a number rounded or truncated to the hundredth's place (e.g. $78.65) and include a dollar symbol. The first line of your output must be your name.

You must follow the Documentation and Coding Standards so that it is easy to read your program and so that others can understand your code. Be sure to read the common errors that former CMPSC 101 students have made in the past on their programming assignments.

Preconditions:

You must hand in the following on separate pages stapled in this specified order:

  1. The hardcopy source code for this assignment. Staple multiple pages together, if applicable.
  2. The typed pseudocode that you used to write out your C++ code.
  3. The typed test plan that you developed before writing out the code. (Make sure that you use this test plan to test your program before submitting it.) The test plan must include several reasonable inputs as well as boundary and any other special values. You should include as many test cases as necessary so that the test plan is thorough. If possible, use tables in MS Word or MS Excel to neatly format your test plan (Excel can perform the calculations themselves.)

You must also hand in the source file (Assignment1.cpp) saved on a floppy disk as Assignment1.cpp. It is very important that you name the file as Assignment1.cpp exactly. There should be no other computer files or folders on this floppy disk. Use a full-sized blank floppy disk label and write label it with your name, the instructor's name, the name of the assignment (Assignment #1) and the name of the course, CMPSC 101. You will lose points if the file is named incorrectly or if the disk is labelled incorrectly. The floppy disk will not be returned to you after this assignment has been graded.


TENTATIVE Assignment #2:

Write a program that asks the user to think of a secret, whole number between or including 1 and 100 and then guesses the number. The program should make an initial guess of 50. The program should present a menu asking the user to enter 'h' if 50 is too high, an 'l' if its too low, or an 'r' if its right on. The program should be case-insensitive, allowing the user to enter upper or lowercase letters. Based on the response given by the user, the program should make another guess and present the menu again or it shoud tell the user what his/her secret number was. The program must continue to guess until the correct number is reached. You should use the most efficient algorithm as possible.

You will definitely need to use an indefinite loop structure in this program.  Also, you must use a switch structure in which your control expression is the char variable that the user inputs (an 'h', 'l', or 'r'). You must follow the Documentation and Coding Standards so that it is easy to read your program and so that others can understand your code. The first line of your output must be your name.

Preconditions:

You must hand in the following on separate pages stapled in this specified order:

  1. The hardcopy source code for this assignment. Staple multiple pages together, if applicable.
  2. The pseudocode that you used to write out your C++ code.
  3. The test plan that you developed before writing out the code. (Make sure that you use this test plan to test your program before submitting it.) The test plan must include at least 5 possible sequences of the game (that is, for 5 different user secret numbers) but more if you think they are necessary. Remember that you must show all boundary and other special cases as well as a few normal cases. If possible, use tables in MS Word or MS Excel to neatly format your test plan (Excel can perform the calculations themselves.)

Here are two example test plan items:

Possible Inputs

Expected Output

Reason for Test Item

 

guess

usersInput

   
1. 50, 25, 37, 43, 40, 38, 39 h, l, l, h, h, l, r "The secret number is 39." normal case
2. 50, 25, 12, 6, 3, 1 h, h, h, h, h, r "The secret number is 1." lower boundary

You must also hand in the source file saved on a floppy disk as Assignment2.cpp. It is very important for grading purposes that you name the file with the exact name, Assignment2.cpp . There should be no other computer files or folders on this floppy disk. Use a full-sized blank floppy disk label and write label it with your name, the instructor's name, the name of the assignment (Assignment #2) and the name of the course, CMPSC 101.You will lose points if the file is named incorrectly or if the disk is labelled incorrectly. The floppy disk will not be returned to you after this assignment has been graded.

Here is possible pseudocode for Assignment #2. You may use this pseudocode or entirely different pseudocode. You certainly may expand it, giving even more detailed steps if you think its necessary.

- ask the user to think of a number between 1 and 100
- make the initial guess of 50
- prompt the user to tell him that the computer's guess is the number 50

- begin a loop

         - prompt the user to signify whether the current guess is too high, too low, or the exact number
            - obtain the user's inputted answer
            - if the guess was the exact number, display a message indicating that the computer correctly guessed the secret number
            - if the guess was too high, make a new guess that is halfway between that last guess and the lower bound and reset the upper bound
            - if the guess was too low, make a new guess that is halfway between that last guess and the higher bound and reset the lower bound
            - display the new guess to the screen with an output message to the user


- end loop if the user indicates that the exact number was guessed

In class, we discussed the use of the statement,     guess = (upperBound - lowerBound) / 2 + lowerBound;


TENTATIVE Assignment #3:

Merge Projects 10-2 and 10-3 (from p. 202) into one project. The program must present a menu to the user that asks him/her whether he/she would like to encrypt or decrypt a string. After the user makes this choice, he/she will be asked to input a string. The string will be a sequence of characters that is guaranteed not to include any spaces. If encrypt is selected from the menu, the program must use a loop to encrypt a string by adding 1 to the ASCII value of each character in the string. The string "abcdef" would become "bcdefg", and the string "apple" would become "bqqmf. If decrypt is selected from the menu, the program must use a loop to decrypt a string by subtracting 1 from the ASCII value of each character in the string. The program should print an explanatory output message with the desired encrypted or decrypted result. Finally, the program must continue by presenting the menu of choices to the screen, allowing the user to select a third menu option, "Quit", at any time.

You should not use brute force (with 26 if statements). You must use functions (at least 2, maybe more), loops, and switch structures where appropriate. The letter 'z' must encrypt to an 'a' and the letter 'a' must decrypt to a 'z'. You must follow the Documentation and Coding Standards so that it is easy to read your program and so that others can understand your code.

You must also hand in the source file saved on a floppy disk as Assignment3.cpp. It is very important for grading purposes that you name the file with the exact name, Assignment3.cpp . There should be no other computer files or folders on this floppy disk. Use a full-sized blank floppy disk label and write label it with your name, the instructor's name, the name of the assignment (Assignment #3) and the name of the course, CMPSC 101. You will lose points if the file is named incorrectly or if the disk is labelled incorrectly. The floppy disk will not be returned to you after this assignment has been graded.

Preconditions:

You must hand in the following on separate pages stapled in this specified order:

  1. The hardcopy source code for this assignment. Staple multiple pages together, if applicable.
  2. The pseudocode that you used to write out your C++ code.
  3. The test plan that you developed before writing out the code. Remember that you must show all boundary and other special cases as well as a few normal cases. You should include as many test cases as necessary so that the test plan is thorough. If possible, use tables in MS Word or MS Excel to neatly format your test plan (Excel can perform the calculations themselves.)

Partial Pseudocode Suggestion for Assignment #3

explain to the user the purpose of the program (cout statement)

begin the loop
    prompt the user with a menu (a bunch of cout statements) - maybe a function
    prompt the user to input his/her menu choice (cout)
    obtain the user's choice (cin)
    use a switch structure to go to the encryption part of the program (probably a function)or the decryption part (another function)
end this outer loop when the user chooses the quit option on the menu

encryption part:
prompt the user to input a word
obtain the user's string

start a loop
        examine the next character of the string (use subscript notation)
        if the character is an 'a' then change it to a 'b' and display that character
end the loop when the length of the string has been reached

decryption part:
model it on the encryption part


TENTATIVE Assignment #4:

Write a program that declares an array of 5 floating point values. Have the program prompt the user for each of the five floating-point values and store them into the array. The program must determine how many of the values entered (not counting the first one) are larger than very first one. After displaying your name on the screen, display the number of values that are greater than the first one with an appropriate message. For example, if the values entered are 5.2, 6.1, 2.8, 8.9, and 3.3, the program should display the following message on the screen: "2 of the values in the array are larger than the value of the first element". The program must also write the highest value overall (possibly the first one) to an external sequential access file named "Asst4Data.txt". You can use Notepad on your computer to view the external sequential access file. This text file will be saved to the same folder as the source code file. Use functions in this program where they are appropriate. Do not forget that the first line of your displayed output must be your name.

You must follow the Documentation and Coding Standards so that it is easy to read your program and so that others can understand your code. You must also hand in the source file saved on a floppy disk as Assignment4.cpp. It is very important for grading purposes that you name the file with the exact name, Assignment4.cpp . There should be no other computer files or folders on this floppy disk. (It is not necessary to have Asst4Data.txt on the disk since it will be created automatically by your program upon execution.) Use a full-sized, blank floppy disk label and label it with your name, the instructor's name, the name of the assignment (Assignment #4) and the name of the course, CMPSC 101. You will lose points if the file is named incorrectly or if the disk is labelled incorrectly. The floppy disk will not be returned to you after this assignment has been graded.

Preconditions:

You must hand in the following on separate pages stapled in this specified order:

  1. The hardcopy source code for this assignment. Staple multiple pages together, if applicable.
  2. The pseudocode that you used to write out your C++ code.
  3. The test plan that you developed before writing out the code. Remember that you must show all boundary and other special cases as well as a few normal cases. You should include as many test cases as necessary so that the test plan is thorough. If possible, use tables in MS Word or MS Excel to neatly format your test plan (Excel can perform the calculations themselves.)

Possible Pseudocode for Assignment #4

explain to the user the purpose of the program
initialize all counters to zero
open the sequential access text file (outfile.open) and return a message to the user if an error occurred opening the file

prompt the user to input the first element
obtain the inputted value from the user
set this first element as the current largest element

begin a loop

   prompt the user to input the next element

   check to see if this element is larger than the very first element
      if it is larger, then increment the counter

   check to see if this element is larger than the current largest element
      if is larger, then set the current largest element to this element

end the loop when the 5th overall element has been entered

printout the final answer to the user with an output prompt message
write the largest element to the sequential access text file
close the external sequential access text file


CMPSC 101 Home Page