% denotes an ordinary comment within a Turing program. %% denotes a comment to students that would not ordinarily appear in a program. Part A ====== 1. No answer needed. 2. % Prompt for, read, and copy a string. % Output both original string and the copy. var inputString: string var copy: string put "Enter a string. " .. %% just 2 dots, not 3 get inputString %% copy needs an initial value. It needs to already have a %% value the first time the right-hand side of the assignment %% statement in the 4th line below this one is reached. % Copy input string by appending one character at a time. copy := "" for i: 1 .. length(inputString) copy := copy + inputString(i) end for %% With good spacing, all 3 strings apearing on screen %% (input, original out, copied out) will line up. put "Original string is ", "\"", inputString, "\"" put "Copied string is ", "\"", copy, "\"" Sample runs: Enter a string. "This is a sample string." Original string is "This is a sample string." Copied string is "This is a sample string." Enter a string. "A stitch in time saves nine." Original string is "A stitch in time saves nine." Copied string is "A stitch in time saves nine." 3.a % Copy an input string, changing any "a" to "A". % Output original string and copy. var inputString: string var copy: string put "Enter a string. " .. get inputString % Make a copy by appending a character at a time. copy := "" for i: 1 .. length(inputString) % Replace "a" with "A" but otherwise make no changes if inputString(i) = "a" then copy := copy + "A" else copy := copy + inputString(i) end if end for put "Original string is ", "\"", inputString, "\"" put "Copied string is ", "\"", copy, "\"" Sample runs: Enter a string. "aaabbbcccaaa" Original string is "aaabbbcccaaa" Copied string is "AAAbbbcccAAA" Enter a string. "Aladin's Lamp" Original string is "Aladin's Lamp" Copied string is "AlAdin's LAmp" 3.b. % Copy an input string backwards. % Output original string and copy. var inputString: string var copy: string put "Enter a string. " .. get inputString % make a copy by appending a character at a time copy := "" for i: 1 .. length(inputString) % To make a backwards copy, % append to the left instead of the right. copy := inputString(i) + copy end for %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Another method: %% traverse the input string from right to left. %% %% for decreasing i: length(inputString) .. 1 %% copy := copy + inputString(i) %% end for %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% put "Original string is ", "\"", inputString, "\"" put "Copied string is ", "\"", copy, "\"" Sample runs: Enter a string. "abcdef" Original string is "abcdef" Copied string is "fedcba" Enter a string. "able was I ere I saw elba" Original string is "able was I ere I saw elba" Copied string is "able was I ere I saw elba" 3.c % Copy an input string, but with copy having no repeated characters. % Output original string and copy. var inputString: string var copy: string put "Enter a string. " .. get inputString % Make a copy by appending a character at a time. copy := "" for i: 1 .. length(inputString) % Append the character only if it is not % already part of the copy made so far. if index(copy, inputString(i)) = 0 then copy := copy + inputString(i) end if end for put "Original string is ", "\"", inputString, "\"" put "Copied string is ", "\"", copy, "\"" Sample runs: Enter a string. "aaabbbcccdddeee" Original string is "aaabbbcccdddeee" Copied string is "abcde" Enter a string. "Double double toil and trouble" Original string is "Double double toil and trouble" Copied string is "Double dtianr" Enter a string. "one, two, three, four" Original string is "one, two, three, four" Copied string is "one, twhrfu" 3.d % Copy an input string, changing small letters to capitals % but changing nothing else. const smallLetters := "abcdefghijklmnopqrstuvwxyz" const largeLetters := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" var inputString: string var copy: string put "Enter a string. " .. get inputString % make a copy by appending a character at a time copy := "" for i: 1 .. length(inputString) % Replace small letters with capitals. %% If the character is a small letter, it will appear %% in the smallLetters string. In that case, substitute %% the corresponding character from the largeLetter string. %% The "corresponding" letter is the one that appears in the %% same position (position "n"; see below); const n := index(smallLetters, inputString(i)) if n not= 0 then %% i.e. inputString(i) is a small letter. copy := copy + largeLetters(n) else copy := copy + inputString(i) end if end for %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Another solution: using ord and chr %% % make a copy by appending a character at a time %% copy := "" %% for i: 1 .. length(inputString) %% const n := ord("A") - ord("a") %% const m := ord(inputString(i)) %% if m >= ord("a") and m <= ord("z") then %% copy := copy + chr(m + n) %% else %% copy := copy + inputString(i) %% end if %% end for %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% put "Original string is ", "\"", inputString, "\"" put "Copied string is ", "\"", copy, "\"" Sample run: Enter a string. "aaaBBBcccDDDeee123" Original string is "aaaBBBcccDDDeee123" Copied string is "AAABBBCCCDDDEEE123" Part B ====== 1. % Read, add and output total of two strings representing binary % numbers. Assume the two input strings have the same length. var n1 : string var n2 : string var total : string var carry : string var illegal_input : boolean := false put "Enter 2 binary numbers as strings. " .. get n1 get n2 %% Build up the total as you would with normal addition, %% i.e. from right to left. %% The carry bit is zero in the first column. total := "" carry := "0" for decreasing j: length(n1) .. 1 %% Column sum is "00" or "01" or "10" or "11" in binary. %% Append last digit to left of total %% and carry first digit to next column. %% First case: column sum is "00" if (carry = "0" and n1 (j) = "0" and n2 (j) = "0") then total := "0" + total carry := "0" %% Second case: column sum is "01" elsif (carry = "0" and n1 (j) = "0" and n2 (j) = "1") or (carry = "0" and n1 (j) = "1" and n2 (j) = "0") or (carry = "1" and n1 (j) = "0" and n2 (j) = "0") then total := "1" + total carry := "0" %% Third case: column sum is "10" elsif (carry = "0" and n1 (j) = "1" and n2 (j) = "1") or (carry = "1" and n1 (j) = "0" and n2 (j) = "1") or (carry = "1" and n1 (j) = "1" and n2 (j) = "0") then total := "0" + total carry := "1" %% Fourth case: column sum is "11" elsif (carry = "1" and n1 (j) = "1" and n2 (j) = "1") then total := "1" + total carry := "1" else % string contains some characters other than "0" or "1" illegal_input := true exit %% the for-loop end if end for %% If bad input, stop this attempt here, otherwise proceed normally. if illegal_input then %% better than: "if illegal_input = true then" put "Illegal input. Try again" else % Perhaps a carry caused the total string to be one character longer. if carry = "1" then total := "1" + total end if put "The sum of the binary numbers ", n1, " and ", n2, " is ", total, "." end if Sample runs: Enter 2 binary numbers as strings. "1" "1" The sum of the binary numbers 1 and 1 is 10. Enter 2 binary numbers as strings. "1001" "1111" The sum of the binary numbers 1001 and 1111 is 11000. Enter 2 binary numbers as strings. "1001" "11A1" Illegal input. Try again 2. % Repeatedly % read, add and output total of two strings representing binary numbers. % (Assume the two input strings have the same length.) % Stop when first string input is "quit". %% The program is like that of question 1 except that the beginning and end %% have changed. Place the old program in a loop and exit only if first %% string input is "quit". Leave one blank line each time around the loop. var n1 : string %% var n2 : string %% var total : string %% AS BEFORE var carry : string %% var illegal_input : boolean := false %% loop put "Enter 2 binary numbers as strings. " .. get n1 exit when n1 = "quit" %% better is: "exit when n1(1) = "q" or n1(1) = "Q" " get n2 ... AS BEFORE ... put "The sum of the binary numbers ", n1, " and ", n2, " is ", total, "." end if %% Leave a blank line between additions. put "" end loop put "GOODBYE" -------------------------------------------------------------------------------- Sample run: Enter 2 binary numbers as strings. "1" "1" The sum of the binary numbers 1 and 1 is 10. Enter 2 binary numbers as strings. "1001" "1111" The sum of the binary numbers 1001 and 1111 is 11000. Enter 2 binary numbers as strings. "1001" "quit" Illegal input. Try again Enter 2 binary numbers as strings. "quit" "1111" GOODBYE ----------------------------------------------------------------------------------- Full text of program for question B2 % Repeatedly % Read, add and output total of two strings representing binary % numbers. (Assume the two input strings have the same length.) % Stop when first string input is "quit". var n1 : string var n2 : string var total : string var carry : string var illegal_input : boolean := false %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% loop %% %% CHANGED put "Enter 2 binary numbers as strings. " .. %% FROM get n1 %% QUESTION B1 exit when n1 = "quit" %% %% better is: exit when n1(1) = "q" or n1(1) = "Q" %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% get n2 %% Build up the total as you would with normal addition, %% i.e. from right to left. %% The carry bit is zero in the first column. total := "" carry := "0" for decreasing j: length(n1) .. 1 %% Column sum is "00" or "01" or "10" or "11" in binary. %% Append last digit to left of total %% and carry first digit to next column. %% First case: column sum is "00" if (carry = "0" and n1 (j) = "0" and n2 (j) = "0") then total := "0" + total carry := "0" %% Second case: column sum is "01" elsif (carry = "0" and n1 (j) = "0" and n2 (j) = "1") or (carry = "0" and n1 (j) = "1" and n2 (j) = "0") or (carry = "1" and n1 (j) = "0" and n2 (j) = "0") then total := "1" + total carry := "0" %% Third case: column sum is "10" elsif (carry = "0" and n1 (j) = "1" and n2 (j) = "1") or (carry = "1" and n1 (j) = "0" and n2 (j) = "1") or (carry = "1" and n1 (j) = "1" and n2 (j) = "0") then total := "0" + total carry := "1" %% Fourth case: column sum is "11" elsif (carry = "1" and n1 (j) = "1" and n2 (j) = "1") then total := "1" + total carry := "1" else % string contains some characters other than "0" or "1" illegal_input := true exit %% the for-loop end if end for %% If bad input, stop this attempt here, otherwise proceed normally. if illegal_input then %% better than: if illegal_input = true then put "Illegal input. Try again" else % Perhaps a carry caused the total string to be one character longer. if carry = "1" then total := "1" + total end if put "The sum of the binary numbers ", n1, " and ", n2, " is ", total, "." end if %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Leave a blank line between additions. %% put "" %% CHANGED %% FROM end loop %% QUESTION B1 put "GOODBYE" %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3. % Repeatedly % read, add and output total of two strings representing binary numbers. % (DO NOT assume the two input strings have the same length.) % Stop when first string input is "quit". %% The program is like that of question 2, except that we make changes to %% handle input strings of unequal lengths. If this involves changing the %% input strings (they were not changed in the answers to questions 1 and 2), %% then the original strings must be remembered, since they are used in the %% output statement. %% After "get n2" (about line 10) we add % We are going to change the values of n1 and n2. % Remember the original values so we can print them out at the end. const original_n1 := n1 const original_n2 := n2 % Make n1 and n2 the same length, by padding the shorter % string on the left with the appropriate number of "0"'s for i: 1 .. (length(n1) - length(n2)) n2 := "0" + n2 %% never executed if n2 not shorter than n1 end for for i: 1 .. (length(n2) - length(n1)) n1 := "0" + n1 %% never executed if n1 not shorter than n2 end for %% We also change the output line to put "The sum of the binary numbers ", original_n1, " and ", original_n2, " is ", total, "." ------------------------------------------------------------------------------------ Sample runs: Enter 2 binary numbers as strings. "1" "1" The sum of the binary numbers 1 and 1 is 10. Enter 2 binary numbers as strings. "1" "1000" The sum of the binary numbers 1 and 1000 is 1001. Enter 2 binary numbers as strings. "111" "1111111" The sum of the binary numbers 111 and 1111111 is 10000110. Enter 2 binary numbers as strings. "quit" GOODBYE ----------------------------------------------------------------------------------- Full text of program for question B3 % Repeatedly % read, add and output total of two strings representing binary numbers. % (Assume the two input strings have the same length.) % Stop when first string input is "quit". %% The program is like that of question 2, except that we make changes to %% handle input strings of unequal lengths. If this involves changing the %% input strings (they were not changed in the answers to questions 1 and 2), %% then the original strings must be remembered, since they are used in the %% output statement. var n1 : string var n2 : string var total : string var carry : string var illegal_input : boolean := false loop put "Enter 2 binary numbers as strings. " .. get n1 exit when n1 = "quit" %% better is: "exit when n1(1) = "q" or n1(1) = "Q" " get n2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % We are going to change the values of n1 and n2. %% % Remember the original values so we can print them %% CHANGED % out at the end. %% FROM const original_n1 := n1 %% QUESTION B2 const original_n2 := n2 %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Simplest method: %% % make n1 and n2 the same length, by padding the shorter %% % string on the left with the appropriate number of "0"'s %% %% Does not change the numerical value of the string. %% for i: 1 .. (length(n1) - length(n2)) %% n2 := "0" + n2 %% CHANGED %% never executed if n2 not shorter than n1 %% FROM end for %% QUESTION B2 for i: 1 .. (length(n2) - length(n1)) %% n1 := "0" + n1 %% %% never executed if n1 not shorter than n2 %% end for %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Build up the total as you would with normal addition, %% i.e. from right to left. %% The carry bit is zero in the first column. total := "" carry := "0" for decreasing j: length(n1) .. 1 %% Column sum is "00" or "01" or "10" or "11" in binary. %% Append last digit to left of total %% and carry first digit to next column. %% First case: column sum is "00" if (carry = "0" and n1 (j) = "0" and n2 (j) = "0") then total := "0" + total carry := "0" %% Second case: column sum is "01" elsif (carry = "0" and n1 (j) = "0" and n2 (j) = "1") or (carry = "0" and n1 (j) = "1" and n2 (j) = "0") or (carry = "1" and n1 (j) = "0" and n2 (j) = "0") then total := "1" + total carry := "0" %% Third case: column sum is "10" elsif (carry = "0" and n1 (j) = "1" and n2 (j) = "1") or (carry = "1" and n1 (j) = "0" and n2 (j) = "1") or (carry = "1" and n1 (j) = "1" and n2 (j) = "0") then total := "0" + total carry := "1" %% Fourth case: column sum is "11" elsif (carry = "1" and n1 (j) = "1" and n2 (j) = "1") then total := "1" + total carry := "1" else % string contains some characters other than "0" or "1" illegal_input := true exit %% the for-loop end if end for %% If bad input, stop this attempt here, otherwise proceed normally. if illegal_input then %% better than: if illegal_input = true then put "Illegal input. Try again" else % Perhaps a carry caused the total string to be one character longer. if carry = "1" then total := "1" + total end if %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CHANGED put "The sum of the binary numbers ", original_n1, %% FROM " and ", original_n2, " is ", total, "." %% QUESTION B2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end if %% Leave a blank line between additions. put "" end loop put "GOODBYE"