Programming Assignments for Math 118


Lab 11: Inheritence (Not to be graded)

Define classes that satisfy the following conditions and the hierarchy in the diagram given in the class.

A definition of an object, for a class in the bottom of the hierarchy, should produce a message consisting of the names of all the classes that directly or indirectly are related to the object.
A definition of an object for a class not in the bottom of the hierarchy should prompt the compiler to issue an error message.
Each defined object should be able to respond to the following functions.
talk-- A parameter-free function that produces a message from the object.
xivore-- A parameter-free function that produces one of "carnivore" or "herbivore" that reflects the kind of object in discourse.
Each of the classes is allowed to explicitly declare at most two member functions, with at most one of the two not being a constructor.
The body of each member function should be a single command of the form cout<<" ....";
A string containing the name of a class may appear only within members of the class. (That is, if A is a name of a class and x is a string "...A..." referring to A, then x may only be included within members of A)


Example: The program

#include ...

//missing definitions

int main( )

{

leopard a;

a.talk( );

a.xivore( );

return 0;

}

should produce an output of the following form.

animals carnivore cats leopard

leooo

carnivore


Lab 10

Part I: Due Midnight, Thursday Dec 6

Programming Projects 5 and 7 on pages 802-3. 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: Due Midnight, Monday Dec 10

Do Programming Project 8 on page 803.

Bonus (Due midnight Tuesday Dec 11). 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)


Assignment 9: Due midnight, Thursday Nov 29

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. 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 midnight, Thu Nov 15

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\Course Material\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 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 midnight Thu, Nov 8

Part I:

A)- For this part, first copy the file "gradefile.txt" which is available in our class folder (full path: P:\\Class\\Math\\Aydin\\118\\Course Material\\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 )
2. As you see, this file contains a number of test scores. Read every score 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. 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 pages 343-345 of the textbook).
3. Also output all the scores in descending order, and the median score both to the screen and to the file gradeout.cpp

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

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

Part II: Do Programming Project 18 on page 374-5 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 376. You will earn bonus points by creating good pictures of your instructor. You can directly write the svg file (you do not need to write the svg file through a C++ program)


Lab 6 Due midnight, Thu, Nov 1

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

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

Part 2 (due, Fri Nov 2 midnight): 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 Structs and Classes

Part I: Due: midnight, Monday, Oct 22

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.

Student data should be stored in a struct variable of the type StudentType, which has 3 components:
name (string), score (int, btw 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. 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

Part II: Due midnight, Thu Oct 25

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 Friday, Oct 6, midnight

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 Thursday, Sep 27, midnight

Do the program that is described here.


Lab 2: Due Monday, Sep 17, midnight

i) Do programming project 13 at the end of Chapter 3

ii) Do programming project 14 at the end of Chapter 3

iii) Write a program that takes a positive integer n from the user and computes the following quantity

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


Lab 1: Due Monday, Sep 10, midnight

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 (ass 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
Bye !...

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

Notes: i) Follow the guidelines about Program Style in 2.5

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.


Lab 0: Due Monday, Sep 3 midnight

Do Programming Project 7 and 8 at the end of Chapter 1 (page 37-38)