Vous êtes sur la page 1sur 2

/*

Reinforcement 4.3
give an algorithm that uses tail recursion to convert every element in
an integer array A into its absolute vale.

*/

#include <iostream>
#include <cmath>
using namespace std;

int absolute(int Arr[], int size) // the function


{
if(size < 0)
return 0;
else
{
if (Arr[size-1] < 0)
{
Arr[size-1] = abs(Arr[size-1]);
absolute(Arr,size-1);
}

}
}

int main() //driver programme


{

int* Array;
int size;

cout <<"how many values " << endl;


cin >> size;

Array = new int[size];

int val;

cout << "please input the values "<< endl;


for (int i =0; i< size; i++)
{
cin >> val;
Array[i]= val;
}

absolute(Array,size);
for (int i =0; i< size; i++)
{
cout << "their absolute values are " <<Array[i] <<endl;
}

Note:

A recursion is descibed to be a tail recursion if the last action of the


recuresive funtion is a recursive call.

Disclaimer:

The question presented above is the property of john wiley& sons inc. However the code is
solely a humble effort from my side…

Have fun!

If you have any comments please do contact me. My code is open for critique and review.

Vous aimerez peut-être aussi