Programming Assignments for Math 118, Spring 09


Lab 11: Inheritence (Not to be graded)

 


Lab 10 Both parts due 11:59 pm, Thu Apr 30

Part I:

Programming Projects 5 and 7 on pages 820-1.

For project 5, the second function should take a string argument (not a C-string). You will need to convert this string argument to a C-string using the c_str member function. Then, you will need to convert a C-string to a string object by using a constructor (whose argument is a C-string).

For Project 7, all the functions you use in sorting (including finding index_of_smallest, if you use it) must be recursive; you cannot use any loops. If you do this by modifying the sort function from Chapter 7, it would be easier to use 3 arguments for the sort function (instead of 2).

Part II:

Do Programming Project 8 on page 821.

Bonus (Due 11:59 pm Fri May 1). Do project 8 with a graphical illustration of all moves of the disks during the execution of the algorithm ( if you can do this with 3 disks, that will be good enough)


Lab 9: Due 11:59, Thu Apr 16

For this lab you need to create a project composed of several files.

The file that contains the main function must have the content of the file "Lab9Main.cpp" EXACTLY (except that you should replace the namespace "aydin" with your lastname), located in the class folder (P:\Class\Math\Aydin\118\FilesForLabs). Copy that file to your folder.

Write other necessary files, create a namespace (having your last name) and combine them into a project so that you can run the main program and it produces a reasonable output.

Here are the other files you need to write:

An interface (header) file called complex.h for the complex number class (which contains a definition of the complex number class, but no implementations of the member functions or constructors)
An implementation file, complex.cpp, which contains implementations of the members of the complex number class.
An interface (header) file called Functions.h with prototypes of the following functions:
factorial function // int fact(int);
exponential function // double exp(double)
natural logarithmic function// double log(double)
trig functions sine and cosine // double sin(double) and double cos(double)
An implementation file for the functions, Functions.cpp which contains definitions of the functions declared in the interface file. Use the formulas given in the class to define these functions. Also pay attention to the following: The factorial function is only defined for non-negative integers and the formula given for ln function is only valid if the argument is positive but less than 2. If an argument different from these is given to these 2 functions, then an appropriate error message should be output.

All of these functions and ComplexNumber class must be a part of the namespace having your last name.


Lab 8: Due 11:59 pm, Apr 9

In this lab we are going to practice operator overloading with complex numbers.
We will also be using some of the earlier concepts such as file input-output, formatting and sorting.
First, copy the file "inputdata.txt" from the class folder (P:\Class\Math\Aydin\118\FilesForLabs) into your own folder (and DO NOT modify this file).
All the output should be sent BOTH to the screen and to an output file which should be called "outputdata.txt".
The file "inputdata.txt" contains 9 pairs of real (double) numbers.
Overload the binary operators +, *, >>, <<, and > as well as the unary operator - for the complex number class. For >, compare complex numbers on the basis of their moduli, i.e., if z1 and z2 are two complex numbers then z1 > z2 if and only if modulus of z1 > modulus of z2. The modulus of a complex number z = a+bi is sqrt(a*a+b*b)
Read these numbers from inputdata.txt and create a complex number corresponding to each pair. Store these complex numbers in an array (of complex numbers) of size 10.
Ask for one more complex number from the user to store in the last entry of the array using the overloaded operator >>.
Print out (and send to the file as well) each one of these complex numbers in a+bi form (use overloaded << operator to do that).
If the imaginary part is negative, there should be negative sign only. For example if a complex number has real part 1 and imaginary part -2 then it should be printed out as 1-2i rather than 1+-2i. If the imaginary part is + or -1, do not write 1. For instance instead of 2-1i, write 2-i. Also, if either (but noth both) part is 0, then do not write it out.
Print out the negatives of each one these complex numbers.
Find and print out the sum and product of all these 10 complex numbers. Do necessary formatting so that the real and imaginary parts of sum and product have 2 places after the decimal point. Also print out the modulus of the sum and the modulus of the product.

Finally, sort these complex numbers in descending order according to their moduli (moduli is the plural of modulus) and print them in sorted order.


