Variable names are important; try to make them meaningful. You have one array called "num" and then you have another array called "number". What is the difference in purpose between these two arrays? (After reading your post again, I realize what that difference is, but it's still a bit confusing when you first read it). I would change it to "digits" to actually store the digit counts.
Also, note that (once you add 'break;'s like dutch said), your switch statement can collapse down to simply:
num[number[x]-1] += 1;
Now, I have no idea if that's supposed to be right or not, I'm just pointing out an observation.
Edit: Furthermore, you're re-creating your "number" array each loop iteration. Is that what you want? Furtherfurthermore, the way you're creating your number array is not legal C++ because arrays have to have compile-time constant sizes.
I suggest using a vector.
Edit: Just kidding, you don't even need a vector, because you don't need to actually store each iteration. You just need to update your histogram of digits.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
// Example program
#include <iostream>
using std::cout;
using std::cin;
int main()
{
int count;
cout << "Enter number of items: ";
cin >> count;
cout << "Enter digits: ";
int digits_histogram[10] {}; // init'd to all 0s
for (int i = 0; i < count; i++)
{
int number;
cin >> number;
digits_histogram[number % 10]++;
}
for (int i = 0; i < 10; i++)
{
cout << "digit " << i << ": " << digits_histogram[i] << '\n';
}
}
|
Enter number of items: 7
Enter digits: 1 1 0 9 2 2 3
digit 0: 1
digit 1: 2
digit 2: 2
digit 3: 1
digit 4: 0
digit 5: 0
digit 6: 0
digit 7: 0
digit 8: 0
digit 9: 1 |