c109s00f - CSc 109 Final Examination 4 PM Sunday 14 May...

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

CSc 109 Final Examination 4 PM Sunday 14 May 2000 PL 455 >>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<< 1. (20 pts) In class I presented an algorithm for converting a string containing an expression in infix form to a string containing an expression in postfix form. The algorithm involves the input string, a stack, and the output string. For the string "2+3+4*5>6*3+2", considering each character as a token and assuming the C++ precedence for operators, in the table below show the status of the stack and the output string after each token is read. I have provided the list of tokens read. Token Stack Output String ------------------------------------------------------------------ 2 | ) | 2 ------------------------------------------------------------------ + | ) + | 2 ------------------------------------------------------------------ 3 | ) + | 2 3 ------------------------------------------------------------------ + | ) + | 2 3 + ------------------------------------------------------------------ 4 | ) + | 2 3 + 4 ------------------------------------------------------------------ * | ) + * | 2 3 + 4 ------------------------------------------------------------------ 5 | ) + * | 2 3 + 4 5 ------------------------------------------------------------------ > | ) > | 2 3 + 4 5 * + ------------------------------------------------------------------ 6 | ) | 2 3 + 4 5 * + 6 ------------------------------------------------------------------ * | ) * | 2 3 + 4 5 * + 6 ------------------------------------------------------------------ 3 | ) * | 2 3 + 4 5 * + 6 3 ------------------------------------------------------------------ + | ) + | 2 3 + 4 5 * + 6 3 * ------------------------------------------------------------------ 2 | ) > + | 2 3 + 4 5 * + 6 3 * 2 ------------------------------------------------------------------ ) | | 2 3 + 4 5 * + 6 3 * 2 + > ------------------------------------------------------------------ 2. (15 pts) Write Simpletron code for the C++ program below, assuming a Simpletron machine with 100 words of memory. void main(){ for(int i=1; i<=10; i++) cout<<(i*i); } 2012 3312 2113 1113 2012 3010 2112 3111 4100
Image of page 1

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

4300 0001 0011 0001 END 3. (15 pts) Write SimPL code for the following C++ program. void main(){ int s=0; for(int i=1; i<=10; i++) s+=i*i; cout<<s; } 100 let i=1 110 let z=10 120 let s=0 130 let s=s+i*i 140 let i=i+1 150 if i<=z goto 130 160 print s 170 stop END 4. (30 pts) We did not use the C++ function strtok() for parsing SimPL programs because it gobbles up the delimiter(s) specified when calling it. If we had insisted on using tokstr(), we could have processed each line by adding spaces in appropriate places (e.g., change "10 let x=(4+y)*3" to "10 let x = ( 4 + y ) * 3"), and then fed the resulting string to tokstr(), using ' ' as the delimiter. Write a function spaceOut() which takes a char pointer as an argument and returns a pointer to a string. It is assumed that the original string obeys the syntax for a line of SimPL code. The string returned should have the symbols of the original string, with spaces added between all the tokens of the original string. Note, by the way,
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '08
  • Kay
  • ASCII, return key, class rek, int t,int sc,char

{[ 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