Assignment 14: 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 defiition 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



Assignment 13: RECURSION

Part II: Due Friday, 12/12, 10 pm

Programming project 5 on page 581.

Part I: Due Wed, 12/10, 9:30 am

Do programming project 3 on page 581 (at the end of Chapter 13) of the textbook.


Assignment 12: Separate Compilation and NameSpaces

DUE WED DEC 3, 10 pm

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 "Lab12Main.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.


Assignment 11: String Processing

Due Tuesday, Nov 18, 9:30 am

Do programming projects 1 and 2 on page 397 of the textbook.


Assignment 10: Operator Overloading

Due Tuesday, Nov 11, 9:30 am.

Part I: Programming Project 2 on page 346 of the textbook.

Part II: In this part 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 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 -.
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 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 these 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.

Sort these complex numbers in descending order according to their moduli (moduli is the plural of modulus).


Assignment 9: File I/O: Due Tuesday, Nov 4, 9:30 am

Part II: Do programming project 10 in Chapter 12 (page 543) with the file "Lab9Part2.txt" located in the class folder. In addition to the average word length, your program should also report the number of characters other than blanks, commas, periods, question marks and the end of line character as well as number of words. Make a copy of the file Lab9Part2.txt in your folder (with the same name) and do not modify it in any way. Also test your program with smaller files to make sure that it works correctly. The most challenging part of the assignment is to decide when a word has been read.

Part I: For this part, first copy the file "gradefile.cpp" which is available in our class folder (full path: P:\\class\\math\\aydin\\Math118\\gradefile.cpp) 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.cpp (from your own folder) and copy its content EXACTLY to another file in your folder. Call this output file gradeout.cpp. 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.cpp. 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.cpp.

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 513-515 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


Assignment 8: Classes and Vectors: Due Tuesday, 28, 9:30 am.

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 7. 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.


Assignment 7: Due Thursday, Oct 23, 9:30 am

Part I: Implement Lab 6 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 page 265)

Part II: Do Programming project 3 in chapter 6 (page 255-256)


Assignment 6: Due Thursday, Oct 16, 9:30 am

Look at here.


Assignment 5: Due Tuesday, Oct 7, 9:30 am

Part I: Make necessary changes to to the sorting program 5.8 (pages 201-203 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)


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 intgers. Be sure to use a 2 dimensional array to store the input.


Assignment 4: Due Thursday, Oct 2, 9:30 am

Programming projects 3 and 4 on pages 168 and 169 of the textbook (Chapter 4).

Hint for 3: First use as many quarters as possible, then as many dimes as possible then as many pennies as needed.


Assignment 3: Two parts. Both are due Tuesday, Sep 30, 9:30 am

Part I: Programming project 5 in chapter 3 (page 131)

Part II: Here


Assignment 2: See this page.


Assignment 1: Do Programming Projects 2 and 6 in Chapter 1.

Due Tuesday 9/9/03, 9:30 am.

A typical run is as follows:

Enter the weight of mouse in grams

15

Enter the lethal dose for the mouse in grams

100

Enter the desired weight of the dieter in grams

45400

For these parameters:

mouse weight: 15 grams

lethal dose for the mouse: 100 grams

Dieter weight: 45400 grams

The lethal dose in grams of sweetener is: 302667

Lethal number of cans of pop: 864762