Results 1 to 10 of 10

Thread: Help with a display issue am having

  1. #1
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default Help with a display issue am having

    Ok I got an assignment thats to be handed in tonight. I finish the first one and completing the 2nd one now. I have figured out the hard part but a cout statement that i have is not printing in the order i specified.

    Question:
    The proper divisor of an integer n are the positive divisors less than n. a positive integer is said to be deficient, perfect or abundant number if the sum of its proper divisors are less than, equal to, or greater than the number respectively. For example, 8 is deficient because its proper divisors are 1,2, and 4 and 1 + 2 + 4 < 8; 6 is perfect because 1 + 2 + 3 =6; 12 is abundant because 1 + 2 + 3 +4 +6 > 12. Write a program that classifies n as being deficient, perfect, or abundant for n=20 to 30, then for n=400 to 500 and finally for n=8120 to 8130.

    Code:
    #include <iostream>

    using namespace std;

    int n;
    char check (int r);

    /*
    *
    */
    int main(int argc, char** argv) {

    int r1 = 0, r2, r3;

    cout << "Below will be printed the Proper Divisors" << endl;

    sleep(5);

    for (n = 20; n >= 20 && n <=30; n++) {

    cout << "Integer " << n << ":";

    for (int d = 1; d < n; d++) {

    if (n % d == 0) {

    r1 = r1 + d;
    cout << d << ", ";
    }
    }

    cout << " = " << r1 << ". It is " << check(r1) << endl;
    r1 = 0;
    }


    return 0;
    }

    char check (int r) {

    if (r < n){

    cout << "Deficient";
    }

    else if (r == n) {

    cout << "Perfect";
    }

    else {
    cout << "Abundant";
    }
    }
    Output:
    Below will be printed the Proper Divisors
    Integer 20:1, 2, 4, 5, 10, Abundant = 22. It is *
    Integer 21:1, 3, 7, Deficient = 11. It is *
    Integer 22:1, 2, 11, Deficient = 14. It is *
    Integer 23:1, Deficient = 1. It is *
    Integer 24:1, 2, 3, 4, 6, 8, 12, Abundant = 36. It is *
    Integer 25:1, 5, Deficient = 6. It is *
    Integer 26:1, 2, 13, Deficient = 16. It is *
    Integer 27:1, 3, 9, Deficient = 13. It is *
    Integer 28:1, 2, 4, 7, 14, Perfect = 28. It is *
    Integer 29:1, Deficient = 1. It is *
    Integer 30:1, 2, 3, 5, 6, 10, 15, Abundant = 42. It is
    So as you can see the words Abundant, Deficient and Perfect should be printer at the very end, but for some reason it printing else where.
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

  2. #2
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Default

    Works here. Using LLVM/Clang. Your compiler may be old. Upgrade it or change your IDE.

  3. #3
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default

    Computer aint old man. Am using NetBeans as my IDE. I also ran the executable file thats created and still get the same output
    Last edited by lovepython; Sep 30, 2012 at 05:00 PM.
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

  4. #4
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Default

    http://ideone.com/ik7yb

    I see. That is why I have no confidence in C++ as a language. I may know nothing about programming so far, but C++ based binaries are often problematic to compile on my personal Arch Linux distribution. So, I conclude C++ is absolutely no good.

    Plus, I am strongly influenced by things read, like harmful.cat-v.org/software/ [corrected].
    Last edited by carey; Sep 30, 2012 at 09:46 PM.

  5. #5
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default

    Yes that exactly what am getting. The hamful.cat link not working for me.

    The NetBeans IDE uses cygwin as the compiler, which uses gcc and g++ to comiple code.

    I find it very strange that it would display something in a place that its not supposed to.
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

  6. #6
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    maybe the problem is your check function. you wanted it to return a value but you don't
    1337

  7. #7
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Default

    Code:
    Below will be printed the Proper Divisors
    Integer 20:1, 2, 4, 5, 10,  = 22. It is Abundant
    Integer 21:1, 3, 7,  = 11. It is Deficient
    Integer 22:1, 2, 11,  = 14. It is Deficient
    Integer 23:1,  = 1. It is Deficient
    Integer 24:1, 2, 3, 4, 6, 8, 12,  = 36. It is Abundant
    Integer 25:1, 5,  = 6. It is Deficient
    Integer 26:1, 2, 13,  = 16. It is Deficient
    Integer 27:1, 3, 9,  = 13. It is Deficient
    Integer 28:1, 2, 4, 7, 14,  = 28. It is Perfect
    Integer 29:1,  = 1. It is Deficient
    Integer 30:1, 2, 3, 5, 6, 10, 15,  = 42. It is Abundant
    That's the output from my compiled executable. All the others I tried show the error he is getting.
    Code:
    ./testcode.cpp:55:1: warning: control reaches end of non-void function
          [-Wreturn-type]
    }
    ^
    1 warning generated.
    This is the only warning generated.

  8. #8
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default

    Ok i finally fixed the problem. It is rather sill/stupid if you ask me but it works.

    Changed the single line statement:
    cout << " = " << r1 << ". It is " << check(r1) << endl;
    To this:
    cout << " = " << r1 << ". It is ";
    cout << check(r1) << endl;
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

  9. #9
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Default

    Wow! Compiler turn interpreter now?

  10. #10
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default

    So this is now my completed code:

    Code:
    /* 
     * File:   ProperDivisor.cpp
     * Author: Dwayne Smith
     *
     * Created on September 29, 2012, 3:18 PM
     */
    
    #include <iostream>
    
    using namespace std;
    
    int n;
    char check (int r);
    
    /*
     * 
     */
    int main(int argc, char** argv) {
        
        int r1 = 0, r2 = 0, r3 = 0;
        
        cout << "Below will be printed the Proper Divisors of a specific range of numbers" << endl << endl;
        
        sleep(5);
        
        cout << "Proper Divisor for range 20 - 30" << endl << endl;
        
        sleep(5);
        
        
        //Calculate and print Proper divisors and classify 
        for (n = 20; n >= 20 && n <= 30; n++) {
            
            cout << "Integer " << n << ": ";
            
            for (int d = 1; d < n; d++) {
                
                if (n % d == 0) {
                    
                    r1 = r1 + d;
                    cout << d << " ";
                }
            }
            
            cout << "= " << r1 << ". It is ";
            cout << check(r1) << endl;
            r1 = 0;
        }
        
        //Re-initialize n
        n = 0;
        
        cout << endl << "Proper Divisor for range 400 - 500" << endl << endl;
        
        sleep(5);
        
        //Calculate and print Proper divisors and classify 
        for (n = 400; n >= 400 && n <= 500; n++) {
            
            cout << "Integer " << n << " : ";
            
            for (int d = 1; d < n; d++) {
                
                if (n % d == 0) {
                    
                    r2 = r2 + d;
                    cout << d << " ";
                }
            }
            
            cout << "= " << r2 << ". It is ";
            cout << check(r2) << endl;
            r2 = 0;
        }
        
        //Re-initialize n
        n = 0;
        
        cout << endl << "Proper Divisor for range 8120 - 8130" << endl << endl;
        
        sleep(5);
        
        //Calculate and print Proper divisors and classify 
        for (n = 8120; n >= 8120 && n <= 8130; n++) {
            
            cout << "Integer " << n << " : ";
            
            for (int d = 1; d < n; d++) {
                
                if (n % d == 0) {
                    
                    r3 = r3 + d;
                    cout << d << " ";
                }
            }
            
            cout << "= " << r3 << ". It is ";
            cout << check(r3) << endl;
            r3 = 0;
        }
        
        cin.get();
        return 0;
    }
    
    char check (int r) {
        
        if (r < n){
            
            cout << "DEFICIENT";
        }
        
        else if (r == n) {
            
            cout << "PERFECT";
        }
        
        else {
            cout << "ABUNDANT";
        }
    }
    Last edited by Arch_Angel; Oct 1, 2012 at 03:55 AM. Reason: changed quote tag to code tag
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •