Winter 2018, Zemoudeh
CSE 202: CS II
School of Computer Science and Engineering
California State University, San Bernardino

Last modified: Mon, Jun 11, 2018

Fraction.cpp

Homework 4. Due Wed, Jun 6.
Write a program that decides whether a file contains balanced curly braces, parentheses, square brackets, and angled brackets. Input the file name, open it, and read one character at a time until end-of-file. If the file is balanced, your program should output "Balanced". If the file is not balanced, your program should output "Not Balanced". For example, the following files are balanced:

[()]{([])<>}

This is <((a))> {test} file
and [your] program should
[({o}ut)p{u}[t] ]<!>[] Balanced

#include <iostream>
using namespace std;
main()
{
    int a[100];
    a[20] = ((13 / 2) + a[5]) / 2;
    cout << a[20];
    cin >> a[20];
}

And the following files are not balanced:

This is { ( not ) 
a <balanced> file

neither is this
< > ] ( ) [ < >

{ ( not balanced } )

Use a stack of characters.

stack<char> brackets;
When an open bracket is read, it's pushed on stack. When a close bracket is read and its corresponding open bracket is on top of stack, the open bracket is popped.

Hand in a printout of your program and typescript of sample runs for each of the above input files.



Midterm Wed, May 16.

Homework 3. Due Wed, May 9.
Design class Person with private fields name and social security number, and a public virtual member function print().
Derive classes Employee and Student from Person. An employee has a private field salary and a student has a private field grade in addition to the data fields in person.
Derive classes Professor and Secretary from Employee. A professor has a private field research area and a secretary has a private field department he/she works for in addition to the data fields in employee.

Every class has to have a print() function to print its own data fields and those of its base class.

Make sure the following main() function works correctly with your classes.

main()
{
    vector<Person *> persons;

    persons.push_back(new Person("Dave"));
    persons.push_back(new Person("Jack", "123-45-6789"));
    persons.push_back(new Employee("Sam", "111-22-3333", 50'000));
    persons.push_back(new Professor("Kay", "444-33-2222", 70'000, "Parallel Computing"));
    persons.push_back(new Professor("Joy", "555-55-5555", 1'000, "AI"));
    persons.push_back(new Secretary("Misty", "999-99-9999", 900'000));
    persons.push_back(new Student("Ace Prog", "654-32-3456", "B"));

    for (int i = 0; i < persons.size(); i++)
        persons[i]->print();

    for (int i = 0; i < persons.size(); i++)
        delete persons[i];
}
Hand in a printout of your program and typescript of its compile and run.

Clock and TravelClock with virtual methods illustrate polymorphism and dynamic binding

Clock and TravelClock example illustrates inheritance

Homework 2. Due Mon, Apr 30.
Add owner name owner and a pointer to a linked account attached_account to class Account from lab 1.
class Account {
private:
    string owner;
    double balance;
    double interest_rate; // in percent
    Account * attached_account;
public:
    ...
};
In main() read in a list of accounts from Account_list and populate a vector of pointers to accounts.
vector<Account *> all_accounts;
Use attached_account pointer to link accounts with the same owner name. We assume a person can only have up to two accounts.
Finally, generate a report such that if an owner has two accounts, both accounts are listed on the same line. Here is one possible output for the above input file.
Washington 10000.00 5.00%
Jefferson   2000.00 4.50% SECOND ACCOUNT: 45000.21 3.25%
Hamilton   30000.50 6.00% SECOND ACCOUNT:  2000.00 4.50%
Franklin   25000.00 7.20% SECOND ACCOUNT: 21200.33 6.00%
Jefferson  45000.21 3.25% SECOND ACCOUNT:  2000.00 4.50%
Franklin   21200.33 6.00% SECOND ACCOUNT: 25000.00 7.20%
Adam       15000.00 4.50%
Hamilton    2000.00 4.50% SECOND ACCOUNT: 30000.50 6.00%
Note the info for people with two accounts is printed twice. We will fix this in an enhancement later.

Don't forget to clean up (delete) all dynamically allocated memory!

Use this find() function to find matching accounts.

Hand in a printout of your program and typescript of compile and run of the program.



Exercise P7.2 on vector<Person *> and vector<Car *>

Homework 1. Due Wed, Apr 18.
Design class Student to contain student name and birthday.
class Student
{
private:
    string name;
    string month;
    string day;
    string year;
public:
    ...
};

Design class Roster to contain a vector of students.
class Roster
{
private:
    vector<Student> students;
public:
    ...
};

In main() read a set of name and birthdays from the file students.in (until end-of-file) and populate students vector.
Then iterate over the vector and print all students with current month (Apr) birthday.

Hand in a printout of your program and typescript of its compile and run.



Staff Program

Country Program

To be able to just type a.out to run a program (instead of ./a.out), add the two characters :. to the end of $PATH line in the file .bash_profile in your home directory.
In Linux . (dot) stands for current directory, and this adds current directory to search path. Now when you run a program, such as when you type a.out, the system will also look in the current directory and will find it.
.bash_profile runs when you first login, so this effect kicks in the next time you login. If you are too impatient and want to see the effect right away, manually run the file:
$ . .bash_profile


Add the following lines to .vimrc file in your home directory to automatically convert tabs to four spaces in vi (vim).
If .vimrc doesn't exist, create it.

" Only do this part when compiled with support for autocommands.
if has("autocmd")
    " Use filetype detection and file-based automatic indenting.
    filetype plugin indent on

    " Use actual tab chars in Makefiles.
    autocmd FileType make set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
endif

" For everything else, use a tab width of 4 space chars.
set tabstop=4       " The width of a TAB is set to 4.
                    " Still it is a \t. It is just that
                    " Vim will interpret it to be having
                    " a width of 4.
set shiftwidth=4    " Indents will have a width of 4.
set softtabstop=4   " Sets the number of columns for a TAB.
set expandtab       " Expand TABs to spaces.



Please read Ch. 5 and 6 in the text book. Topics covered in these chapters are prerequisites for CSE 202. Specifically you need to read, understand, and effectively use

Tutorials on PuTTY and WinSCP.
The first one illustrates how to login remotely on one of the lab machines.
The second one illustrates how to transfer files from your computer to the lab machines and vice versa.

The CSE Club is a great resource for all CSE students. You can gather a lot of useful information from your peers who have gone through the same courses earlier. The club usually has Linux and other tutorials at the beginning and through out the quarter.
Become a member and attend every event.

Follow the course Coding Style in all of your programming assignments and lab works.

LABORATORIES

Lab 1: Classes
Lab 2: Vectors
Lab 3: Arrays and Pointers
Lab 4: Classes, Vectors, and Pointers
Lab 5: Inheritance and UML
Lab 6: Streams
Lab 7: Recursion
Lab 8: Sorting and Searching
Lab 9: Stacks, Templates, and Adapters
Lab 10: Maps

Use this Lab Report Format to submit your reports.

You have one week, after first attending a lab, to hand in the Lab Report and email the source code to Mr. Thakshak Gudimetla (thakshag@coyote.csusb.edu) who is the course TA and lab assistant.

Email subject line must be "Last name, First name, CSE 202, Lab #, Monday/Wednesday". Either specify Monday or Wednesday depending on which lab you are registered in.

EMAIL the source code. HAND IN a printout of lab report.

LATE LAB REPORTS WILL NOT BE ACCEPTED.



Syllabus