Need help with HW for my C++ class.

I ran into a wall and want to see what I'm doing wrong. I'm trying to do the last part and find the number with the largest amount of divisors.

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
46
47
48
49
50
51
52
53
54
55



#include <iostream>
#include <conio.h>

using namespace std;

void maxDivs(int &low, int &high);
void numDivs(int &num);

int main()
{
	int low, high;

	cout << "Enter two numbers, a low number followed by a high: \n";

	cin >> low >> high;
	maxDivs(low, high);

	_getch();
	return 0;
}

void maxDivs(int &low, int &high)
{
	
	int divs = 0;
	int divisor;

		for (int i = high; i >= low; i--)
	{
			
			if(i % 2 == 0)
		{
			divs += 1;
			divisor= high /= i;
			numDivs(i);
		}
	}
}

void numDivs(int &num)
{
	int divs = -1;
	 	for (int i = num; i > 1; i--)
	{
		if(num % i ==0)
		{
			divs += 1;
		}
	}
		cout << num <<" has " << divs << " divisors.\n";

}
I deleted maxDivs function so you might not like this:
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
46

#include <iostream>


using namespace std;

void numDivs(int num1, int num2);

int main()
{
	int low, high;

	cout << "Enter two numbers, a low number followed by a high: \n";

	cin >> low >> high;
	numDivs(low, high);

	return 0;
}


void numDivs(int num1, int num2)
{
	int divs = 0;
	 	for (int i = num1-1; i > 1; i--)
	{
		if(num1 % i ==0)
		{
			divs += 1;
		}
	}
		cout << num1 <<" has " << divs << " divisors.\n";

		divs = 0;
	 	for (int i = num2-1; i > 1; i--)
	{
		if(num2 % i ==0)
		{
			divs += 1;
		}
	}
		cout << num2 <<" has " << divs << " divisors.\n";

		

}
It's fine. That didn't work how I intended. I'm trying to figure out how to only print out the number with the most divisors. I have edited my code.
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <conio.h>

using namespace std;

void maxDivs(int &low, int &high);
void numDivs(int &num);

int main()
{
	int low, high;

	cout << "Enter two numbers, a low number followed by a high: \n";

	cin >> low >> high;
	maxDivs(low, high);

	_getch();
	return 0;
}

void maxDivs(int &low, int &high)
{
	
	int divs = 0;
	int divisor;
	int largerDiv = 0;

		for (int i = high; i >= low; i--)
	{
			
		
			divs += 1;
			divisor= high /= i;
			
			numDivs(i);
			
			
	
		
	}
}

void numDivs(int &num)
{
	
	int divs = 0;
	 	for (int i = num; i >= 1; i--)
	{
		if(num % i ==0 )
		{
			divs += 1;
		}
		
	}
	
		cout <<num << " has "<< divs << " divisors.\n";

}
You need two additional variables somewhere.

int maxNumberOfDivisors = 0;
int numberWithTheMaxNumberOfDivisors = 0;

So at some point you can do
1
2
3
4
if ( divs > maxNumberOfDivisors ) {
    maxNumberOfDivisors = divs;
    numberWithTheMaxNumberOfDivisors = num;
}

I tried doing something similar but it didn't work. It keep iterating through all of the values.
I don't think you should output anything in the numDivs(). Just return a result.

You have a list of values. A range [low..high].
You want to find an element from list, whose property "divisors" is largest.

There are two subtasks:
1. Compute the property "divisors" of a value. divisors = numDivs(value);

2. Find largest value. How would you do that for a simple list of values, say an array:
1
2
3
4
std::vector<int> range;
// Something adds some numbers to range

// How to find the largest from range? 
mivasquez386 wrote:
I tried doing something similar but it didn't work. It keep iterating through all of the values.

You need to at least post your attempt at it.
Saying "you tried and it didn't work" doesn't tell us why what was suggested didn't work for you.
Salem, I will do so, sorry about that. I guess I need to find the right placement where to insert the if statement.

As for the actual problem at hand, here is the actual assignment that was provided:

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 10 to divs since 48 has 10 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 8 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.

You must read the range of integers and print the results in main only. You must not use any global variables. You must use the two functions, as specified, in addition to main. Not following instructions will result in significant loss of points.

I'm going to work on this some more with the suggestions you guys provided, I really appreciate it. I am really new to this programming language but I hope to learn more and can become really proficient. I have read my assignment over and over again and kept modifying it based on my understanding from the context that was given.
Topic archived. No new replies allowed.