P. 1
Elements of an array converted to absolute values by using recursion

# Elements of an array converted to absolute values by using recursion

|Views: 145|Likes:
answer to reinforcement 4.3 about converting elements of an array into absolute value using recursion!
answer to reinforcement 4.3 about converting elements of an array into absolute value using recursion!

See more
See less

10/02/2013

pdf

text

original

# /* 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() { int* Array; int size; cout <<"how many values " << endl; cin >> size; //driver programme

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.

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->