> Begginer C++ programer, need some help on an equation involving loops and SVR functions. Can anyone point me in the righ

Begginer C++ programer, need some help on an equation involving loops and SVR functions. Can anyone point me in the righ

Posted at: 2014-12-18 
Whip out a blank sheet of paper and manually work the process using an input value of 10. Then note each step you took and convert that into code. For example:

0. Program informs user that it will determine the divisors of any number they enter.

1. Program asks user to enter a number between 2 and 50

- store User's input into 'n'

- (If necessary, validate User's input)

- Find the n/2 value which becomes our loop's max value. Ignore any fractional bits. Store the n/2 value into 'loop_max'

2. Create a loop that starts at '1' and ends at 'loop_max'

- using loop index variable 'i' -- and using the modulo symbol (%), test each i to see if it's a valid divisor of 'n': n % i. If it is, add 'i' to the summation variable 'sum'. (Note: remember to zero out sum each time the user chooses a new 'n'.)

3. When "summation" loop ends, print 'n' and 'sum' (future version may print the divisors that went into the sum.)

4. Program ends, or loops back to the top where it asks for another input value.

-------------

Update: If you take out a piece of paper and trace the code you wrote -- you'll find the error of your ways. To trace your variables, write the variable names on the top line and draw a line beneath them. Each time your initialize or change a variable's contents, write the new number *below* the previous one. For example:

i -- n -- sum

----------------

* -- 6 -- * : User enters a '6', so n=6

6 -- 6 -- 1: sum is set to '1'; i = n (now i=6)

[Error: we messed with the loop counter: i ] Our 'i' counter should start at 1 and stop at some calculated value -- according to the instructions.

Your 2nd error will be that you're using the variable 'sum' to determine when to add the loop value to sum. That's not quite right.

My last hint: Try using a "For" loop -- easy to see the start and end of the loop. Your "While" loop is not built correctly.

I was able to write all the void functions in my program, but I cannot figure out the SVR function I must write. I am stumped.

Given hints:

Determine if a number is a perfect divisor of another by using the % operator. Use the loop control variable as the divisor. Also have another local variable sum initialize to zero or one. Whenever you find a perfect divisor, add that to the sum.

Besides 1, the smallest and largest divisors of any number n will fall between 2 and n/2, inclusive.

Use a loop in the function to test every potential divisor that the integer has, ans the sum of the perfect ones.

ex. 6 is a perfect number because its divisors are 1, 2, and 3, and 1+2+3=6. 28 = 1+2+4+7+14. If you pass 18 into the function, it should return 21, 21=1+2+3+6+9.

I'm lost at this point, I cannot figure out which loops to use along with the math I should be implenting with those loops. Here is what I have.

int sum_of_divisors(int n)

{

int sum = 1;

int i = n;

if(sum % i == 0)

{

sum = sum + i;

i++;

}



while(2 <= i >= i/2);



return (sum);

}