C++ Looping Issue

Started by
11 comments, last by F E A R 18 years, 1 month ago
I've had my eye on this forum for quite awhile, but this is my first post. I'm a 28 y/o beginning programmer taking C++. Any and all responses to this problem are greatly appreciated, because this one has been driving me crazy. This program I'm writing has to be written in looping form, to take a single number from 3 sets of numbers. Once it takes that number, it has to determine whether it is a deficient, perfect, or abundant number based on the sum of its divisors(or factors). (For example, the numbers that will divide into 12 leaving no remainder, are: 1, 2, 3, 4, 6 (the number itself, in this case 12, is exempt). 1 + 2 + 3 + 4 + 6 = 16. Therefore, 16 > 12 and so 12 is considered an "abundant" number since the sum of its factors is greater than the number itself. If the factors had equaled 12 exactly, 12 would be considered a "perfect" number. If the sum of the factors of 12 had equaled less than 12, they would be considered a "deficient" number. The three sets of numbers I've been given to work with for this program, are: n = 20 to 30 inclusive n = 490 to 500 inclusive n = 8120 to 8130 inclusive For example, the first number that would need to be tested is 20. 20 would be factored down to: 1, 2, 4, 5, 10 --because these are the #'s that will divide into 20 leaving no remainder. You would then add them together to get: 1 + 2 + 4 + 5 + 10 = 22 Therefore, 22 > 20 and 20 would be considered an abundant number. Okay, if I haven't lost you yet, here is my program below. Everything prints out okay, but I'm having trouble getting the calculations to work. When I run the program, everything reads as "abundant," and I know that's not right. I'm guessing there may be a problem with the accumulator (total += divisor) but I'm not sure. Help, please! The textbook I'm using has been little help for this program; there are no examples like this anywhere in it. *******************************************************
//This program will determine whether a number is deficient, perfect, or 
//abundant for the given values of n:

//n = 20-30 inclusive
//n = 490-500 inclusive
//n = 8120-8130 inclusive


#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    char key;
    
    
    //Introduce the user to the program and prompt an initial response
    cout << "This program will determine whether a number is deficient, " << endl;
    cout << "perfect, or abundant for the given values of n:" << endl;
    cout << "n = 20-30 inclusive" << endl;
    cout << "n = 490-500 inclusive" << endl;
    cout << "n = 8120-8130 inclusive" << endl << endl;
    cout << "Press any key to begin." << endl << endl << endl;
    cin.get(key);
    cout << "**********************************************" << endl;
    cout << "   Number          " << "Classification" << endl;
    cout << "________________________________________" << endl;


    //Variable declarations
    int number, divisor, total;
    float remainder;
    string numberType;
    
    //Outer Loop 1 Set Determinant
    for ((number = 20); (number >= 20 && number <= 30); number++)
    {
                                  
                    //Inner Loop 1 calculations
                    for ((divisor = 1); (divisor < number); divisor++)
                    {                         
                       remainder = number % divisor;
                       while (remainder = 0)
                       {
                           total += divisor;                           
                       }             
                       if (total < number)        
                       {
                            numberType = "Deficient";
                       }
                       else if (total == number)
                       {
                            numberType = "Perfect";
                       }
                       else if (total > number)
                       {
                            numberType = "Abundant";
                       }
                                                                    
                    }
                    cout << "    " << number << "                    "; 
                    cout << numberType << endl;   
      }              
                 
      //Outer Loop 2 Set Determinant
      for ((number = 490); (number >= 490 && number <= 500); number++)
      {   
                       
                    //Inner loop 2 calculations
                    for ((divisor = 1); (divisor < number); divisor++)
                    {
                        remainder = number % divisor;
                        while (remainder = 0)
                        {                                                       
                          total += divisor;                                    
                        }  
                    }    
                    
                    if (total < number)
                    {
                           numberType = "Deficient";
                    }                   
                    else if (total == number)
                    {  
                           numberType = "Perfect";
                    }                     
                    else if (total > number)
                    {
                           numberType = "Abundant";
                    }                   
                    
                   
                   cout << "    " << number << "                   "; 
                   cout << numberType << endl;
                   
       }            
                          
       //Outer Loop 3 Set Determinant
       for ((number = 8120); (number >= 8120 && number <= 8130); number++)
       {
                      
                    //Inner loop 3 calculations
                    for ((divisor = 1); (divisor < number); divisor++)
                    {
                        remainder = number % divisor;
                        while (remainder = 0)
                        {
                              total += divisor;
                        }      
                    }
          
                    if (total < number)
                    {
                              numberType = "Deficient";
                    }
                    else if (total == number)
                    {
                              numberType = "Perfect";
                    }
                    else if (total > number)
                    {
                              numberType = "Abundant";
                    }
                    
                     cout << "    " << number << "                  ";
                     cout << numberType << endl;
                     
       }                    
                  
    
    system("pause");
    return 0;
}

