Thread: Program prints occurrences but doesn't reset when I enter a new number.

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    13

    Question Program prints occurrences but doesn't reset when I enter a new number.

    What's wrong with my program is that it keeps on adding the repeated digits to the occurrences. I want it to go back to zero after having entered the first number.

    Program prints occurrences but doesn't reset when I enter a new number.-comsci-jpg

    Code:
    #include <stdio.h>
    #define DIGIT 10
    
    Here's my code so far:
    
    
    main(){
    
    
        long long int number=0;
        int digit, i=0;
        int digit_seen[10]={0};
    
    
        printf("Digit Occurence Calculator\n\n");
    
    
        while(){
        printf("Enter a number:");
        scanf("%lld", &number);
    
    
        if(number<=0){
            break;
        }
    
    
        while (number>0){//break up the digit
            digit=number%10; //get the last digit
    
    
            digit_seen[digit]++;//record that you see the number appear once
            number=number/10;
        }
    
    
        printf("\nDigits:    0 1 2 3 4 5 6 7 8 9");
        printf("\nOccurences:");
            for(i=0; i<=9; i++){;
                printf("%d ", digit_seen[i]);
    
    
        }
        printf("\n\n");
        }
    
    
        return 0;
    }
    Any help is greatly appreciated.

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    You have to initialize the digit_seen array with 0 s after printing.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    13
    How would I go about doing that?

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Example code is
    Code:
    for(index=0;index<10;index++)
      array[index] = 0;
    You can adapt to your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program that prints closest number to average value
    By High Voltage in forum C Programming
    Replies: 4
    Last Post: 04-24-2016, 01:40 PM
  2. Replies: 38
    Last Post: 02-19-2015, 10:45 PM
  3. Replies: 14
    Last Post: 01-09-2013, 06:35 AM
  4. Replies: 8
    Last Post: 12-30-2010, 10:08 PM
  5. Program to show the number of occurrences of words...
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 01-26-2002, 06:44 PM

Tags for this Thread