Lab 7, Due 11:59 pm, Thu, Apr 2

A) For this part, first copy the file "gradefile.txt" which is available in our class folder (full path: P:\\Class\\Math\\Aydin\\118\\FilesForLabs) into your own folder. DO NOT modify this file! (even after copying it into your folder).

Then write a program which does the following

1. Open gradefile.txt (from your own folder) and copy its content EXACTLY to another file in your folder. Call this output file gradeout.txt. Make sure it is an exact copy (including spacing).(Hint: use the member functions get and eof ). Close the file gradeout.txt after this operation.

2. As you see, this file contains a number of test scores. You will read the data from the gradefile.txt again. This time use another ifstream object to read the file a second time. Read every score as an integer (as opposed to regarding them as characters) and store in an integer array. (Assume that there are at most 100 scores). Find the number of scores in this file, maximum score, minimum score and average score. Now, open the file gradeout.txt, this time with the option to append. Output all of these values BOTH to the screen and to the file gradeout.txt. The average must be a double and should put exactly 2 places after the decimal point. (So you need to do necessary formatting). APPEND these data to the file gradeout.txt.
For part 1. using the member function get to read would be more convenient. For part 2, the extractor operator >> may be more useful. (See the relevant examples in the textbook).
3. Also output all the scores in descending order, and the median score both to the screen and to the file gradeout.txt

B) Do programming project 17 on page 370. The file babynames2004.txt is located at

P:\\Class\Math\\Aydin\\118\\SourceCodeFromTextbook\\Chapter06

C) Do Programming Project 18 on page 371-2 of the textbook. (Note: Firefox browser in our system should work to open svg files)

Bonus Project (You can work on this until the last day of classes): Do Programming Project 19 on page 373. You will earn bonus points by creating a good picture of your instructor. You can directly write the svg file (you do not need to write the svg file through a C++ program). If you do this, please create folder titled "Bonus". It is due the last day of classes.


Lab 6: Strings and Vectors Both parts due 11:59 pm, Thu March 26

Part 1: Do programming projects 1 and 2 on pages 491-492. To compute the word count, you may find it to be helpful to use library functions such as

isalpha( char ) (described on page 356, and 1006) or isalnum(char) [this one returns true if and only if the argument is a letter or a digit]

Part 2: Write a program that asks user to enter students (name and score; grade should be determined on the basis of score). Student will be a class as in Lab 5. Each time ask the user whether s/he wants to add another student (do not ask how many students the user wants to enter). Store the students in a vector (so you will continuously grow the vector). After the user finishes entering students, the program should report how many students have been entered and what the average score is. It should also sort the students (and print out the info) in descending order of scores


Lab 5

Part I: Due: 11:59 pm, Tue, March 17

Write a program that reads student names followed by their test scores. The program should output each student's name, followed by the test score and relevant grade. It should also find and print the highest test score and the name(s) of students having the highest test score. Each student data should be stored in a struct variable of the type StudentType, which has 3 components:
name (string), score (int, between 0-100), and grade (char). Assume that the class has no more than 20 students. Ask the user how many students is s/he going to enter. Use an array to store all students (so you need an array of students). Let the user repeat the computations as long as s/he wishes.

The grades are assigned as follows:
90-100: A
80-89: B
70-79: C
60-69: D
below 60: F

Note: You may encounter the "mixing cin >> variable and getline" problem. See pages 472-3 for the description and possible solutions to the problem.

Part II: Due 11:59, Thu March 19

a) Implement Part I above using classes this time.

Requirements:

All the data members of Student class (name, score, grade) must be private.
Define appropriate member functions to set and access private data.
Define appropriate constructors.
You should not use any functions other than member functions of Student class and constructors.

Do not forget to define the default constructor (Remember, once you define a constructor with at least one parameter, the compiler's default constructor is no longer available. See the programming tip on page 570)

b) Write a definition of a class named Point that can be used to store and manipulate the location of a point in the 2 dimensional plane. You will need to declare and implement the following member functions:

i) a member function set that sets the private data after an object of this class is created

ii) a member function to move the point by any amount along the vertical and horizontal directions specified by the first and second arguments