[Edited by - F E A R on March 2, 2006 11:15:54 AM]
Advertisement
(Fixed format, thanks.)

I have 3 outer loops. Each one chooses one out of the three sets of numbers I have to work with (n = 20-30, etc). There are also 3 inner loops, each one set to use the divisor on each number.

[Edited by - F E A R on March 2, 2006 11:51:44 AM]
change:
while (remainder = 0)
{
total += divisor;
}

to:
if(remmainder = 0)
{
total += divisor;
}

and see if it works then. Btw use [/s o u r c e] tags to keep the format. (without the spaces)
Simplicity is the ultimate sophistication. – Leonardo da Vinci
I think one err is here:

for ((divisor = 1); (divisor <
Too late ;)
You can use [source lang="cpp"][/source] tags to display source code - it will come with indentation and colors, and will be easier to read :)

Here is a snippet of your code, with comments. Your goal is to replace the things that won't work
//Inner Loop 1 calculations// ** you don't really need these parenthesis (unless you find // ** it more readable, of course)for ((divisor = 1); (divisor < number); divisor++){  // ** nothing to say about the following line  remainder = number % divisor;  // ** here, you assign 0 to remainder and then you do a loop on this "condition"  // ** you won't even enter in the loop, since the condition  // ** is already 0  while (remainder = 0)  {    // ** never executed :(    total += divisor;  }  // ** this is done for each iteration, even if total don't change  // ** in fact, total will be teh same value at the end of the loop -   // ** you don't really need to change the numberType value each time  // ** the total change :)  if (total < number)  {    numberType = "Deficient";  }  else if (total == number)  {    numberType = "Perfect";  }  else if (total > number)  {    numberType = "Abundant";  }}

As a consequence, you total variable never change. Unfortunately, you don't initialize it, so it contains whatever it want (and in your case, it seems that the total is greater than the number).

But the main problem is in your algorithm: why do you put a while () loop here ? You just want to test wether the remainder is 0 or not (and if it's 0, then you'll have a bunch of things to do).

Of course, I won't give you the correct code (it is an exercise, after all). I hope I have been able to help you :)

Regards,
Quote:Original post by ForeverNoobie
change:
while (remainder = 0)
{
total += divisor;
}

to:
if(remmainder = 0)
{
total += divisor;
}

and see if it works then. Btw use [/s o u r c e] tags to keep the format. (without the spaces)<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>It won't work very well (and, as always, I'll let you find why [smile])<br><br>Regards,<br><br>
I didn't read the posts but this really hurts my eyes:

if(remmainder = 0){    total += divisor;}


wouldn't it be

if(remmainder == 0){    total += divisor;}


?
yeah sory, typing quickly. I whent ahead and tried to run the code and found afew problems.

1st of all total needs to be initialized to 0. Second of all you need to set it back to 0 after each number.
Simplicity is the ultimate sophistication. – Leonardo da Vinci
Thanks for the suggestions so far. After I get back from the gym I'll try making some adjustments.

I think I see now that the while loop I have in there is kind of useless. Would it make sense to replace it with an if statement and then run through all the steps I have after that?

For example:

if (remainder == 0){	total += divisor;		if (total < number)	{	numberType = "Deficient";	}	else if (total == number)	{	numberType = "Perfect";	}	else if (total > number)	{	numberType = "Abundant";	}}total = 0;cout << "    " << number << "                    "; cout << numberType << endl;



Would that work, you think? I initialized total as global at the top of my program; do I need to initialize it inside the loop as well for it to accumulate?

[Edited by - F E A R on March 2, 2006 11:38:59 AM]

This topic is closed to new replies.

Advertisement