CS1711 - Fall 1997

To send an email to your instructor, click on the name:

Prof. Ford
Prof. Rosebrugh

Solutions to all the assignments:

www.mta.ca/~rford/courses/fall97/cs1711

Current Assignment

Examples from the text

Some Class Examples - 1711A

A free C++ compiler and programming environment

General Information

The course meets MWF at 9:30 (Sect A) and 1:00 (Sect B); the text is "Problem Solving with C++" by Savitch. We will cover approximately Chapters 1 to 5, parts of 7, 9, 10 and 12. There will be assigned one and one-half hour labs held in the PC Lab in Physics and Engineering. Labs start the week of September 15. There will be several programming assignments. One in-class test will be held on October 24. For official detail see the Department Handbook.

In order to pass the course a passing grade on the aggregate of the test and the final exam must be obtained. The final grade in the course will be assigned with approximately the following weighting:

Using Turbo C++ in residence

The following steps will add the Programming group to your desktop. Then you can load the Turbo C++ IDE just as if you were in the lab.

Windows:
from Program Manager, select File, New, Program Group, and specify the n:\winn\programm.grp in the path field ( not Description field).

Win95:
Start Menu, Run, n:\winn\programm.grp will add shortcut on the desktop

Assignments

Detailed instructions on how to hand in the program files for your assignments will be given in class. Remember that a printed copy is also required. File names are to be as specified after each question.

Headers

Every program submitted must include a `header' comment at the beginning in the following format:

Author: Jane Doe, jnede (give your user name)

Date: 24 September 1997

Task: Assignment 1, Problem 1

Description: (Here you give in your own words a description of the problem solved.)

Filename: convert.cpp

Assignment 1

Due Wednesday, September 24:

1. Write a program to convert a temperature in degrees Fahrenheit to degrees Celsius.

Problem input:
fahrenheit : integer (temperature in degrees Fahrenheit)

Problem output:
celsius : real (temperature in degrees Celsius)

Relevant formula
celsius = (5.0/9.0) x (fahrenheit- 32.0)

File name: convert.cpp

2. Write a program that reads in the length and the width of a rectangular yard and the length and width of a rectangular house situated in the yard. Your program should compute the time required (in minutes) to cut the grass at the rate of 2.3 square meters per second.

File name: cuttime.cpp


Assignment 2

Due Friday, October 3:

1. Write a program that determines the additional tax owed by an employee. The tax on net income is 4%. Determine net income by subtracting a $500 allowance for each dependent from gross income. Your program will read gross income, number of dependents, and tax amount already deducted. It will then compute the actual tax owed and print the difference between tax owed and tax deducted followed by the message `SEND CHEQUE' or `REFUND', depending on whether the difference is positive or negative.

File name: tax.cpp

2. Write a program that will find the smallest, largest, and average values in a collection of N numbers. Read in the value of N before reading each value in the collection of N numbers. In addition, your program must compute and display both the range of values in the data collection and the standard deviation of the data collection. To compute the standard deviation, accumulate the sum of the data (sum) and the sum of the squares of the data values (sumsquares) in the main loop. After loop exit, use the formula

standard deviation = squareroot( [ sumsquares - sum*sum/N] / N )

File name: standev.cpp

Assignment 3

Due Friday, October 17:

Write a program to generate a calendar for a year. The program should accept a year and the day of the week for January 1 of that year (1 = Sunday, 7 = Saturday). Remember that February has 29 days if the year is divisible by 4. The calendar should be printed in the following form (for each month)

January

1 2 3 4 5 6 7
8 9 . . .

Some specifications of functions to use in your program:

void print_monthname(int m)
/* Displays the name of the given month m on a line by itself.
m - month number (1 - January, ..., 12 - December)*/

int month_length(int m, int y)
/* Computes the number of days in the given month. The year is required to determine if February has 28 or 29 days.
m - month we are interested in; y - year we are interested in
returns - the number of days in the given month */

int is_leap_year(int y)
/* Determines if the given year is a leap year. y - the year we are interested in
returns - 1 if the year is a leap year; 0 if the year is not a leap year */

int print_month(int month, int year, int day)
/* Displays the days of the given month in the usual calendar form.
month - the month we would like to see; year - the year we are interested in; day - the starting day of the month
returns - the starting day of the next month. If this month ends on day 3 then next month should start on day 4. */

File name: calendar.cpp

example solution -->


Assignment 4

Due Wednesday, October 29:

1. We are used to counting in base 10, but that is not the only possible base. A base 8 number can be expressed using the digits 0 through 7. Write a program that has functions which input, display, increment, and decrement three-digit base 8 numbers, where there is one formal parameter per digit. Consider only positive numbers.

Some functions to use:

