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.
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 27 28 29 30 31 32 33
|
void digitCount(char digitsArr[], int countDigit[])
{
int countDigits[NUM_DIGITS] = {0};
char oneDigit;
for(int i=0;i<SIZE;i++) {
oneDigit = digitsArr[i];
if (oneDigit =='0')
countDigits[0]++;
else if (oneDigit =='1')
countDigits[1]++;
else if (oneDigit =='2')
countDigits[2]++;
else if (oneDigit =='3')
countDigits[3]++;
else if (oneDigit =='4')
countDigits[4]++;
else if (oneDigit =='5')
countDigits[5]++;
else if (oneDigit =='6')
countDigits[6]++;
else if (oneDigit =='7')
countDigits[7]++;
else if (oneDigit =='8')
countDigits[8]++;
else if (oneDigit =='9')
countDigits[9]++;
else
; //to be completed
}
}
|
What is common between these two lines?
1 2
|
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
1 2 3 4 5 6 7 8
|
if (oneDigit =='049')
countDigits[1]++;
if (oneDigit =='049')
countDigits[i]++;
if (oneDigit =='1')
countDigits[i]++;
|
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 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <stdlib.h>
#include <ctype.h>
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<SIZE;i++) {
oneDigit = digitsArr[i];
if (oneDigit =='2')
countDigits[i]++;
}
printCounts(countDigits);
}
void printCounts(int counts[])
{
cout << "\nCounts are: "<< endl;
for(int i =0; i<NUM_DIGITS;i++) {
cout << "Digit " << i << " occurs " << counts[i] << " time(s)." << endl;
}
}
|
I'm still stuck D:
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
# include <iostream>
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<int>(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.