CSE 109 Test 2 Wednesday 29 November 2006 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>> 1. Consider the abstract data type of an ordered pair and label the elements of the pair x and y. We should be able to create an ordered pair, get the entries in the pair, and print it out. Write the template for the OrdPair class so that the following code will compile and produce the indicated output. OrdPair a(7,3.2); cout< using namespace std; template class OrdPair {public: OrdPair(const A &a, const B &b); A getX()const; B getY()const; template friend ostream & operator<<(ostream &out,const OrdPair & op); private: A x; B y; }; template OrdPair::OrdPair(const A&a, const B&b):x(a), y(b){} template A OrdPair::getX()const{return x;} template B OrdPair::getY()const {return y;} template ostream & operator<<(ostream &out,const OrdPair & op) {out<<"("<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 2. Write a program that takes two and only two command line arguments, the names of two input files of text, and displays on the screen the contents of the first file, followed by the contents of the second file. If the executable code for the program is stored in a.out, the call takes the form a.out inputFile1 inputFile2 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #include #include using namespace std; void check(bool b,char *mess) {if(!b) {cerr<<"ERROR: "< "); ifstream fila(arg[1]),filb(arg[2]); check(fila.good() && filb.good(), "Bad input file"); dump(fila); dump(filb); } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 3. Assume that the class BiNode below is used to construct binary trees. Write a function, div3(), that returns the number of entries in the tree that are divisible by 3. If we have BiNode *head; then the call to div3() would be div3(head). class BiNode {public: int data; BiNode *left,*right; }; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< int div3(BiNode *rt) {int temp; if(rt==NULL) return 0; else {if(rt->data%3==0) temp=1; else temp=0; return temp+div3(rt->left)+div3(rt->right); } } >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 4. Write both an ASM++ program and a MACH1 program that reads in a number and prints out 1 if it is between 1 and 10 inclusive and prints 0 otherwise. Recall the MACH1 operations READ(0), WRITE(1), LOAD(2), STORE(3), CLEAR(4), ADD(5), SUB(6), MUL(7), DIV(8), ADDIMMED(9), SUBIMMED(10), MULIMMED(11), DIVIMMED(12), JUMPLE(13), JUMPGE(14) HALT(15) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< read x jump (x<1) 100 jump (x>10) 100 write 1 halt 100 write 0 halt end 0 0 [0] 13 8 jump if x<=0 [1] 10 11 [2] 14 8 jump if x>=11 [3] 4 0 [4] 9 1 [5] 1 0 write 1 [6] 15 0 [7] 4 0 [8] 1 0 write 0 [9] 15 0 [10] E >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>