c109su00f

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

• Notes
• nizhes
• 8

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

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

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

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){
This is the end of the preview. Sign up to access the rest of the document.
• Spring '08
• Kay
• new String, balanced binary tree, old string

{[ snackBarMessage ]}

What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern