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

Task 3: Harder Recursion The task will involve the class Multiple, located in th

ID: 3601815 • Letter: T

Question

Task 3: Harder Recursion

The task will involve the class Multiple, located in the given file multiple.h For this task you will implement the functions provided in the multiple.h file inside the multiple.cpp file. You are required to create your own makefile The following functions are required to be implemented:

Multiple :

• countOccurences(int numbers[], int length, int index) You are given an array of integers 0 or greater, the length of that array and and index.

Recursively (no loops or libraries) calculate if the array contains in it a value followed in the array by that same value times 10.

The initial call will always pass in index as 0. Example: countOccurences([1, 2, 20], 3, 0) = 1 countOccurences([3, 30], 2, 0) = 1 countOccurences([3], 1, 0) = 0

NOTE: Recursion must be used in order to solve the problem. Use of any loops, search functions or libraries will be taken as cheating and result in a large penalty to your marks.

Given Files:

multiple.h

#ifndef MULTIPLE_H

#define MULTIPLE_H

class Multiple {

public:

bool countOccurences(int numbers[], int length, int index);

};

#endif

multiple.cpp:

#include "multiple.h"

bool Multiple::countOccurences(int numbers[], int length, int index)

{

}

Explanation / Answer

Below the definition of countOccurences method is given-

bool Multiple::countOccurences(int numbers[], int length, int index)

{

//Terminating condition. When last element of array is reached return false.

if(index==length-1) return false;

//Check if next element is 10 times of current elemnt in array and return true if present.

else if(num[index]*10==num[index+1]){

return true;

}

else{

//Else recursive call to countOccurences() method with next index.

return countOccurences(num,length,index+1);

}

}

First the terminating condition will be when we reach till the last element of array without finding 10 times greater element next to the current element. We will return false in this case. After that in other cases we check for required condition(10 times of current element) and return true if found else call countOccurences() function recursively with next element.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote