Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives: Learn and implement recursions References: http://www.danzig.us/cpp/

ID: 3876775 • Letter: O

Question

Objectives: Learn and implement recursions References: http://www.danzig.us/cpp/recursion.html Set up: Create a new folder under your drive, e.g., z:lcsci225 lab2_yourNamel for each programing question below: 1) 2) o Create a new VC++ project o Create a cpp file that: Defines and implements the function in question Write a main() to test the function Lab Questions 1. Write a recursive function that write the digits of a positive decimal integer in reverse order (without convert it into a string first) 2. Implement the minArray algorithm (recursive) for an array of numbers 3. Implement an iteration version of minArray for an array of numbers

Explanation / Answer

#include <stdio.h>
#include <math.h>

int rev(int, int);

int main(void)
{
int num, result;
int length = 0, temp;
int array[100], minimum, size, c, loc = 1;


printf("Enter an integer number to reverse: ");
scanf("%d", &num);
temp = num;
while (temp != 0)
{
length++;
temp = temp / 10;
}
result = rev(num, length);
printf(" The reverse of %d is %d. ", num, result);
printf(" find minimum of array");
printf(" Enter the number of elements in array ");
scanf("%d",&size);

printf(" Enter %d integers ", size);

for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);

minimum = array[0];

for ( c = 1 ; c < size ; c++ )
{
if ( array[c] < minimum )
{
minimum = array[c];
loc = c+1;
}
}

printf(" Minimum element is present at location %d and it's value is %d. ", loc, minimum);

return 0;
}

int rev(int num, int len)
{
if (len == 1)
{
return num;
}
else
{
return (((num % 10) * pow(10, len - 1)) + rev(num / 10, --len));
}
}