9691 Discussion Group
unread,Sep 28, 2011, 12:46:23 AM9/28/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Computing (9691 CIE Syllabus)
This could be written as a C function because of the factor that it is
hard to show a function which receives an array as parameter and after
manipulation calls itself as recursive function. If we avoid sending
an array as parameter and keep it as a global array, i.e. only
available to all functions and procedures of the program then it is
possible to write a pseudo code for it.
In this case the written function cannot be re-used outside the
program it is written for. Anyhow I am writing both below for your
understanding along with their explanations.
C function:
void print(int a[], int n)
{ if (n<=0) return ;
printf("%d\n", a[0]);
print(&a[1], n-1);
}
Explanations:
1)
&a[1] is the address of the second element of the array, which is
effectively the address of the portion of the array after the first
element. So after printing the first element of the parameter array,
print(&a[1], n-1);
passes itself the remaining portion of the array, decreasing the
length by one as well.
For example, if you call print with the array {1, 2, 3, 4, 5} and n ==
5, the chain of events and calls is the following:
print the first element (1)
call itself with the remaining portion of the array, i.e. {2, 3, 4, 5}
and n == 4
print the first element (2)
call itself with the remaining portion of the array, i.e. {3, 4, 5}
and n == 3
print the first element (3)
call itself with the remaining portion of the array, i.e. {4, 5} and n
== 2
print the first element (4)
call itself with the remaining portion of the array, i.e. {5} and n ==
1
print the first element (5)
call itself with the remaining portion of the array, i.e. {} and n ==
0
n<=0 -> return
return
return
return
return
return
2)
This function takes as arguments the remaining part of the array and
how many elements it contains. Every time you print the first element
and then call recursively of the remaining part. Here is an example:
array: 1, 2, 3, 4, 5, 6; N = 6
array: 2, 3, 4, 5, 6; N = 5
array: 3, 4, 5, 6; N = 4
array: 4, 5, 6; N = 3
array: 5, 6; N = 2
array: 6; N = 1
array: ; N = 0 return;
3)
Arrays are basically pointers to the start of the first element, so
you code is essentially this:
void print(int *a, int n)
{ if (n<=0) return ;
printf("%d\n", *a);
print(a+1, n-1);
}
The recursive call is passing in a pointer to the next item in the
array and decreasing the count, which is your used in your recursive
termination condition.
Pseudo code:
Function printArr(arrPos as Integer, n as Integer)
If n <= 0 then
return
Else
Print myArray(arrPos)
printArr(arrPos + 1, n-1)
Endif
End Function
Explanation:
Suppose myArray(n) is a global array with n index. Recursive functions
actually reduce values from end to start that's why we use arrPos and
n two parameters for recursive function printArr where arrPos
increases by one in every recursive call to print subsequent array
value and n decreases to 0 (rough value) to stop recursion.
Following is the calling function of above pseudo code:
Dim myArray(5) as string
myArray(1) = "a"
myArray(2) = "b"
myArray(3) = "c"
myArray(4) = "d"
myArray(5) = "e"
Dim arrPOs as integer
arrPos = 1
Dim n as integer
n = 5
printArr(arrPos, n)