c109s06t2 - CSE 109 Test 2 Wednesday 12 April 2006...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 109 Test 2 Wednesday 12 April 2006 >>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 1. Write a function strcopy() that safely returns the copy of a string. If we have declared char *f,ch[]="A string"; then a call to strcopy() might be f=strcopy(ch); >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> char * strcopy(char *ch) {char *temp; if(ch==NULL) return NULL; temp=new char[strlen(ch)+1]; if(temp==NULL) {cerr<<"Error: Heap overflow\n"; exit(1); } strcpy(temp,ch); return temp; } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 2. Write MACH1 code that would correspond to the MCASM program below. read x jump neq two write x two halt end Hint: Recall the mach1 code: read(0), write(1), load(2), store(3), clear(4), add(5), sub(6), mult(7), div(8), addi(9), subi(10), multi(11), divi(12), lte(13), gte(14), halt(15). >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 0 0 [0] read; no need to store x for this program 13 3 [1] if lte skip next line 14 6 [2] must be > so jump to two 14 5 [3] if gte skip next line 13 6 [4] must be < so jump to two 1 0 [5] write 15 0 [6] this is "two" E Note: A solution that stores the value in x and retrieves it is okay. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
< 3. Write a program that writes out the first line of every file that is both listed on the command line and exists.
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern