0

I have a problem with this code.... The project should show me repeated number in the input number. For example:

$ ./a.out
Enter a number: 9893746595
Repeated: 9 5

Here is the code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{   
    int a[10], b[10] ;
    int n,t;
    printf("Enter a number: ");
    for(n=0; n<10;n++)
    {
        scanf("%d", &a[n]);
        n=t;
        a[n]=b[t];

    }
    for(n=0;n<10;n++)
    {
        for(t=n;t<10;t++)
        {
            if(a[n]=b[t])
            printf("%d", a[n]);
        }
    }
    return 0;
}
6
  • 1
    Please format your code next time. Please also ask a specific question - what's the problem you're having? Your program has a lot of logical errors.
    – Carl Norum
    Apr 19, 2011 at 22:42
  • @Carl: beginners won't know how to format code. Just fix it for them and move on.
    – wallyk
    Apr 19, 2011 at 22:43
  • @wallyk, since there is a big information/help box right beside the question-entering text field, containing all of the instructions on how to properly format a question, I don't think it's too much to ask of new posters.
    – Carl Norum
    Apr 19, 2011 at 22:45
  • 1
    In the first loop, t is uninitialised, as is the array b. In the second loop, only the first iteration is likely to match. The code looks like something out of my nightmares. I'm almost sorry I tried to answer.
    – exception
    Apr 19, 2011 at 22:46
  • I also fixed up his example to be in a codeblock. Apr 19, 2011 at 22:50

5 Answers 5

2

if(a[n]=b[t]) assigns b[t] to a[n].

You most likely wanted to use if(a[n] == b[t]) to compare those values.

It's a good idea to compile using the -Wall -Wextra -Werror flags so all warnings are enabled and treated like errors (so you can't simply ignore them). With those flags the compiler will yell at you for doing an accidental assignment.

1
  • 1
    +1 for warnings, so few people seem to bother with them, even when they know there is something wrong with their code.
    – David X
    Apr 20, 2011 at 1:48
2

Your code is bogus. ;-)

The usual approach here is to create an array of 10 ints, one for each digit, and count the occurrences of each digit in the user-supplied number.

There's an idiomatic technique to get the digits of a number num one at a time: use num % 10 to get the last digit, and num / 10 to get the number without its last digit. Then your program might look something like this:

int dcount[10] = {0};  // 10 ints, all initialized to 0

scanf("%d", &num);

while(num) {
        dcount[num % 10]++;   // increment dcount[i], where i is the last digit of num
        num /= 10;            // "remove" last digit from num
}

for (int i = 0; i < sizeof(dcount)/sizeof(dcount[0]); i++)
        printf("%d occured %d times\n", i, dcount[i]);

I didn't test the above code, so there may be some minor flaws. The general principle should be clear, though.

Hope that helps.

0

Your code assigns t to n before t has been initialized.

This is bound to cause problems. I haven't fully studied the rest of your code but you should start by initializing t before using it. If that still doesn't work, provide information such as what it does or doesn't do.

0

You might want to look at the first for loop. t is read from without ever being written to. Same for b. And, you are overwriting the just read in variable a[n] with b[t]. In the conditional for if, you meant == where = is written.

If you turn on every option in your compiler to emit warnings and strictly check for standard language compliance, it would have caught these.

0
int main()
{
   int i, number, digitCount[10];

   // Before starting, set the digit count for each digit to 0
   for (i = 0; i < 10; i++)
   {
      digitCount[i] = 0;
   }

   // Store the entire number in one int
   printf("Enter a number: ");
   scanf("%d", &number);

   // Find the remainder of number / 10 in order to get the last digit
   // Divide number by 10 in order to remove that digit
   // Continue to peel off digits until you reach zero
   while (number != 0)
   {
      digitCount[number % 10]++;
      number /= 10;
   }

   // For each digit which is counted more than once, print it
   printf("Repeated: ");
   for (i = 0; i < 10; i++)
   {
      if (digitCount[i] > 1)
      {
         printf("%d ", digitCount[i]);
      }
   }

   return 0;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.