Winter 2008, Zemoudeh
CSCI 201: Computer Science I
Computer Science Department
California State University, San Bernardino


Last modified: Monday, March 16, 2008

Solution to Homework 7: client.cpp.

Solution to Homework 6: address.cpp.

Review and understand every detail of fligth.cpp program for the final.

Homework 7: A marketing firm wants to maintain a database of its client companies. Each client info consists of client number, client name, date client first hired the firm, and amount paid by the client for the services rendered by the firm. The main application for this database is to print the list of clients (including all of their info) sorted by hired date and also separately by amount paid. Implement a class Client to contain the above info for a client. Use a class Date in Client to represent date hired. Client should at least have two constructors (one being the default constructor), a print() member function, and two member functions that compare clients based on date hired and amount paid:
	bool Client::greater_by_date(Client x);
	bool Client::greater_by_amount(Client x);
Here is how greater_by_amount() could be defined:
	bool Client::greater_by_amount(Client x)
	{
		if (amount > x.amount)
			return true;
		return false;
	}
The main() function should declare a vector of Clients, populate it from an input file, sort the vector once based on amount paid and print it out, and once by date hired and print it out. Use the following input file exactly as it is. Here is your chance to cut-and-paste without feeling guilty!
12001 IBM
01 02 1990 300000
12002 Microsoft
05 10 1980 400000
12003 Bank of America
03 10 1985  50000
12004 General Motors
10 30 2000 500000
12005 Google
12 20 2005 300000
12006 Manchester United FC
03 20 1985  40000
12006 HP
01 01 1990 400000
Here is a snippet of main():
	vector < Client > list;

	cin >> number;
	getline(cin, name);
	cin >> mon >> day >> year >> amount;
	getline(cin, rest);
	while (!cin.eof()) {
		list.push_back(Client(number, name, Date(mon, day, year), amount);
		cin >> number;
		getline(cin, name);
		cin >> mon >> day >> year >> amount;
		getline(cin, rest);
	}
Hand in a print out of the program and a print out of the output for the above input file.
Due: Monday, March 17, 2008. No late submission for this assignment.

Homework 6: Implement a class Address. An address has a street number, street name, an optional apartment number, a city, a state, and a zip code. Supply two constructors: one with an apartment number and one without. Supply a print() function that prints the address with the street address on one line and the city, state, and zip code on the next line. Supply a member function comes_before() that returns true if one address comes before another. One address comes before another address if the addresses have the same street name but the first one has a smaller street number.
Supply a main() to thoroughly test the correctness of your class. Your main() should include but should not be limited to:
Address addr1(5400, string("Northpark Blvd"), string("SB"), string("CA"), 92407),
        addr2(5440, string("Northpark Blvd"), string("SB"), string("CA"), 92407);

if (addr1.comes_before(addr2))
	cout << "Address 1 comes before address 2.\n";
else
	cout << "Address 1 does not come before address 2.\n";
You may also construct an address based on user input as follows. getline() only works for strings, so use cin >> for integers. But then you need to use getline() right after cin >> to read in the rest of the line so that the next getline() would work properly:
int number, zip;
string street, city, state;
string rest_of_line;

cout << "Enter street number, street, city, state and zip.\n";
cin >> number;
getline(cin, rest_of_line);
getline(cin, street);
getline(cin, city);
getline(cin, state);
cin >> zip;
getline(cin, rest_of_line);

Address addr1(number, street, city, state, zip);
As an alternative to mixing cin >> and getline(), you may use a string to input a number with getline() and then cast it into an integer:
int number, zip;
string street, city, state;
string line;

cout << "Enter street number, street, city, state and zip.\n";
getline(cin, line);
number = atoi(line.c_str());
getline(cin, street);
getline(cin, city);
getline(cin, state);
getline(cin, line);
zip = atoi(line.c_str());

Address addr1(number, street, city, state, zip);
Hand in a printout of the program nicely documented and formatted.
Due: Wednesday, March 12, 2008.

Read Ch. 6 & 7: Object Oriented Programming and Classes.

Modified sales-commission program.

Sales-commission program.

Read Ch. 4: Functions.

Midterm: Monday, Feb 25.

Homework 5: Modify the Sales Commission program given in class to accept 6-digit rep ids. The rep ids range from 100,000 to 999,999. This implies that a 100-element vector doesn't work anymore. Also, you shouldn't use a 1,000,000-element vector either! Declare two parallel vectors as follows:
vector< int > rep_id;
vector< float > sales;
then use the push_back() function to append each pair of rep id and sales amount to the vectors:
while (cin >> id >> amount)
{
	rep_id.push_back(id);
	sales.push_back(amount);
}
Since the input file can have multiple sales entries for the same sales person, the two vectors have to be sorted based on rep ids. This implies in the process of sorting whenever two rep ids are swapped, the corresponding sales amounts have to be swapped too. For input file
200200   4000
100100   5500
300300   2000
200200   1800
The sorted vectors should look like

0 100100 0 5500
1 200200 1 4000
2 200200 2 1800
3 300300 3 2000

Now in producing the commission report, you must pay attention to consecutive rows with the same rep id and combine their sales amount. In this case the sales amount for rep id 200200 would be 5800.
Hand in a print out of the program and the output file for the following input file:
200200   4000
100100   5500
300300   2000
200200   1800
555555    100
100000    200
666666   1000
777777   2000
555555    100
555555    300
666666   4000
555555    600
Due: Monday, Feb 18.

Solution to Homework 4.

Start reading Chapter 3.

The errata list for the text book is here.

Homework 4: Write a C++ program for the algorithm developed for problem 2.11 (Homework 2).
Make sure the input year is valid; any year less than 1997 is illegal. Continue to calculate the population growth while the user wants to continue. This can be done as follows:
	...
	string continue = "yes";
	while (continue == "yes" or continue == "Yes" or continue == "YES") {

		// algorithm from homework 2 converted to C++

		cout << "Do you want to continue? ";
		cin >> continue;
	}
	...
Don't forget to document your program. You will be graded on correctness, documentation, indentation, and simplicity.
Due: Wednesday, Feb. 6. Type in, compile, and run your program to test its correctness. Hand in a printout of the program and the typescript of a sample run.

Homework 3: Modify the largest.cpp program given in class to find the largest and smallest number in an input file. The output of your program should be two numbers: the largest number and the smallest number. Assume the numbers in the file are in the range -9999 to 9999. As an example, if the input file contains:

20
-5
100
3
-20
2


The output of your program should be:

Largest = 100
Smallest = -20


Don't forget to document your program. You will be graded on correctness, documentation, indentation, and simplicity.
Due: Monday, Feb. 4. Type in, compile, and run your program for multiple input files to test its correctness. Hand in a printout of the program. No hand written documents!

Homework 2: Give an ALGORITHM for problem 2.11 (75), you don't need to give a program. Due Wednesday Jan 23.
Finish reading Chapter 2.

Homework 1: Problem 1.4 (27). Due Wednesday Jan 16.

The Beauty of Math!.

The pi.cpp program.

If you wish to work from home, download SSH from here. More specifically under the same web page, if you want the Microsoft Windows SSH client, you need to download the file with exe extension from this location.
The hostname is jbh3-1.csci.csusb.edu
Use your userid and passwd to login on jbh3-1. Then login on one of the machines in the lab:
$ ssh jb359-13

Syllabus

Labs:

  • Lab 1
  • Lab 2
  • Lab 3
  • Lab 4
  • Lab 5
  • Lab 6
  • Lab 7
  • Lab 8
  • Lab 9
  • Lab 10