iii) two accessor functions to retrieve the current coordinates of the point

iv)*** a member function to rotate the point by 90 degrees clockwise around the origin *** Writing this function will be bonus. You will need some trigonometry, and some mathematical functions from the cmath library. Using polar coordinates will help. Try this after finishing other parts.

Embed your class in a test program that requests data for several points from the user, create the points, then exercises the member functions. Part of the assignment is to write a good test program.


Lab 4: Due Thu Feb 19, 11:59 pm

Part I: Make necessary changes to to the sorting program 7.12 (pages 415-20 in the textbook) so that it sorts an array of names (strings) in dictionary order. Make sure that the following are satisfied:

1. Use a global constant variable =20 for an upper bound on the size of the array to be sorted

2. Use a function to print the array in sorted order (also use a function to fill in the array)

3. Give the user option of entering (and sorting) as many sets of names as s/he wants.

4. Do not assume that the strings will be single words (so don't use cin to get the input)


Part II: Call a square matrix a "magic square" if the sum of each row, the sum of each column, and the sum of the (main) diagonal are all equal to the same number. As an easy example, if all the entries of the matrix are equal, then it is a magic square. Write a program which determines whether a given 4 by 4 matrix is a magic square. Assume that the entries are integers. Be sure to use a 2 dimensional array to store the input.


Lab 3: Due Thu, Feb 12, 11:59 pm

Do the programming project described here


Lab 2: Due Tue, Feb 3,11:59 pm

i) Do programming project 6 on page 175 (Keep in mind: If a is 0, it is not a quadratic equation)

ii) Do programming project 16 on page 179.

ii) Write a program that takes a positive integer n from the user and computes the following quantity with 4 digit precision

Hint: Use a nested, double for loop, and use two running totals. To help you test correctness of your program, here are some values: for n = 3, sum is 4.9073; for n =5 it is 10.1283; for n = 6 you should get 13.0839

iv) For practice, not to be graded: Do programming project 13 on page 178 (Note that there is a "Video Note" solution to this program here )


Lab 1: Due Tue, Jan 27 11:59 pm

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

Notes:

1) Starting with this lab, you must follow the guidelines on Program Style described in section 2.5 of the textbook. In particular, follow the style given in Display 2.16 (page 95) of the textbook. You will use points if you don't.

ii) For Project 7, all decimal point numbers must be printed with 2 digits after the decimal point. (e.g. 2.50 instead of 2.5). See pages 55-57 of the textbook for this.

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

I) Do Programming Project 7 (harder version) at the end of Chapter 2 (page 106) and the following problem

II) Write a C++ program which takes a 4-digit number (integer) as input from the user and prints the number in reverse order. For example, if the number entered is 5678 then the output should be 8765. The program should repeat this until the user decides to quit.

You need to pay attention to the following

1. Provide appropriate comments in your code
2. Use meaningful identifiers
3. Your program should give a warning message if the number (value) is not a 4-digit number. For example, 0345 is not considered to be a 4-digit number.
4. Your program should not be case-sensitive for the user choice i.e. both Q and q are should be acceptable quit.
5. You may assume that when asked to enter a 4-digit number, the user enters a number (as opposed to other kind of data, such as a character)

Here is a typical dialogue for this program:

Enter a 4-digit number
134
This is not a 4-digit number: Enter a 4-digit number
1234
The reverse number is 4321
Enter Q to quit, enter any letter to continue
C
Enter a 4-digit number
9001
The reverse number is 1009
Enter Q to quit, enter any letter to continue
Q
Thanks for using this program, bye !...

(Hint: If the number entered is n, what does n %10 give you? How about n/10? )

III (for practice, not to be submitted) Project 14 on page 108.


Lab 0: Due Tue, Jan 20 11:59 pm

i) Do the programming project 6 on page 37. Use the program in Display 1.8 (source code available). Do this for yourself, do not turn in

ii) Do the programming project 8 on page 38. Use the program in Display 1.8 as a model. Submit your program (save it in your class folder and do not modify after) by midnight Tue, Jan 20.