-
P1
-
P1_1: exc 2 pp 38
-
P1_2: exc 4 pp 44
-
P1_3: exc 36 pp 56
-
P2
-
P2_1: exc 2.4 pp 94
-
P2_2:
Write a C++ program (1 file) for the following:
The program will ask the number of students taking 1 test each. Then
it will
allocate anough storage (heap) to read that many names (w/o
spaces - use C_++ string for one name), and to read that many grades
(integers).
The input format is as in this example below.
The program will compute letter grade for every student (use our grade
scale, no -/+)
The program will compute class average
The program will display output the following format
You must duplicate exactly the format as in the example, including
precision and justification.
How many students: 2
Give name of student 1: John Doe
Give grade for John: 95
Give name of student 2: Susan Dof
Give grade for Susan: 92
NAME
SCORE GRADE
John Doe
95 A
Susan Dof
92 A
Class average: 93.50
-
P3
-
P3_1:
Write a program with overloaded function for computing the average
of integers, or appending characters (same name average
for both functions). The integer unction should allow between 1 and 5 arguments,
the character function between 0 and 5 - all these should be handled with
default arguments. Test in an application, program looping until
the user says no more (end of the file). Every time through the loop, the
program will ask whether integers or characters are being given and how
many (1 to 5 or 0 to 5), check the entry, and then will ask for the proper
number of inputs separated by spaces. You may assume correct data
type and a match beween the number of arguments stated and actually supplied.
For characters, the function should produce a dynamically allocated NTS
string by appending all the characters, returned by a pointer. Make sure
not to leak memory. For integers, the average will be produced (as a float),
returned by copy. If the call is made with no arguments, an exception message
(string) should be thrown and handled
by catching it, displaying "Error:
"
followed by the actual message thrown, and going on for another iteration
of the loop.
Note: there will be different function calls for different number
of arguments, thus for example if the user enters 2 characters
a
and b, the call should be average(a,b)
and if the user entered no arguments the call should be average().
Make sure to put the functions in fun.cpp
(with
fun.h)
and the application in app.cpp
For example, this is how the program should run (follow closely, user
input in italic):
Welcome to my average program...
What data you have and how many: 1=integers
2=chars: 1 2
Enter 2 integers: 10 20
The result is 15
What data you have and how many: 1=integers
2=chars: 2 2
Enter 2 characters: a b
The result is ab
What data you have and how many: 1=integers
2=chars: ^D
Thank you.
Note: all projects with classes must be in multiple files, declaration
and implementation file per each class, plus the application main file
-
P3_2: exercise 3.12 pp 170. Create a set should be done with your constructor
of no arguments. Assume only up to 10 elements per set.
Try with an application program, but we will provide our own for testing
your class. Support all the operations as listed, with the names as listed.
Any operation that produces a set with more than 10 elements, or attempting
to remove a non-existing element, or adding an existing element, should
throw a string exception with an
appropriate message.
Make sure to use these interfaces/constraints:
1. Only default constructor.
2. addElement(...)
3. remElement(...)
4. setEnumarate(...)
5. setIntersect(...)
6. setUnion(...)
7. setDifference(...)
Also, make sure:
a) 2,3,4 above are void methods (enumerate prints
the element to the screen)
b) 5,6,7 return the new set by copy and
do not modify the arguments
c) sets and integers can be accepted by reference
or copy but no pointers
-
P4:
-
P4_1: exercise 4.2 pp 226
-
P4_2: Implement a stack of int class, called IntStack. Then derive a stack
of int allowing peek, called IntStackWPeek. Test by yourself but submit
only the two classes (total of 4 files). Name the files correctly. We will
test them in our own application.
IntStack interface:
void push(int);
int pop(void);
bool isFull(void);
bool isEmpty(void);
IntStack(void); // default size 10
IntStack(int); // allocate this size stack
IntStack(const IntStack&); // copy
~IntStack(void);
IntStackWPeek interface:
// inherit interface from Stack except that disable
isFull()/isEmpty() methods
int peek(void); // return the top element
IntStackWPeek(void); // default size 5
IntStackWPeek(int); // allocate this size stack
IntStackWPeek(const IntStackWPeek&); // copy
~IntStackWPeek(void);
Also, any error condition should throw a string exception (C++ string).
For example, pop() on empty would raise the error. The error message should
say what caused the problem.
-
P5:
-
We have cars and people.
-
Create your own class MyString, which would have constructors
MyString(); // allocate object and empty char * string
MyString(const char*);
MyString would use dynamic memory allocation of char array just for
the size needed. Remember about destructor and copy/assignment. (-25% if
you use C++ string)
Assignment should reuse heap object if appropriate (need two counters!)
-
Person has name (MyString) and age (int).
Provide constructors
Person(const char * name, int age)
Person() // read all data from the keyboard
Name of the person should be MyString embedded object
Have a class variable to keep the number of people and a class method
showNumPeople() to print how many people we have. Implement operators:
unary ++ both forms to increment age by 1.
conversion from and to int (from int would create a new person with
"NoName" and age equal to that integer, cast to integer would return the
age).
I/0 << and >> to print/read name and age to/from the standard
devices
Provide copy and destructor to update the class counter
-
Car has make, model (both MyString) and year (int).
Provide constructor
Car(const char *make, const char *model, int year)
Use MyString as embedded objects
Provide show() method to display all car info
-
At this moment test your program with some people and some cars
-
Now extend the program:
-
make Car abstract
-
inherit NewCar and UsedCar with extra float price and int mileage, respectively.
-
ensure that both subclasses have to implement show()
-
extend Person to own a number of cars. Extend Person constructors to ask
how many cars the person owns, alloacate that big array and then read the
cars: for each car, first ask whether it is new or used and then create
it.
-
write an application in main.c that will have john and susan Person objects,
each created with some cars. Then, the application will display info about
john and susan, including their cars. All information should be displayed.
-
P6: (worth 150%)
-
P6_1
-
Write a client application to determine the timing of various operations
on vector, deque,
and list. Use random integer data
(random in the range 1-100).
-
The application should time the 3 containers for the number of operations
entered as a command line argument(and 10,000 by default if the argument
not given). The results should be displayed to the screen in a tabular
format as Figure 7.3.5 except that:
-
entries will be actual times in miliseconds
-
there will be 8 data rows, by separating Insert from Erase.
-
To time insertions, simply keep inserting elements up to the number
-
To test access, use the containers generated above in insertions (make
sure to reset time)
-
To time deletions, use the same containers generated in insertions (make
sure to reset time)
-
Timing - see Exercise 7.10 pp 398 for ideas
-
Random generations - see rand()
in cstdlib
-
P6_2
-
Write a program to read a text file with employees, put the employees into
a container (your choice but your choice should be most efficient - you
must
explain your choice in the ID file), and display them different ways.
-
The input file is a text file, its name is given as a command line argument.
The program should display error message if the file is not specified or
cannot be open. The file contains lines of text data, each line represents
info about one employee as follow:
firstName lastName age salary \n
You may assume everyone has all the data as string, string, integer,
floating. The input file is not sorted.
-
Use a container containing Person
objects (or pointers if you prefer), which gets new person added at the
end for every line read from the file
-
After the container is constructed (upon EOF)
-
display the data from the first to the last, using
a) overloaded operator << for Person
b) applying a printing method with for_each()
STL algorithm
-
sort the data on last name, and then display again but using only one of
the above a/b