void get_digits(int input, int& dig1, int& dig2, int& dig3)
// Pre: a 3 digit base 8 number read from the terminal as if it were
// an integer, input
// Post: the dig1, dig2, dig3 hold the digits of input (largest order to smallest

void display(int dig1, int dig2, int dig3)
// Displays the base 8 number with digits dig1-3

void increment(int& dig1, int& dig2, int& dig3)
//Pre: dig1-3 are digits of a base 8 number, N
//Post: dig1-3 are digits of N+1 (base 8)

void decrement(int& dig1, int& dig2, int& dig3)
//Pre: dig1-3 are digits of a base 8 number, N
//Post: dig1-3 are digits of N-1 (base 8)

File name: base8.cpp

2. The bisection method is one way of finding an approximate root for the equation f(x) = 0 on the interval XLeft to XRight , inclusive (assuming the function is continuous on this interval). The interval end points ( XLeft and XRight ) and the tolerance for the approximation ( Epsilon ) are entered by the user. The bisection method calls for the identification of an interval [XLeft, XRight] that is less than Epsilon in length over which f(x) changes sign (from positive to negative or vice versa). The midpoint ( XMid = (XLeft + XRight)/2.0 ) of the interval will be an approximation to the root of the equation when f(XMid) is very close to 0 . Of course, if you find a value of XMid so that f(XMid)=0 , you have found a very good approximation of the root and the algorithm should stop.

One way to detect a sign change is to examine the value of the products f(XLeft) x f(XMid) and f(XMid) x f(XRight) . If one of the products is negative, then a sign change has occurred over that interval (either [XLeft, XMid] or [XMid, XRight] . If neither product is negative, there is no root in the interval XLeft, XRight . If the sign change occurs in the interval [XLeft, XMid] , let XRight = XMid and repeat the process. Similarly, if the sign change occurs in the interval [XMid, XRight] , let XLeft =XMid and repeat the process.

Write a program that uses the bisection method to determine an approximate root of the equation

5*x*x*x - 2*x - 2 = 0

over the interval [0,2] using Epsilon = 0.0001 .

For a view of the function y(x) = 5*x*x*x - 2*x - 2 on the interval concerned see this gif file.


Assignment 5

Due Friday, November 14:

1. Write a program to accomplish the following tasks:

Open one file for input and one for output. The input file is to be read and copied to the output file with the addition of line numbers at the beginning of each line.

In addition, the program is to display (on screen) the lengths of the shortest and longest lines of the file and display the average line length of the file.

Your program should allow the user to specify the names of the input and output files. (See text example.)

File name: stream.cpp

2. A prime number is any number divisible only by one and itself. Write a program to compute all of the prime numbers between 1 and 2000 by the method described here. One way to generate prime numbers is to create an array of Boolean values (i.e. values may only be 0 or 1) where the value is TRUE (1) for indices of prime values and FALSE (0) otherwise. Initially set all array entries to TRUE. Then, for every number from 2 to 1000, set array locations indexed by multiples of the number (but not the number itself) to FALSE. When done, output all numbers whose array location is TRUE. These will be prime numbers. NOTE: 0 and 1 are not prime!

File name: prime.cpp


Assignment 6

Due Monday, December 1:

1. In chess the Queen can move horizontally, vertically or diagonally on an 8 x 8 board. Write a program that displays a chess board as characters. The square has a Q if a queen is on it, an X if it is protected by a queen on the board and a dot (period) otherwise. The board starts with all locations empty. The user can place queens by supplying a row and column location that contains a dot. The program ends when there are no dots left. Try to see how many queens you can place on the board (8 is possible).

For implementation, you must use the following functions:

clear_board - clears the board by placing the appropriate symbol
place_queen - places a queen on the board in the given location. If the location is non-empty this operation should be ignored
board_empty - checks a location to see if its empty
display_board - displays the board on the screen (you may wish to clear the screen first).

File name: queen.cpp

2. A real number in scientific notation is represented by its mantissa (a decimal fraction) and its exponent. You are to write functions which implement a data structure for real numbers in scientific notation. You will need functions to read and write a real number (represented as a char value for the sign, a double values for the mantissa and an integer value for the exponent.)

Also write functions that compute the sum, product, difference and quotient of two real numbers. (You will want to use the math.h library function pow.)

Your main function must be adequate to test the functions required.

File name: real.cpp


Text example programs

1-08
2-01
2-03
2-06
2-09
2-10
2-13
2-14
2-15
3-1
3-3
3-5
3-7
3-8
3-9
3-10
3-11
3-12
3-13
3-14
3-15
4-02
4-02
4-02
4-03
4-04
4-06
4-07
4-08
4-09
4-10
4-11
5-1
5-2
5-3
5-5
5-6
5-7

Some class example programs

ball-bearing volume
ball-bearing cost (if statements and while loops)
and reimplemented with functions
modular multiplication table and a driver
palindrome checker
magic square checker
rational abstract data type