CSE1502-O4 - Introduction to Software Development with C++

Matt Mahoney
Spring 2009
http://cs.fit.edu/~mmahoney/cse1502/

Schedule

Jan. 12 through Apr. 29, 2009 on Mon., Wed., and Fri., 3:00-3:50 PM, Olin room EC 127-128 (combined rooms). No class on Jan. 19, Feb. 16, Mar. 2-6. Final exam May 7, 2009, 8:00-10:00 AM.

Instructor

Matt Mahoney, mmahoneycs.fit.edu or matmahoneyyahoo.com. Office hours are immediately after class or by email. I do not have an office. Help is also available in room EC272. See schedule outside the room.

Textbook

Engineering Problem Solving with C++ 2nd Ed. by Delores M. Etter and Jeanine A. Ingber, Pearson/Prentice Hall. ISBN 978-0-13-602071-4. Also read the tutorial and Introduction to C++.

Lab Computers

You will need a TRACKS user name and password to use the classroom computers. They have g++ and Visual Studio C++ installed. We will mostly be using g++ in class and running programs from a command window.

Mailing list

All students are expected to join the CSE 1502 mailing list. You are responsible for material posted to this list. You are also encouraged to post questions or answers about your homework or about C++ in general here, because other students might have the same questions.

To post a message, send email to cse1502@lists.fit.edu. When replying to a post, remember to check the "to" address before sending, depending on whether you want to reply to the sender or the whole class. Please do not post your homework to the list.

Grading policy

Exams (50% of grade)

Your exam grade is the average of the best three grades after dropping the lowest grade. There are no makeup exams for any reason. All exams are weighted equally. If you miss an exam (including the final), then that is your drop. All exams are open book and open notes, but no computers or electronic devices are allowed.

Fall 2006 exams
Exam 3
Final exam

Spring 2007 exams
Exam 1
Exam 2
Exam 3
Final

Fall 2007 exams
Exam 1
Exam 2
Exam 3
Final

Spring 2008 exams
Exam 1
Exam 2
Exam 3
Final

Fall 2008 exams
Exam 1
Exam 2
Exam 3
Final

Homework (50% of grade)

Homework assignments will be programming assignments graded manually for correct design, function, documentation, and neatness. Partial credit is given based on my subjective opinion of how well you understand and describe the problem in comments (the specification), your approach (design), and testing (whether it works or gives appropriate error messages for unusual or unexpected inputs). Grading depends on human readability as well as machine readability. Code should be indented using the style in the book. Major sections should be commented. Variable names should be self documenting or their purpose should be commented as appropriate. Your homework should include your name and email address in the comments.

Homework must be submitted by email by midnight of the due date. Late assignments are penalized 20% per day. I encourage you to submit homework early for me to comment on, and submit revised versions later without penalty. I will grade only the best version.

You must cite all sources of outside help in your comments, including help received from other students or the Internet. Copying code or allowing your code to be copied is not allowed. Group assignments are not allowed. You are expected to know the CS department policy on academic honesty. Penalties for cheating can range from a zero on the assignment to expulsion from the university. I report incidents of cheating to the department head.

Final grade = (average of top 3 exams)/2 + (total of homework grades)/2.
90-100=A, 80-89=B, 70-79=C, 60-69=D, 0-59=F.

Fall 2006 syllabus
Spring 2007 syllabus
Fall 2007 syllabus
Spring 2008 syllabus
Fall 2008 syllabus

Compiler

