Thread: Please help a biginner

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Question Please help a biginner

    I want to write a short code to sum the digits of a 5digit number. Someone please help me to developed the pseudo code and maybe the approach to write to code. Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What ideas do you have?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    You can do something like:
    Code:
    while(n > 0) 
    sum += n%10;  // sum is 0 at the beginning
    n /= 10;
    using the mod operator you take the last digit of the number and decrement the number dividing it by 10

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Do modulus ten on the number to get the last digit, then divide by ten (C rounds down), and repeat.

    Looks like rob90 beat me.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    I was looking at an array approach...what do you think?

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Thanks rob90 but I was hoping for some rather simple approach to it..like an array approach. Help please..

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by KOFI
    I was looking at an array approach...what do you think?
    If you read in the number as a string, that certainly is a good idea, otherwise you might as well use what has been suggested.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    Yes, you should take the number as a string and then read each element of the array and convert it to an integer to make the sum, but I think that using the modulus is even simpler

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by KOFI View Post
    Thanks rob90 but I was hoping for some rather simple approach to it..like an array approach. Help please..
    I'm gonna third the opinion that modulus/division is the simplest method, but if you want to use an array:

    [edit] corrected from sscanf to sprintf
    Code:
    int x = 54321;
    char array[6];  // we need room for the null terminator
    ssprintf(array, "%d", x);
    "array" is now a string version of x, where each character is a single digit. You will now need to consult the ASCII table to do arithmetic with it:

    ASCII Table / Extended ASCII Codes

    Which is not a bad idea, ASCII is very fundamental to even basic C programming. Feel free to ask questions.
    Last edited by MK27; 04-01-2010 at 03:33 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    You will now need to consult the ASCII table to do arithmetic with it:
    Luckily, you don't: you can just subtract '0'.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Luckily, you don't: you can just subtract '0'.
    Why does that work, laserlight?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    Why does that work, laserlight?
    Because it is guaranteed that for any standard conforming implementation, the sequence of digits in the character set from '0' to '9' will be contiguous.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I still think KOFI will be missing out on the excitement, unless...

    ASCII Table / Extended ASCII Codes
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I think it's sprintf()

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nonoob View Post
    I think it's sprintf()
    Yeah, eerie, I was just doing more or less this exact thing and then suddenly remembered something...anyway fixed.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed