CSE 109 Test 2 Wednesday 9 April 2003 <><><><><><><><><>SUGGESTED ANSWERS<><><><><><><><><>><><><><><> 1. An Association is a pair consisting of a "key" and a "value." Write the template class for Association so that the following code compiles and produces the output indicated. The class need have no other functionality. Association a(2,2.7); Association b("hope","esperanza"); a.setValue(2.3); cout< template class Association {public: Association(const KEY &k,const VALUE &v); void setValue(const VALUE &v); template friend ostream & operator <<(ostream &out,const Association & a); private: KEY key; VALUE value; }; template Association::Association(const KEY &k,const VALUE &v) :key(k),value(v){} template void Association::setValue(const VALUE &v) {value=v;} template ostream &operator <<(ostream &out,const Association &a) {out<<"{"<='0' && ch<='9') return DIGIT; else return JUNK; } } 3. Recalling the operations 0(add), 1(sub), 2(mul), 3(div), 4(cjump), 5(move), 6(push), 7(pop), assemble the following asm3 program, that is, write the op3 code which corresponds to the given asm3 code. mov x, mem0 5010000000 add z, x, y 4010005 cjump v,v,v 4006006006 z var 0 ; any number will do y const 16 20 v mov mem0,z 5000004000 cjump mem0, mem0, mem0 4000000000 x var E ; no number needed end 2 3 2 3 4. Write a function "allEqual()" for which the following code returns true if and only if the strings a, b, and c each contain the same characters. The code should be resistant to crashing. char *a, *b, *c; //...... intervening code return allEqual(a,b,c); ==================================================================== bool allEqual(char *a, char *b, char *c) {return a==NULL && b==NULL && c==NULL || a!=NULL && b!=NULL && c!=NULL && strcmp(a,b)==0 && strcmp(b,c)==0; }