You will need a C++ compiler for your home computer. You may use any standard ANSI C++ (released since 1999) compiler under any operating system (Windows, UNIX, Linux, Mac). I recommend (but don't require) MinGW g++ for Windows or GNU g++ for other operating systems. We will be using g++ in class. All of the following are free under Windows:

MinGW g++. This runs from a command window only. Download and run the installer (MinGW_Toolbox_Setup.exe, 23 MB). Add C:\mingw\bin to your PATH. In Vista, you may also have to copy the file cc1plus.exe from c:\mingw\libexec\gcc\mingw32\3.4.5 to c:\mingw\bin.

Visual Studio 2005 Pro or Standard. You will need a TRACKS account and be registered for this class to download it. This has a GUI and lots of features. The Pro version download is 2.8 GB (DVD image). Not recommended for older machines or slower connections. To install, download the 4 .rar files, unrar e *.rar, then burn a DVD from the .iso file or use the provided program to mount the .iso to disk and run vs\autorun.exe as administrator.

Borland C++ 5.5. This runs from a command window only. Works on old machines. Click on "compiler", register, and download the installer (8.7 MB). After installing, add C:\Borland\BCC55\bin to your PATH and create the file C:\Borland\BCC55\bcc32.cfg exactly as shown below:

-I"c:\Borland\Bcc55\include"
-L"c:\Borland\Bcc55\lib"

Digital Mars C++ Compiler 8.49. This runs from a command window only. Works on old machines. Unzip stlport.zip (2 MB) and dm849c.zip (3 MB) in C:\ to install in C:\dm and add C:\dm\bin to your PATH.

Be sure to test your compiler by writing a simple program, compiling, and running it, e.g.

// Paste this file into notepad and save as hello.cpp
// To compile from a command window:
//   MinGW:   g++ -Wall hello.cpp -o hello.exe
//   Borland: bcc32 hello.cpp
//   Mars:    dmc hello.cpp -IC:\dm\stlport\stlport
// To run: hello
//
// In Visual Studio: create new project, select Win32 console application, 
//   uncheck "precompiled headers", delete the supplied code, replace with this code
//   and click Build project, Debug/Start debugging.
//
#include <iostream>
using namespace std;
int main()
{
  cout << "Hello world\n";
  return 0;
}

Homework #1 - Projectile

Due Mon. Jan. 26, midnight, 10 points. A projectile is fired at initial velocity v and angle theta from horizontal. You will write a program that inputs v (in meters per second) and theta (in degrees) and outputs the distance in both feet and meters to where the projectile lands. See figure below. Assume the surface is horizontal, that the acceleration of gravity is 9.8 m/s2, and neglect the effects of air resistance.

Be sure your program prompts the user to enter the two values. For example (user input shown in bold):

  Enter initial velocity in meters per second: 25.7
  Enter elevation angle (0 to 90 degrees): 64.5
  The projectile travels 52.3773 meters, or 171.841 feet.

The input and output does not have to be formatted exactly as shown, but should give the correct answers. Your program should include a specification (what are the inputs and outputs?) and your name in the comments. Email the source code file (.cpp file) to me as an attachment.

Homework #2 - Test Grader

Due Mon. Feb. 9, midnight (15 points). Write a program to score arithmetic tests. Your program will read arithmetic equations and output whether or not they are correct. If not, provide the correct answer. When finished, print the number correct, total number of equations, and the numeric grade (out of 100). Each equation will be on a line by itself and have the form:

  number operator number = number
where number is an integer like 87 or -2 (no decimal point), operator is one of +, -, *, or /, and there are single spaces between each of the 5 tokens. The program should read until end of file or until it encounters any invalid input. For example, if the input is:
  3 + 2 = 5
  100 / -7 = -14
  8 - 2 = 5
  4 * 1000 = 4000
  2.0 = 73 - seventy one (invalid input, stop here)
  2 + 2 = 4
Then the output should be:
  3 + 2 = 5 is correct.
  100 / -7 = -14 is correct.
  8 - 2 = 5 is wrong. The answer is 6.
  4 * 1000 = 4000 is correct.

  3 out of 4 were correct (75%).

You can test your program by copying the above input into a text file and running your program with the input redirected to that file.

The following input is invalid: characters other than digits or a leading - where a number is expected, characters other than + - * or / where an operator is expected, and anything other than = where = is expected. You do not need to print an error message here. You may simply ignore the input and everything after it. You do not need to prompt for input.

Be sure your specification is complete and understandable to someone who knows nothing about this assignment or your program. Be sure to include your assumptions about how division is computed with regard to rounding and division by 0.

Homework #3 - Fifth roots

Due Feb. 25, 2009, midnight (15 points). Write a program to calculate fifth roots to at least 12 significant digits using the Newton-Raphson method (section 5.8). For example:

  Enter a number: 32
  The fifth root of 32 is 2

  Enter a number: -99.5
  The fifth root of -99.5 is -2.50936950618

Homework #4 - e

Due Mon. Mar. 16, midnight (20 points). Write a program that inputs a number n and outputs e (2.718281828459...) to n decimal places. You can calculate e as follows:

  e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! +...
Since n might be large (like 1000000) you will need to use strings to represent high precision numbers. Your program must have two functions as follows, which must be used in your computation:

Solution.

Homework #5 - vector

Due Wed. Apr. 8, midnight (20 points). Write a program that reads a list of numbers (type double) from a file into a vector. Your program should output the size of the list, the mean, median, range, and standard deviation, and the original list sorted from smallest to largest. The median is the middle value when sorted, or the average of the two middle values if the size of the list is even. The range is the difference between the smallest and largest values.

The input file will be a text file with one number on each line. Your program should ask the user to enter the name of the file. It should print an error message if the file does not exist. Your program should read to the end of the file, or until the first invalid input, whichever comes first. For example, if the file test3.txt contains:

  3
  5.4
  2.0
  -6.82
You may assume the input contains at least 2 numbers, although it would still be a good idea to check. Your program might run as follows:
  Input file? test1.txt
  There are 3 numbers
  The mean is 2.5
  The median is 2.5
  The range is 2
  The standard deviation is 1
  The list in order is 1.5 2.5 3.5

  Input file? test2.txt
  There are 12 numbers
  The mean is 30.4167
  The median is 31
  The range is 3
  The standard deviation is 0.900337
  The list in order is 28 30 30 30 30 31 31 31 31 31 31 31

  Input file? test3.txt
  There are 4 numbers
  The mean is 0.895
  The median is 2.5
  The range is 12.22
  The standard deviation is 5.33755
  The list in order is -6.82 2 3 5.4

  Input file? test4.txt
  There are 1000 numbers
  The mean is 0.321899
  The median is 0.0584966
  The range is 35.0572
  The standard deviation is 2.00096
  The list in order is 0.0305274 0.0305563 ... 35.0877

  Input file? foo
  Error: foo not found

Solution

Homework #6 - Magic Squares

Due Apr. 29, 2009, midnight (20 points). A magic square is an n by n square of numbers where each of the rows, columns, and the two diagonals add up to the same values. For example, in the n = 3 magic square below, the sum is 15.

   8   1   6
   3   5   7
   4   9   2
When n is odd, an easy way to make a magic square is to write the numbers 1 through n2 starting at the top center and moving up and to the right. When going off the edge of the square, you "wrap around" to the opposite edge and continue from there. If the space is occupied, then write the next number in the space below instead of the upper right.

It is also possible to have an initial value and step different from 1. For example, the following n = 5 sqaure has a starting value of 10 and a step size of 2, so it counts 10, 12, 14, ..., 58. The rows, columns, and diagionals each add up to 170.

  42  56  10  24  38
  54  18  22  36  40
  16  20  34  48  52
  28  32  46  50  14
  30  44  58  12  26

Your homework assignment is to write a program that prints magic squares up to n = 19 where n is odd. Your program should take 3 command line arguments (not input from cin). The first argument is n. The second is the initial value. The third is the step size. For example, if your program is called square.cpp and compiles to square.exe, then the command:

  square 3 1 1
will output the first magic square above, and
  square 5 10 2
will output the second square. Be sure the output is formatted so that the columns line up neatly for all printed numbers in the range -99 to 999. Your program should print an error message (not crash) if the user enters less than 3 arguments or if the first argument is not an odd number in the range 1 through 19. The other arguments could be any integer and might be negative.

Extra Credit (up to 20 points, but not exceeding 100 for the total of all homework). Extend your program to accept even numbers for n (except 2). For example:

  square 4 100 10
would output:
 100 240 230 130
 210 150 160 180
 170 190 200 140
 220 120 110 250
or some other magic square. In any case, the output numbers must all be from the sequence {start, start+step, start+step*2, start+step*3, ..., start+step*(n2-1)} using each number once. Be sure your specification says what values of n are accepted, whether or not you do the extra credit.

You can use sqtest.cpp to test your program (with or without extra credit) by piping the output of your program to the input of this program. If correct, it will report n, start, step, and the sum over each row. If not correct, it will tell you why your square is not magic. To run:

  square n start step | sqtest