printf("\n\tHow many integers: ");
scanf("%d", &num);
iPtr = (int*) malloc(num * sizeof(int));
for(i = 0; i < num; i++) {
printf("Enter integer # %d ", i + 1);
scanf("%d", (iPtr + i));
temp = *(iPtr + i);
while(temp != 0) {
if(i == temp % 10) {
ary[i]++;
}
temp /= 10;
}
}
for(i = 0; i < 10; i++) {
if(ary[i] > 0) {
printf("digit %d : %d\n", i, ary[i]);
}
}
I am writing a program to store the occurrences of an digit from an integers,but when I run the program, I see the array is not working
so I want to ask why it is not allow me to store the digit from an integer?
and how to fix it?
and 1 more question, what is the different between if(x = y) and if(x == y)
Ok so you’ve almost got it right but I think the biggest issue is your array has garbage in it to begin with (when declared).
This program is also incomplete, because your loop might end before you’ve even checked a digit high enough to match. Ie, you’re checking if temp still has a number in it, but you’re only comparing it to i, which doesn’t change (so if the digit in temp is 9, but i is 1, you’ll never trigger to see that there’s a 9 to count).
So I modified
to
And now the program counts all the digits.
Output:
This is all assuming I’m even understanding your program right. And usually I don’t like straight posting code but I really didn’t know how else to get the point across. Tired atm. :/