CSE 109 Test 2 SPRING 2006

CSE 109 Test 2 - CSE 109 Test 2 Wednesday 12 April 2006 >SUGGESTED ANSWERS< 1 Write a function strcopy that safely returns the copy of a string If

Info iconThis 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/05/2008 for the course CSE 109 taught by Professor Kay during the Spring '08 term at Lehigh University .

Page1 / 2

CSE 109 Test 2 - CSE 109 Test 2 Wednesday 12 April 2006 >SUGGESTED ANSWERS< 1 Write a function strcopy that safely returns the copy of a string If

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online