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

Info iconThis 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 count=count+1 pow=pow*y halt END 24 2 2. (15 pts) Translate the following NewSimPL program into New Simpletron code. read x x=42 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
Background image of page 1

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

View Full DocumentRight 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){ left=(length-n)/2; for(int j=0;j<n;j++){ temp[j]=old[left]; left++; } temp[n]='\0'; } else{ left=(n-length)/2; for(int j=0;j<left;j++) temp[j]=' '; temp[left]='\0'; strcat(temp,old); for(int j=length+left;j<n;j++) temp[j]=' '; temp[n]='\0'; } return temp; } 4. (15 pts) Fill in the table for the tree below, where the node is identified by its number. 1
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

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

Page1 / 8

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

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

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