Thread: Help with Number Assignment

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    79

    Help with Number Assignment

    I need to write a function that will ask for an integer. The function needs to determine the sign (positive or negative) and the number of times a digit appear in the given integer for all possible digits (i.e., 0, 1, 2,…9). I'm stuck because I don't know how to use mod/loops to extract each number from the possible input (which can be anything from a 1 digit number [3] to a 10 digit number [1245029341])

    A sample of the output should be :

    Enter an integer: 1034029
    The sign : +
    The counts of digits:
    0 2
    1 1
    2 1
    3 1
    4 1
    5 0
    6 0
    7 0
    8 0
    9 1

    P.S. Can't use arrays (haven't learned them yet)

    So basically, my real problem is how to extract each single number from the input at a time until there is no number to extract.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by .C-Man. View Post
    So basically, my real problem is how to extract each single number from the input at a time until there is no number to extract.
    Use this:
    Code:
    digit = number % 10;
    number /= 10;
    Untill number == 0 of course.
    Devoted my life to programming...

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    To get the digits, in reverse, just compute mod 10 of your number, which gives you the last digit, and then divide by 10 to remove that digit(truncate the number by the last digit) and move on to the next. If you can't use arrays you will probably need 10 variables to keep the count of every digit. Use a switch statement to increase the correct count depending on what digit you encounter.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Quote Originally Posted by Sipher View Post
    Use this:
    Code:
    digit = number % 10;
    number /= 10;
    Untill number == 0 of course.
    Thanks, I don't know how I didn't get that. But, if I do that until number == 0 wouldn't it add an extra counter to 0?

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Just do while(number != 0).... what we said. Then, for every digit you get, check what it is and increment the counter for that digit.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Code:
    void displayIntegerDigit( void ){
    	
    	int Num0;
    	int Num1;
    	int Num2;
    	int Num3;
    	int Num4;
    	int Num5;
    	int Num6;
    	int Num7;
    	int Num8;
    	int Num9;
    	int NumExtract;
    	int NumberEntered;
    	int NxtNum;
    	
    	printf("Enter a integer: ");
    	scanf( "%d", &NumberEntered );
    
    	
    	while (NumberEntered != 0){
    		
    		NumExtract = NumberEntered % 10;
    		NxtNum = (NumberEntered /= 10);
    		
    		if(NumExtract == 0){
    			Num0++;
    		}
    		if(NumExtract == 1){
    			Num1++;
    		}
    		if(NumExtract == 2){
    			Num2++;
    		}
    		if(NumExtract == 3){
    			Num3++;
    		}
    		if(NumExtract == 4){
    			Num4++;
    		}
    		if(NumExtract == 5){
    			Num5++;
    		}
    		if(NumExtract == 6){
    			Num6++;
    		}
    		if(NumExtract == 7){
    			Num7++;
    		}
    		if(NumExtract == 8){
    			Num8++;
    		}
    		if(NumExtract == 9){
    			Num9++;
    		}
    		
    	}
    	printf("\nThe counts of digits: ");
    	printf("\n0 %d", Num0);
    	printf("\n1 %d", Num1);
    	printf("\n2 %d", Num2);
    	printf("\n3 %d", Num3);
    	printf("\n4 %d", Num4);
    	printf("\n5 %d", Num5);
    	printf("\n6 %d", Num6);
    	printf("\n7 %d", Num7);
    	printf("\n8 %d", Num8);
    	printf("\n9 %d", Num9);
    	
    }
    This is what I have so far... it's printing out weird things. I'm still working on it though.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Set num0 thru num9 equal to 0 at start of program.

    This is not the correct way.
    Code:
    while (NumberEntered != 0){
    		
    		NumExtract = NumberEntered % 10;
    		NxtNum = (NumberEntered /= 10);
    This might work; untested; when inside a loop you need to change the value tested by the loop 99 percent of the time.

    Code:
    NxtNum = NumberEntered; 
    while (NxtNum != 0){
    		
    		NumExtract = NxtNum % 10;
    		NxtNum = (NxtNum /= 10);
    Note: I think the number of zero might be low if the last or first digit is zero.

    Tim S.
    Last edited by stahta01; 11-10-2010 at 03:02 PM. Reason: Added Note and Code

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Code:
    void displayIntegerDigit( void ){
    	
    	int Num0 = 0;
    	int Num1 = 0;
    	int Num2 = 0;
    	int Num3 = 0;
    	int Num4 = 0;
    	int Num5 = 0;
    	int Num6 = 0;
    	int Num7 = 0;
    	int Num8 = 0;
    	int Num9 = 0;
    	int NumExtract;
    	int NumberEntered;
    	int NxtNum;
    	
    	printf("Enter a integer: ");
    	scanf( "%d", &NumberEntered );
    
    	NxtNum = NumberEntered; 
    	while (NxtNum != 0){
    		
    		NumExtract = NxtNum % 10;
    		NxtNum = (NxtNum /= 10);		
    		
    		if(NumExtract == 0){
    			Num0++;
    		}
    		if(NumExtract == 1){
    			Num1++;
    		}
    		if(NumExtract == 2){
    			Num2++;
    		}
    		if(NumExtract == 3){
    			Num3++;
    		}
    		if(NumExtract == 4){
    			Num4++;
    		}
    		if(NumExtract == 5){
    			Num5++;
    		}
    		if(NumExtract == 6){
    			Num6++;
    		}
    		if(NumExtract == 7){
    			Num7++;
    		}
    		if(NumExtract == 8){
    			Num8++;
    		}
    		if(NumExtract == 9){
    			Num9++;
    		}
    		else {
    			break;
    		}
    
    		
    	}
    	printf("\nThe counts of digits: ");
    	printf("\n0 %d", Num0);
    	printf("\n1 %d", Num1);
    	printf("\n2 %d", Num2);
    	printf("\n3 %d", Num3);
    	printf("\n4 %d", Num4);
    	printf("\n5 %d", Num5);
    	printf("\n6 %d", Num6);
    	printf("\n7 %d", Num7);
    	printf("\n8 %d", Num8);
    	printf("\n9 %d", Num9);
    	
    }
    Sample Output:

    Enter a integer: 8222

    The counts of digits:
    0 0
    1 0
    2 1
    3 0
    4 0
    5 0
    6 0
    7 0
    8 0
    9 0

    It only counts the least significant digit but nothing else.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Look at your if statements, and more specifically your else statement. They are disjoint. On the first run through your loop, NumExtract is 2 (as it should be). The if(...1) fails, the if(...2) succeeds, the if(...3) fails, and so on. When the if(...9) fails, you hit your else block, which calls a break.

    You should be using:
    Code:
    		if(NumExtract == 0){
    			Num0++;
    		}
    		else if(NumExtract == 1){
    			Num1++;
    		}
                    ...
    		else if(NumExtract == 9){
    			Num9++;
    		}
    		else {
    			break;
    		}
    so it can only ever enter 1 of those sections. Or better yet, use a switch statement, since it was designed for exactly this sort of situation.

    As a side note, after making the changes I suggested, your program still won't work for the input 0 (zero). I'm not sure, if this is for homework, if you need to consider that special case.
    Last edited by anduril462; 11-10-2010 at 05:01 PM. Reason: added some code tags

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Yes, I realized that and I changed it. But another problem I'm having is when the number inserted is long that it starts counting to random numbers.
    ex.


    Enter a integer: 10340291029

    The counts of digits:
    0 1
    1 1
    2 0
    3 2
    4 1
    5 2
    6 1
    7 2
    8 0
    9 0

    or

    Enter a integer: 30928490238409238

    The counts of digits:
    0 0
    1 2
    2 1
    3 3
    4 1
    5 0
    6 1
    7 0
    8 2
    9 0

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Actually, they're not random. You get the same output in both cases. I changed a small section of your code. Try putting this in your program and running it.

    Code:
        int NumberEntered;
        int NxtNum;
        int rv;
    
        printf("Enter a integer: ");
        rv = scanf( "%d", &NumberEntered );
    
        printf("scanf returned: %d, errno is %d: %s\n", rv, errno, strerror(errno));
        printf("The number you entered is %d\n", NumberEntered);
    
        NxtNum = NumberEntered;
        while (NxtNum != 0){

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Oh, and you will probably need a

    Code:
    #include <errno.h>
    at the top of your file.

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    I would use that but unfortunately I can't use anything I haven't learned in class. Also, when the sign is negative it prints out for everything. Do you know why?

    Enter a integer: -2389429
    The sign: -
    The counts of digits:
    0 0
    1 0
    2 0
    3 0
    4 0
    5 0
    6 0
    7 0
    8 0
    9 0

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by .C-Man. View Post
    I would use that but unfortunately I can't use anything I haven't learned in class.
    I didn't intend for you to use that in your final draft that you turn in, it was for your edification. If you make the changes I suggested, you will one, learn about the errno variable and how standard C functions such as scanf describe to you any problems you have, and two, understand why your program doesnt work for those large values.

    Quote Originally Posted by .C-Man. View Post
    Also, when the sign is negative it prints out for everything. Do you know why?
    Yes, I do. I'll tell you once you try out the changes I suggested and tell me what you found.

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    This is what I got:

    Program received signal: “EXC_BAD_ACCESS”.
    sharedlibrary apply-load-rules all
    (gdb)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone help?
    By javalurnin in forum C Programming
    Replies: 11
    Last Post: 12-02-2009, 06:02 AM
  2. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 01:48 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. My perfect number assignment...
    By Ninestar in forum C Programming
    Replies: 8
    Last Post: 11-16-2005, 02:47 AM
  5. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM