Maurice Abney
Maurice Abney

Reputation: 223

Write a function count(number, array, length) that will count the number of times number appears in array

I'm completing the problem sets out of an o'reilly textbook on my own and I'm a bit confused on what I'm supposed to be doing with this program. The question is: Write a function count(number, array, length) that will count the number of times number appears in array. the array has length elements. The function should be recursive. Write a test program to go with the function.

The question is copied word for word, but I have a few questions about this question. if you could leave a comment stating your opinion about the following, that would be nice:

  1. Okay, I need an array with length length, do I store my own numbers in the slots? or do I generate random integers to go in the slots?
  2. also, do I have the right plan? My plan is to:

create the array and call count in main. the function count will be an if loop that goes through the array until it finds number it will then add one to a counter. then the count function will call it's self with the parameters count(int number; int array; int (length - 1))

Now that I try to talk myself through it, I am now even more confused. Maybe I'm just making the question more difficult. Any tips will help. Thanks

Upvotes: 1

Views: 4089

Answers (2)

Chaos
Chaos

Reputation: 382

  1. To test whether your function is right yes, you can use your own numbers in the array. It's just important that the algorithm works.
  2. Nope your approach is wrong. Recursion has two parts:

a) The clause about what you know

b) The clause about what you could know if you change what you have.

So in your case the function count(.....) should at first get to know whether the length is 0 because then the number number is 0 times in array. If it is not, you need to call your function count(.....) with number as the number (cause you still want to count the same number) the array except for the last element of the array and length-1. That recursively shovels down the array until it has no more items in it.

~ short break, to let the brain refresh ~

You should save the return value of the function in a variable, and afterwards add 1 if the last item of array is number, otherwise not. And again return that value. Now this value is passed backwards through all the function calls and incremented if the arrayelement we are looking at is the number. The original call to count(....) returns the number of numbers in array

I hope I have no fails in my explanation.

Upvotes: 0

aldeb
aldeb

Reputation: 6828

I think a code snippet worth more than a lot of words. So I would go for something like that:

#include <iostream>

int count(int num, int* arr, int length) {
    if (!length)
        return 0;
    int c = count(num, arr+1, length-1);
    return arr[0] == num? c + 1: c;
}

int main(void) {
    int arr[10] = {1, 2, 3, 4, 3, 2, 1, 4, 3, 2};

    std::cout << count(2, arr, 10);

    return 0;
}

Output:

3

Upvotes: 2

Related Questions