c109su00f - CSc 109 Summer Final Examination 7 July...

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

View Full Document Right Arrow Icon
CSc 109 Summer Final Examination 7 July 2000 _________________SUGGESTED ANSWERS_____________________________ 1. (15 pts) Write a NewSimPL program which reads in two integers, which are assumed to be positive, and writes out the highest power to to which the second can be raised and still divide the first. For example, if the numbers are 24 and 2, the answer is 3 (because 2^3 divides 24, but 2^4 does not). For another example, if the numbers are 24 and 5, the answer is 0. (Hint: The NewSimPL language has instructions like halt, if .... , etc.) read x read y count=0 pow=y &2 rem=x-x/pow*pow if rem>0 goto &1 count=count+1 pow=pow*y if 0==0 goto &2 &1 write count halt END 24 2 2. (15 pts) Translate the following NewSimPL program into New Simpletron code. read x if x==0 goto &200 x=42 &200 write x halt 2000 ;this is the output when the above programs is 21 ;fed to NewSimPL 1021 5019 9011 1019 5021 9011 5000 8015 1020 999 1999 21 1021 3000 5000 8000 0 42 3. (30 pts) Write a function "center" which takes a string and an integer, n, as arguments and returns a new string consisting of the old string, centered: if the string has fewer than n characters, it appears
Image of page 1

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

View Full Document Right Arrow Icon
in the new string surrounded by blanks, roughly n/2 blanks before the old string and roughly n/2 blanks after the old string. If the string has more than n characters, only the middle n characters appear in the new string. For example, if we have char s[]="hello there" then the call center(s,7) would return the string "llo the" and the call center(s,15) would return the string " hello there " char * center(char * old,int n){ char *temp; int length,left; if(n<=0 || old==NULL) return NULL; temp=new char(n+1); length=strlen(old); if(length>n){
Image of page 2
Image of page 3
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