In: Computer Science
Using an array and a function, print the values of an array backwards. Please follow these guidelines:
- Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer).
- Call your function. You should send two parameters to such function: the array’s length and the array.
- Inside the function, go ahead and print the array backwards.
- Your function shouldn’t return anything.
Solution:
Since no programmming language is specified I have provided the algorithm,pseudocode and a program in C++ language.
Algorithm for function:
START
Step 1 → Loop for each value of A in reverse order
Step 2 → Display A[n] where n is the value of current iteration
STOP
Pseudocode of function:
procedure print_reverse(len,Array)
FOR i from len-1 to 0
DISPLAY A[i]
END FOR
end procedure
C++ code of function:
void print_reverse(int len,int array[])
{
int i=0;
for(i = len-1; i >= 0; i--)
cout<<array[i]<<" ";
}
Complete implementation of code with main function:
#include <iostream>
using namespace std;
void print_reverse(int len,int array[])
{
int i=0;
for(i = len-1; i >= 0; i--)
cout<<array[i]<<" ";
}
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int n=10;
print_reverse(n,array);
return 0;
}
Output of the above code: