0

I'm writing a function that will find the number with max number of divisors but the function is not returning anything. Can someone point out my mistake?

This is the question

Write a C++ program that creates and integer array having 30 elements. Get input in this array (in main function). After that, pass that array to a function called “Find_Max_Divisors” using reference pointer. The function “Find_Max_Divisors” should find (and return) in the array that number which has highest number of divisors. In the end, the main function displays that number having highest number of divisors.

#include <iostream>
using namespace std;

int main ()
{
    int arr[30];
    int* array = &arr[30];
    cout << "Please enter values of the array" << endl;
    for (int i=0; i<30; i++)
    {
        cin >> arr[i];
    }
    cout << "Number with most divisors in array is " << endl;
    int Find_Max_Divisors (*array);
}

int Find_Max_Divisors (int p[])
{
    int count=0, max_divisor, max_counter, prev=0, repeat=0, divisor;
    for (int i=2; i<=30; i++)
        {
            if (p[i]%i==0)
            {
                count++;
            }
            if (count > prev)
            {
                prev = count;
                divisor = p[i];
            }
            if (count==max_counter && max_counter!=0)
            {
                cout << p[i] <<" has maximum of "<< count <<" divisors.\n";
            }
        max_counter = prev;
        max_divisor = divisor;
        repeat++;
        }
        return count;
}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Omer Aman
  • 3
  • 2

4 Answers4

1

change

int Find_Max_Divisors (*array);

to

int value = Find_Max_Divisors(arr);

You can get rid of the array variable altogether.

It's quite possible you'll find you need to put your function before main, too.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42
1

Firstly, you declare an array that has 30 elements

int arr[30];

But here you make the pointer point to the out of arr.

int* array = &arr[30];

I guess you want to make pointer point to arr, if i am not wrong, you can do as:

int *array = &arr[0]; // or int * array = arr;

Then when you call the Find_Max_Divisors function, you should change to:

int return_value = Find_Max_Divisors(array);

One more thing, int this function:

for (int i=2; i<=30; i++)

When i=30, p[i] go to out of bount again. It should be:

for (int i=2; i< 30; i++)
Hitokiri
  • 3,607
  • 1
  • 9
  • 29
  • We can also take advantage of [array decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) and `int *array = arr;` – user4581301 May 08 '20 at 02:30
  • Yes, in this case, i just want to emphasize on his point of view. I mean pointer should point to first element not element 31. I edited. Anw, thank you for your comment. – Hitokiri May 08 '20 at 04:44
0

There are several mistakes in your code:

  • First, if your main function should know the funtions it calls, you should declare them previously. Just add a line Find_Max_Divisors (int p[]); Before the main function.

  • An array in C or C++ is a pointer, when you only call it by it's name. So call Find_Max_Divisors (arr) and get rid of that awful pointer-assignment.

  • In the last line just try to call the function, but never put it to stdout, you should change it to this:

cout << "Number with most divisors in array is " << Find_Max_Divisors(arr) << endl;

What you actually did with int Find_Max_Divisors (*array); was declaring a new variable and not calling a function.

void
  • 142
  • 9
0

you don't need pointers to do that this simple code can fix your problem just change the size of your array as you want i am testing with array of size 4 here

#include <iostream>
using namespace std;

int Find_Max_Divisors(int p[])
{
    int count = 0, max = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 1; j < p[i] / 2; j++) {
            if (p[i] % j == 0) {
                count++;
            }
        }
        if (count > max)
            max = p[i];
    }
    return max;
}
int main()
{
    int arr[30];
    // int* array = &arr[30];
    cout << "Please enter values of the array" << endl;
    for (int i = 0; i < 4; i++) {
        cin >> arr[i];
    }
    int value = Find_Max_Divisors(arr);
    cout << "Number with most divisors in array is " << value << endl;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64