number of divisors

Since I am terrible at summarizing here is what I need to do

"Write a function - maxDivs() - that takes two integers such as low and high and assigns the integer that has the largest number of divisors among all the integers that lie between low and high to an output parameter - num - and the number of divisors of num to another output parameter - divs. For example, if low and high are 20 and 50, the function assigns 48 to num since 48 has the largest number of divisors between 20 and 50 and assigns 8 to divs since 48 has 8 divisors - more than any other in the 20-50 range.
Write another function - numDivs() - that takes an integer as its only parameter and returns its number of divisors. For example, if its input parameter is 24, it returns 6 as its number of divisors.
Write a main program that reads two integers for low and high and passes them to maxDivs() function. maxDivs() function in turn gets the number of divisors for each integer between low and high by calling numDivs() function for each integer in the range.
Print both the integer with the largest number of divisors and the number of divisors it has, as output by maxDivs(), in main."

I'm unsure on how to properly do the maxDivs function currently. I believe everything else to be ok.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  #include <iostream>
using namespace std;
int maxdivs(int num, int divs);
int numdivs(int);
int main() // also will become void(int num, int divs)
{
	int max, min;
	cout << "input the lowest number. ";
		cin >> min;
	cout << "input the highest number. ";
	cin >> max;
	maxdivs(min, max);
}

int maxdivs(int num, int divs)
{
	int low, high;
	for (int i=2;i<=high;i++)
		if (low%i==0)
	{
			cout << low;
			low++;
	}
}

int numdivs(int div)
{
	int div;
	int amount;
	amount = 0;
	cout << "put number ";
		cin >> div;
	for (int i=2;i<div;i++)
	{
		if (div%i==0)
			amount++;
		
	}
	cout << amount;
	return amount;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

// returns the number of integral divisors of number excluding  1 and number
int numdivs( int number )
{
    if( number < 0 ) number = -number ; // make it non-negative

    int num_divs = 0 ;
    for( int div = 2 ; div <= number/2 ; ++div )
    {
        if( number%div == 0 ) ++num_divs ;
    }

    return num_divs ;
}

// on return, num == number in the inclusive interval [low,high] with the largest number of divisors
//            div == number of integral divisors (excluding 1 and num) of num
// note: output parameters are passed by reference
void maxdivs( int low, int high, int& num, int& div )
{
    // (optional) swap low and high if low > high
    if( low > high ) { int temp = low ; low = high ; high = temp ; }

    num = low ;
    div = numdivs(low) ;

    for( int number = low+1 ; number <= high ; ++number )
    {
        const int ndivs = numdivs(number) ;
        if( ndivs > div ) { div = ndivs ; num = number ; }
    }
}

int main()
{
    int low = 20 ; // std::cin >> low
    int high = 50 ; // std::cin >> high

    int num ;
    int ndivs ;
    maxdivs( low, high, num, ndivs ) ;

    std::cout << num << ' ' << ndivs << '\n' ;
}

http://coliru.stacked-crooked.com/a/1a0e6e7a4280c7c7
Topic archived. No new replies allowed.