Help on a function.

This is a discussion on Help on a function. within the C++ Programming forums, part of the General Programming Boards category; Same program I was asking for help on earlier. Got it to work just fine except for this part. Here's ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    68

    Help on a function.

    Same program I was asking for help on earlier. Got it to work just fine except for this part.

    Here's the whole function.

    Code:
    // Returns single character 
    char FindSum(int x)
    {
        int y, z, sum = 0;
        char Class;
        for (y=1; y<x && y % 1 == 0; y++)
        {
            z = x / y;
    
            if (x % y == 0)
                sum += z;
        }
    
        if (sum == x)
            Class = 'P';
        else if (sum > x)
            Class = 'A';
        else
            Class = 'D';
    
        return Class;
    }
    The idea is to classify numbers as abundant, deficient, or perfect. However, I can't fin the flaw in my logic that is turning up every number I punch in (including a known perfect number like 6) as abundant.

    Can anyone help?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > However, I can't fin the flaw in my logic
    So debug it then.

    Here's an idea.
    Code:
    // Returns single character 
    char FindSum(int x)
    {
        int y, z, sum = 0;
        char Class;
        for (y=1; y<x && y % 1 == 0; y++)
        {
            z = x / y;
            if (x % y == 0)
                sum += z;
            printf("x=%d, y=%d, z=%d, x%%y=%d, sum=%d\n", x, y, z, x%y, sum);
        }
    
        if (sum == x)
            Class = 'P';
        else if (sum > x)
            Class = 'A';
        else
            Class = 'D';
    
        return Class;
    }
    My results for calling it with 6
    Code:
    $ gcc foo.c
    $ ./a.out 
    x=6, y=1, z=6, x%y=0, sum=6
    x=6, y=2, z=3, x%y=0, sum=9
    x=6, y=3, z=2, x%y=0, sum=11
    x=6, y=4, z=1, x%y=2, sum=11
    x=6, y=5, z=1, x%y=1, sum=11
    A
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    y%1 is always zero.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-07-2012, 05:35 AM
  2. Replies: 13
    Last Post: 03-20-2012, 09:29 AM
  3. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  4. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21