CST1620 C++ Programming
Assignments


Click on this link to turn in your assignments:
\\Rc-hutch-ap\VOL1\HOME\allen_b\CST1620Labs
  • Turn in the complete folder for each exercise.  The folder should contain the all your files.
  • Your name and exercise number should be in the comments at the top of the program.

Note:  This link only works if you are connected to the Ridgewater campus network and using Microsoft Internet Explorer. (Netscape cannot be used).

If you do not have network access, you may turn your homework in on a floppy disk.  You must label the diskette with your name, assignment number, and course number.  The diskette must be turned in at the start of class.


Note:  Most of the programming examples in the textbook can be found on the CD-ROM included with the book.

Data types in C++.

If you compile your code in debug mode, you can expect your exe file to be about 550KB for these exercises.  If you compile your code in release mode, your files will be about 150KB.

Lab 1

Exercises

Exercise 1.25

Write a program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words "is larger."  If the numbers are equal, print the message "These numbers are equal."

  Result

Exercise 1.26

Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers.  The screen dialogue should appear as shown in the book.

  Result

Exercise 1.32

Write a program that reads in two integers and determines and prints if the first is a multiple of the second.  (Hint: use the modulus operator.)

 


Lab 2

Exercises

Exercise 2.16

Write a program to solve the following:

Drivers are concerned with the mileage obtained by their automobiles.  One driver has kept track of several tankfuls of gasoline by recording miles driver and gallons used for each tankful.  Develop a C++ program that uses a while structure to input the miles driving and gallons used for each tankful.  The program should calculate and display the miles per gallon obtained for each tankful.  After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls.

 

Exercise 2.28

Write a program that reads in the size of the side of a square and then prints a hollow square of that size out of asterisks and blanks.  Your program should work for squares of all sizes between 1 and 20.  For example, if your program reads a size of 5, it should print

*****
*   *
*   *
*   *
*****

 

Exercise 2.47

Write a program that uses for structures to print the following patterns separately, one below the other.  Use for loops to generate the patterns.  All asterisks(*) should be printed by a single statement of the form cout << '*'; (this causes the asterisks to print side by side).  Hint: The last two patterns require that each line begin with an appropriate number of blanks.

Pattern A
*
**
***
****
*****
******
*******
********
*********
**********
Pattern B
**********
*********
********
*******
******
*****
****
***
**
*
Pattern C
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *
Pattern D
         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********

  Result


Lab 3

Exercises

Exercise 3.19

Define a function hypotenuse that calculates the length of the hypotenuse of a right triangle when the other two sides are given.  Use this function in a program to determine the length of the hypotenuse for each of the triangles below.  The function should take two double arguments and return the hypotenuse as a double.

Test data:
Triangle  Side1  Side2
    1       3.0    4.0
    2       5.0   12.0
    3       8.0   15.0

Hint:  In a right triangle a2 + b2 = c2   where a and b are the short sides and c is the hypotenuse.

Exercise 3.27

Implement the following integer functions:

  1. Function celsius returns the Celsius equivalent of a Fahrenheit temperature.
  2. Function fahrenheit returns the Fahrenheit equivalent of a Celsius temperature.
  3. Use these functions to write a program that prints charts showing the Fahrenheit equivalents of all Celsius temperatures from 0 to 100 degrees, and the Celsius equivalents of all Fahrenheit temperatures from 32 to 212 degrees.  Print the outputs in a neat tabular format the minimizes the number of lines of output while remaining readable.

Hint:  Celsius = 5/9 * (Fahrenheit - 32)  and Fahrenheit = (9/5 * Celsius) + 32
You will need to use casting in the equation to produce correct results.

  Result


Lab 4

Exercises

Exercise 4.17

Write a program that simulates the rolling of two dice.  The program should use rand to roll the first die, and should use rand again to roll the second die.  The sum of the two values should then be calculated.  Note:  Since each die can show an Integer value from 1 to 6, the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and and 12 being the least frequent sums.  The figure below shows the 36 possible combinations of the two dice.  Your program should roll the two dice 36,000 times.  Use a single subscripted array to tally the number of times each possible sum appears.  Print the results in a tabular format.  Are the results you obtained reasonable? (i.e., there are six ways to roll a 7), so approximately one sixth of all the rolls should be 7.

  1 2 3 4 5 6
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 5 6 7 8 9
4 5 6 7 8 9 10
5 6 7 8 9 10 11
6 7 8 9 10 11 12

  Result


Lab 5

Exercises

Exercise 5.12

Modify the program in Figure 5.24 so that the card dealing function deals a five-card poker hand.  Then write functions to accomplish each of the following:

  • Determine whether the hand contains a pair.
  • Determine whether the hand contains two pairs.
  • Determine whether the hand contains three of a kind (e.g., three jacks).
  • Determine whether the hand contains four of a kind (e.g., four aces).
  • Determine whether the hand contains a flush (i.e., all five cards of the same suit).
  • Determine whether the hand contains a straight (i.e., five cards of consecutive face values).
Extra Credit - Exercise 5.13

Use the functions developed in Exercise 5.12 to write a program that deals two five-card poker hands, evaluates each hand and determines which is the better hand.
 

Exercise 5.44

Dates are commonly printed in several different formats in business correspondence.  Two of the more common formats are

12/07/1941
December 7, 1941

Write a program that reads a date in the first format and prints that date in the second format.
 

Exercise 5.46

Write a program that inputs a numeric check amount and writes the word equivalent of the amount.  Your program should be able to handle check amounts as large as $99,999.99.  For example the amount 112.42 should be written as

ONE HUNDRED TWELVE and 43/100


Lab 6

Exercises

Exercise 6.7

Create a class called Rational for performing arithmetic with fractions.  Write a program to test your class.  Create appropriate .cpp and .h header files.

Use integer variables to represent the private data of the class - the numerator and the denominator.  Provide a constructor that enables an object of this class to be initialized when it is declared.  The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form.  For example, the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator.  Provide public member functions that perform each of the following tasks:

  • Adding two Rational numbers.  The result should be stored in reduced form.
  • Subtracting two Rational numbers.  The result should be stored in reduced form.
  • Multiplying two Rational numbers.  The result should be stored in reduced form.
  • Dividing two Rational numbers.  The result should be stored in reduced form.
  • Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator.
  • Printing Rational numbers in floating-point format.

  Result

Exercise 6.8

Modify the Time class of Fig. 6.18 to include a tick member function that increments the time stored in a Time object by one second.  The Time object should always remain in a consistent state.  Write a program that tests the tick member function in a loop that prints the time in standard format during each iteration of the loop to illustrate that the tick member function works correctly.  Be sure to test the following cases:

  • Incrementing into the next minute.
  • Incrementing into the next hour.
  • Incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).
Exercise 6.9

Modify the Date class of Fig. 6.24 to perform error checking on the initializer values for data members month, day and year.  Also, provide a member function nextDay to increment the day by one.  The Date object should always remain in a consistent state.  Write a program that tests function nextDay in a loop that prints the date during each iteration to illustrate that nextDay works correctly.  Be sure to test the following cases:

  • Incrementing into the next month.
  • Incrementing into the next year.
Exercise 6.10

Combine the modified Time class of Exercise 6.8 and the modified Date class of Exercise 6.9 into one class called DateAndTime.  Modify the tick function to call the nextDay function if the time increments into the next day.  Modify function printStandard and printUniversal to output the date and time.  Write a program to test the new class DateAndTime.  Specifically, test incrementing the time into the next day.

Exercise 6.11

Modify the set functions in the program of Fig. 6.18 to return appropriate error values if an attempt is made to set a data member of an object of class Time to an invalid value.  Write a program that tests your new version of class Time.  Display error messages when set methods return error values.


Lab 8

Exercises

Exercise 8.17 (Modified from the book exercise)

Modify the program that you created for Exercise 6.7 in the following ways:

  • Overload the addition, subtraction, multiplication, and division operators for this class.
  • Overload the relational ( >, <, >=, <=, !=, ==) operators for this class.

Have the user enter two rational numbers.

Use your overloaded relational operators to compare the two rational numbers.  Print text describing if the first rational number is greater than, or less than, or equal to the second rational number.

In your program use your overloaded operators to perform addition, subtraction, multiplication, and division operations.  Print the result in a rational format.

  Result     Ex8_17.cpp     rational.h

Exercise 8.19

Develop class Polynomial.  The internal representation of a Polynomial is an array of terms.  Each term contains a coefficient and an exponent.  The term 2X4 has the coefficient of 2 and the exponent of 4.  Develop a complete class containing proper constructor and destructor functions as well as set and get functions.  The class should also provide the following overloaded operator capabilities:

  • Overload the addition operator (+) to add two Polynomials.
  • Overload the subtraction operator (-) to subtract two Polynomials.
  • Overload the assignment operator (=) to assign one Polynomial to another. (Extra Credit)
  • Overload the multiplication operator (*) to multiply two Polynomials. (Extra Credit)

You may hard code the original values into your arrays so that you do not spend a lot of time and effort coding the input mechanism.  Have an output method so that the results get printed to the screen.


Lab 12

Exercises

Exercise 12.17

Write a program that uses a for structure to print a table of ASCII values fro the characters in the ASCII character set from 33 to 126.  The program should print the decimal value, octal value, hexadecimal value and character value for each character.  Use the stream manipulators dec, oct, and hex to print the integer values.


Lab 13

Exercises

Exercise 13.1

Modify the program that you wrote in exercise 8.17.   Use try catch blocks to catch various execution errors.  An execution error that come to mind is having a 0 (zero) in the denominator in a rational or having extremely large integer values for the numerator and denominator causing an overflow condition.  Catch the errors and provide the user with the appropriate error message.


Lab 14

In Exercise 14.12  you will be creating a binary data file.  This file cannot be opened in Notepad or any other text editor.

To see the results of your binary file that you create, you may want to download a hex editor to view the hex digits that comprise your file.  Here is a hex editor that is has a 30 day demo period. 

HexEdit  http://www.ExpertComSoft.com

Exercises

Exercise 14.1

Modify the program that you wrote in exercise 13.1 (The Rational problem).   In the past, all output was directed to the screen.  Direct output to a sequential-access file named "rational.txt".  Be sure to output your input data as well so that someone later viewing the file can see both the data that was input to the program and the resulting output data. Every time you run the program, the new output should append to the existing file.  Time stamp your output with the current date and time.

Exercise 14.12

You are the owner of a hardware store and need to keep an inventory that can tell you what different tools you have, how many of each you have on hand, and the cost of each one.  Write a program that initializes the random-access file "hardware.dat" to one hundred empty records, lets you input the data concerning each tool, enables you to list all your tools, lets you delete a record for a tool that you no longer have and lets you update any information in the file.  The tool identification number should be the record number.  Use the following information to start your file:

Record # Tool name Quantity Cost
3 Electric sander 7 57.98
17 Hammer 76 11.99
24 Jig saw 21 11.00
39 Lawn mower 3 79.50
56 Power saw 18 99.99
68 Screwdriver 106 6.99
77 Sledge hammer 11 21.50
83 Wrench 34 7.50

Lab 15

Exercises

Exercise 15.7

(Simple Encryption)  Some information on the Internet may be encrypted with a simple algorithm known as "rot13", which rotates each character by 13 positions in the alphabet.  Thus, 'a' corresponds to 'n', and 'x' corresponds to 'k'.  rot13 is an example of symmetric key encryption.  With symmetric key encryption, both the encrypter and decrypter use the same key.

  • Write a program that encrypts a message using rot13.
  • Write a program that decrypts the scrambled message using 13 as the key.
  • Write a program to encrypt the message using a published algorithm or an algorithm that you develop.
  • Write a program that decrypts the message using your algorithm.

Lab 16

See this page for Lab 16.


Lab MFC

Exercises

Exercise MFC

Your instructor will show you how to create a MFC program using the Visual Studio 6 C++ MFC wizard.  Also your instructor will give you an example of a simple MFC program in class.  Use your instructor's example as a basis to creating your own MFC program.

Take your Rational program that you have been working on, and convert it to an MFC program so the user can enter their rationals in text boxes and the results are displayed on a form.