### Shorter way to read/print array

I know there is a way to have all these if statements done in 1-3 lines, but I haven't been able to find it.

 ``123456789101112131415161718192021222324252627282930313233`` `````` void digitCount(char digitsArr[], int countDigit[]) { int countDigits[NUM_DIGITS] = {0}; char oneDigit; for(int i=0;i
What is common between these two lines?
 ``12`` ``````if (oneDigit == '2') countDigits[ 2 ]++;``````

(Hint: the '2'|2. Figure out how to turn a '2' into a 2. There's a pretty simple trick. Hint: '0'..'9' are contiguous.)
Their index or ASCII ID?
Yep. You don't have to care what the actual number is. You only have to care what the difference is.

0 + 7 = 7
'0' + 7 = '7'
Do I use either ASCII or index, does it matter? I'm not 100% sure on how I change it honestly.

I've tried
 ``12345678`` `````` if (oneDigit =='049') countDigits[1]++; if (oneDigit =='049') countDigits[i]++; if (oneDigit =='1') countDigits[i]++;``````

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include #include #include using namespace std; const int SIZE = 12; const int NUM_DIGITS = 10; void printCounts(int []); void digitCount (char[], int[]); int main() { char digitsArr[SIZE]={'3','0','8','1','9','4','2','6','7','5','2','2'}; char oneDigit; //one digit int countDigits[NUM_DIGITS] = {0}; for(int i=0;i
I'm still stuck D:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include void count_digits( int (&counts)[10], long long number ) { if( number < 0 ) return count_digits( counts, -number ) ; // ignore leading sign for( int& cnt : counts ) cnt = 0 ; if( number == 0 ) counts[0] = 1 ; else while( number != 0 ) { ++counts[ number%10 ] ; number /= 10 ; } } // invariant: array number consists of decimal digits except for an optional leading sign // note: leading zeroes are counted void count_digits( int (&counts)[10], const char number[], std::size_t n ) { for( int& cnt : counts ) cnt = 0 ; if( n == 0 ) return ; if( number[0] == '+' || number[0] == '-' ) count_digits( counts, number+1, n-1 ) ; // ignore leading sign else for( std::size_t i = 0 ; i < n ; ++i ) { // if( !std::isdigit( number[i] ) throw something const std::size_t digit = number[i] - '0' ; // note: '5' - '0' == 5 etc. ++counts[digit] ; } } int main() { int counts[10] ; count_digits( counts, -12345678908766789 ) ; for( int cnt : counts ) std::cout << cnt << ' ' ; std::cout << '\n' ; const char number[] = "-00012345678901234567890123452345345455" ; count_digits( counts, number, sizeof(number)-1 ) ; for( int cnt : counts ) std::cout << cnt << ' ' ; std::cout << '\n' ; }``````

http://coliru.stacked-crooked.com/a/9eaf102d13e31b11
 ``12345678910111213141516171819`` ``````# include constexpr auto SIZE = 12; constexpr auto NUM_DIGITS = 10; int main() { char digitsArr[SIZE]={'3','0','8','1','9','4','2','6','7','5','2','2'}; int countDigits[NUM_DIGITS] = {0}; for (auto i = 0; i < SIZE; ++i) { ++countDigits[static_cast(digitsArr[i] - '0')]; } for (auto i = 0; i < NUM_DIGITS; ++i) { std::cout << "Number of occurrences of: " << i << ": " << countDigits[i] << "\n"; } }``````
Topic archived. No new replies allowed.