Write a function Palindrome which accepts two integers.

COSC 1436: Function Lab

There are a number of different parts to this lab. Each one will be graded equally, though some are dependent on other steps being completed. Each of the parts will ask you to create a new function (or possibly use a function already created), and I intend for you to do this all in one file, with one main function calling all the other ones. The main function will not be graded, but should be used to test your functions to make certain they work as intended. The questions will tell you want to call your functions but if there is any confusion commenting will help.

  • Summation: Write a function Summation which accept two integers as arguments and returns their sum. Call this function from main( ) and print the results in main( ).
  • Raising to a Power: Raising a number to a power p is the same as multiplying n by itself p times. Write a function called Raise Power that takes two arguments, a double value for n and an int value for p, and return the result as double value. You should calculate the value yourself (not use cmath::pow()). Use default argument of 2 for p, so that if this argument is omitted the number will be squared.
  • Is Even: Write a function is Even that accepts an integer and returns a bool indicating if the integer parameter is even.
  • Using Reference Variables: Write a function called Zero Small that has three integer arguments being passed by reference and sets the smallest of the three numbers to 0.
  • Write a function AddLargest2EvenNumbers, that takes 3 numbers. Check if the numbers are odd using the is Even function and if so adds 1 to the number. Now take the 3 numbers and add the two largest of the numbers by using the Zero Small and the Summation (you should not need to modify the summation function for this problem). Return the result to main.
  • Find the largest Divisor: Write a function Largest Divisor that takes 1 integer and tests if it is prime – another way of putting this is that it will test if the largest divisor is 1. If so it returns 1, else it returns the largest divisor (that is not the number). To accomplish this one must use a loop and modulus to see if the division of numbers comes out to a whole number (modulus is often called the divisibility operator).
  • Write a function Power To TheDivisor that takes 1 integer. It uses the Largest Divisor to get another integer and then the Raise Power function to raise the fed in integer to the power of its largest divisor. Return the result.
  • Write a function Sum Of Digits, that takes an integer and adds the value of the digits together and returns this to main. Examples 156 = 12, or 4461 = 15. Note that the integer can be any size that would fit in an int. 1389411 = 27
  • Write a function SumOfPrimeDigits that takes three integers – a min, a max, by value and a sum as a reference variable. The function will start searching for prime numbers at the min and will stop at the max. For each prime number found the function will use SumOfDigits and add it to the sum. Finally, if the sum is prime, the function returns true, if not it returns false.
    Example: SumOfPrimeDigits(3, 12, sum) => sum would equal 17 (3+5+7+2) at the end and the function would return true.
  • Write a function called DivisibilityTester. Which takes three integers and outputs (cout in the function) all the numbers between the first two that are divisible the third number. The third number however should be an optional parameter. If it is not given, then the function should output all of the prime numbers.
    input: 1, 10, 3
    outputs: 3, 6, 9
    input: 4, 10
    outputs: 5,7
  • Bonus 10%: Write a function Palindrome which accepts two integers. A number to test and the number of digits in the number that we are testing and returns true if the digits in the number form a palindrome (the number reads the same way forwards as it does backwards). Example being 34543, or 118811. Note that the number can have an even or odd number of digits and the number could be any size capable of fitting into and integer. No credit will be given to anyone who uses strings to solve this problem.
  1. Challenge Bonus 15%: Write a function called knights Move, to calculate how many knight’s moves it will take to get from two input numbers to two target numbers. A knight’s move is one that either adds 2 or subtracts 2 from one number and adds 1 or subtracts 1 from the other number. So if my inputs were:
    1 and 4
    and the target was:
    6 and 0
    then the moves I could make are:
    1+2 = 3, 4-1=3
    3+1 = 4, 4-2=1
    4+2= 6, 1-1 =0
    so the function would return 3, indicating that it would take 3 knight’s moves to reach the target.
  0 1 2 3 4 5 6 7
0                
1         1      
2                
3       2        
4   3            
5                
6 4              
7