Give a recursive algorithm in [Pascal style] pseudocode which computes the conca
ID: 3627007 • Letter: G
Question
Give a recursive algorithm in [Pascal style] pseudocode which computes the concatenation of i copies of a string.
For example:
procedure concat(w = w1w2 . . .wn : string, i : nonnegative integer)
. . .
. . .
. . .
(v = v1v2v3 . . . is the concatenation of i copies of w)
return v
For two strings x = x1 . . . xk and y = y1 . . . y, use the pseudocode expression xy to refer to the concatenation of x and y.
For example, the assignment z := xy will result in z being the string x1 . . . xky1 . . . y.
THIS IS WHAT I HAVE SO FAR, IS IT CORRECT?
procedure concat(w = w1w2 . . .wn : string, i : nonnegative integer)
if i:= 0 then
v:=w
return v
else if i1 then
v:=concat(v+w, w, i-1)
return v
end
Explanation / Answer
Not correct. One problem is that you call concat with three arguments and it can only take two arguments. Here is the correct answer -- I don't know the details of Pascal-style pseudocode but I'll just follow the style you've used and you can alter my answer if I've made any mistakes in details of style.
procedure concat(w = w1w2 . . .wn : string, i : nonnegative integer)
if i:= 0 then
v:= "" // Zero copies of w is the empty string
return v
else
// Concatenate w i-1 times, then add on one more w
v:= concat(w, i-1)
v:= vw
return v
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.