Thread: need help with task

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    7

    need help with task

    The teacher gave me an assignment to get 10 numbers in the program. Check the sum of digits of a number, and the length of its digits. Check the number about the largest digit length, check the number with the largest number of digits. And if the number with the largest sum of digits of a number is equal to the number with the largest digit length, display in different message. The file did not run well for me, I would be happy for help.
    Code:
    #define _CRT_SECURE_NO_WARNINGS
        #include <stdio.h>
        
        int len(int n);
        int sum(int a);
        
        int main()
        {
            int x, i = 1, maxcount, maxsum;
            printf("Please enter number: \n");
            scanf("%d", &x);
        
            maxcount = x;
            maxsum = x;
        
        
        
        
            while (i <= 9) {
                printf("Please enter number: \n");
                scanf("%d", &x);
                
                if (len(maxcount)<len(x))
                    maxcount = x;
        
                if (sum(x) > sum(maxsum))
                    maxsum = x;
        
                i++;
                }
            
            
            if (maxcount==maxsum) {
                printf("The sum and the count of the length are equal and the number is: %d \n",maxcount);
            }
            else {
                printf("The number with max length is: %d \n", maxcount);
                printf("The number with max sum is: %d \n", maxsum);
            }
            return 0;
        }
        
        
        int len(int n) {
            int i, count = 1;
            for (i = 0; i <= n; i++) {
                count++;
        
                n /= 10;
                
        
            }
            return count;
        }
        
        
        
        int sum(int a) {
            int i, count = 0;
            for (i = 0; i <= a; i++) {
                count += (a % 10);
        
                a /= 10;
        
        
            }
            return count;
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,313
    Quote Originally Posted by Afik
    Check the sum of digits of a number, and the length of its digits. Check the number about the largest digit length, check the number with the largest number of digits. And if the number with the largest sum of digits of a number is equal to the number with the largest digit length, display in different message.
    That's a little confusing to me: what's the difference between "largest digit length" and "largest number of digits"? I'm guessing that you meant:
    • Compute the sum of digits of each number and record the number with the largest sum of digits.
    • Count the number of digits of each number and record the number with the largest number of digits.
    • If the number with the largest sum of digits is equal to the number with the largest number of digits, print the number and a message informing the user of this fact, otherwise print the number with the largest sum of digits and the number with the largest number of digits.

    Is this what you meant?

    If so, one thing I'd suggest is that instead of always computing len(maxcount) and sum(maxsum), you use these as caches of the largest known. Instead, store the numbers corresponding to the largest len and sum in another pair of variables.

    Quote Originally Posted by Afik
    The file did not run well for me, I would be happy for help.
    What does "did not run well for me" mean? For example, you should provide your test input, expected output, and actual output. It might also good to explain why you think your expected output is correct whereas the actual output is wrong.

    Additionally, one of the things to do is to check that your len and sum functions are working as expected before you use them.
    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
    Sep 2020
    Posts
    7
    Quote Originally Posted by laserlight View Post
    That's a little confusing to me: what's the difference between "largest digit length" and "largest number of digits"? I'm guessing that you meant:
    Quote Originally Posted by laserlight View Post
    • Compute the sum of digits of each number and record the number with the largest sum of digits.
    • Count the number of digits of each number and record the number with the largest number of digits.
    • If the number with the largest sum of digits is equal to the number with the largest number of digits, print the number and a message informing the user of this fact, otherwise print the number with the largest sum of digits and the number with the largest number of digits.

    Is this what you meant?

    If so, one thing I'd suggest is that instead of always computing len(maxcount) and sum(maxsum), you use these as caches of the largest known. Instead, store the numbers corresponding to the largest len and sum in another pair of variables.


    What does "did not run well for me" mean? For example, you should provide your test input, expected output, and actual output. It might also good to explain why you think your expected output is correct whereas the actual output is wrong.

    Additionally, one of the things to do is to check that your len and sum functions are working as expected before you use them.






    I meant that the sum of digits of the number for example 1234, is 10, and the length of its digits is 4.

    And I want to check 10 numbers, and finally if the number with the largest length is also the number with the largest sum of the digits, display in a different message.
    The number that both max length and sum length is the max is: %d

    and If not, display in 2 messages:
    one:
    The number with the max length is: %d
    two:
    The number with the max sum is: %d

    The functions work great.
    The problem is probably in the int main....



  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,313
    Quote Originally Posted by Afik
    I meant that the sum of digits of the number for example 1234, is 10, and the length of its digits is 4.

    (...)

    The functions work great.
    Ah, how interesting. I compiled and ran this program:
    Code:
    #include <stdio.h>
    
    int len(int n) {
        int i, count = 1;
        for (i = 0; i <= n; i++) {
            count++;
    
            n /= 10;
        }
        return count;
    }
    
    int sum(int a) {
        int i, count = 0;
        for (i = 0; i <= a; i++) {
            count += (a % 10);
    
            a /= 10;
        }
        return count;
    }
    
    int main(void) {
        int n = 1234;
        printf("Number of digits of %d is %d\n", n, len(n));
        printf("Sum of digits of %d is %d\n", n, sum(n));
        return 0;
    }
    The output that I received was:
    Code:
    Number of digits of 1234 is 4
    Sum of digits of 1234 is 9
    This contradicts your claim that the functions work great since the sum of digits is incorrect. Then I changed n to 0, and the output I received was:
    Code:
    Number of digits of 0 is 2
    Sum of digits of 0 is 0
    This contradicts your claim that the functions work great since the number of digits is incorrect.

    EDIT:
    Quote Originally Posted by Afik
    The problem is probably in the int main....
    Actually, I fixed len and sub and tested with your main function. While there's room for improvement, the revised program with your main function works great.
    Last edited by laserlight; 09-30-2020 at 05:03 PM.
    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

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    7
    Quote Originally Posted by laserlight View Post
    Ah, how interesting. I compiled and ran this program:
    Code:
    #include <stdio.h>
    
    int len(int n) {
        int i, count = 1;
        for (i = 0; i <= n; i++) {
            count++;
    
            n /= 10;
        }
        return count;
    }
    
    int sum(int a) {
        int i, count = 0;
        for (i = 0; i <= a; i++) {
            count += (a % 10);
    
            a /= 10;
        }
        return count;
    }
    
    int main(void) {
        int n = 1234;
        printf("Number of digits of %d is %d\n", n, len(n));
        printf("Sum of digits of %d is %d\n", n, sum(n));
        return 0;
    }
    The output that I received was:
    Code:
    Number of digits of 1234 is 4
    Sum of digits of 1234 is 9
    This contradicts your claim that the functions work great since the sum of digits is incorrect. Then I changed n to 0, and the output I received was:
    Code:
    Number of digits of 0 is 2
    Sum of digits of 0 is 0
    This contradicts your claim that the functions work great since the number of digits is incorrect.

    EDIT:

    Actually, I fixed len and sub and tested with your main function. While there's room for improvement, the revised program with your main function works great.
    ok, can you please show me how to fix it?

  6. #6
    Registered User
    Join Date
    Sep 2020
    Posts
    7
    I fix the functions and now its works!!!
    but the int main still with problem...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,313
    I'd suggest using a while loop instead of a for loop and comparing with 0 in the loop condition.
    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
    Sep 2020
    Posts
    225
    This line in sum() is suspect:

    for(i = 0; i <= a; i++)

    Also, when it doesn't seem to work for negative numbers:


    $ ./sum_of_digits
    Number of digits of -1234 is 1
    Sum of digits of -1234 is -10

  9. #9
    Registered User
    Join Date
    Sep 2020
    Posts
    7
    It's good now. but still have a problem in the int main.

    Code:
    int sum(int a)
    {
    	int s = 0;
    	while (a > 0)
    	{
    		s += (a % 10);
    		a /= 10;
    	}
    	return s;
    }
    int len(int n)
    {
    	int c = 0;
    	while (n > 0)
    	{
    		n /= 10;
    		c++;
    	}
    	return c;
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,313
    Quote Originally Posted by Afik
    It's good now.
    Not quite: len(0) is incorrect, and if you don't intend to handle negative numbers, you should deal with unsigned int rather than int.

    Quote Originally Posted by Afik
    but still have a problem in the int main.
    How does it not work? Just saying that you still have a problem is way too vague. Tell us what you tried (the test input), what you think the result should be (the expected output), and what are the symptoms of the problem (the actual output and why it is wrong).
    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
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,906
    Changing these to work with negative numbers is very easy, just one little change

    Look at this line here...
    Code:
    while (n > 0)
    And then read out what it is doing

    "Do the following while n is larger than 0", which would never work for a negitive number, because they are always going to be smaller than 0.

    What you want is to loop while n is not equal to 0.

    Easy!

    The usual thing to do is to put a special case at the top of the function
    Code:
    int len(int n)
    {
        if(n == [a value that won't work in your algorithm, usually 0])
        {
            return [something specific to this case];
        }
    
        ....
    If the user enters the number 0 10 times, do you want the largest length to be 1? Or is this another special case that this number has a length of 0?

    If that is the case, don't call len if the user enters 0 (simple if statement should do it)
    Last edited by Click_here; 09-30-2020 at 07:24 PM. Reason: Removing incorrect info
    Fact - Beethoven wrote his first symphony in C

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,313
    Quote Originally Posted by Click_here
    Look at it this way: 10 has a length of 2, but if you divide it by 10, you get 0
    You get 1 from that division, not 0. Assuming non-negative input, the issue is specifically for n == 0 because the loop condition is never satisfied, hence c remains 0.

    Quote Originally Posted by Click_here
    The usual thing to do is to put a special case at the top of the function
    That's what I would suggest too, although strictly speaking you can rewrite the loop condition so that you don't need to treat 0 as a special case, and hence set the initial value of c to 1.
    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
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,906
    You get 1 from that division, not 0. Assuming non-negative input, the issue is specifically for n == 0 because the loop condition is never satisfied, hence c remains 0.
    Thanks Laserlight, I'll snip that section out of my reply as it might lead to confusion to anyone reading it
    Fact - Beethoven wrote his first symphony in C

  14. #14
    Registered User
    Join Date
    Sep 2020
    Posts
    225
    Calculating the length of a number is one of the rare times where "do while" loops are useful, as it makes one pass of the loop body before the test.

    Code:
    int len(int n)
    {
      int c = 0;
    
    
      do {
        n /= 10;
        c++;
      } while (n != 0);
    
    
      return c;
    }
    "do while" loops aren't used very often, but sometimes they just make the code better.

  15. #15
    Registered User
    Join Date
    Sep 2020
    Posts
    7
    need help with task-jpg
    this what i can't figure it out.
    why it say that in length of digits the number is 4900, and not both in sum and in length is 7963

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with the task please :3
    By flakee in forum C Programming
    Replies: 3
    Last Post: 03-28-2016, 08:20 AM
  2. Help from the task
    By MAJA83 in forum C Programming
    Replies: 1
    Last Post: 06-06-2012, 11:35 AM
  3. Need help to do the big task
    By Steve Cao in forum Linux Programming
    Replies: 1
    Last Post: 07-16-2010, 02:01 PM
  4. Replies: 2
    Last Post: 12-31-2007, 11:40 AM
  5. Task Bar
    By cfrost in forum Windows Programming
    Replies: 1
    Last Post: 07-22-2004, 07:37 AM

Tags for this Thread