-2

I have an int array called doubledNumbers and if a number in this array is greater than 9, I want to add the digits together. (example, 16 would become 1+6=7, 12 would become 3, 14 would become 5, etc)

Lets say I had the following numbers in doubledNumbers:

12 14 16 17

I want to change the doubledNumbers to:

3 5 7 8

I'm unsure how to do this for an int array as I get the error

invalid types 'int[int]' for array subscript

This is the code I have (thrown in a for loop):

  if (doubledNumbers[i]>9) {
       doubledNumbers[i]=doubledNumbers[i][0]+doubledNumbers[i][1];
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Marisa
  • 59
  • 9
  • 3
    That's because an `int` is an `int`. Convert it into something array-like if you want to use `operator[]`. Also, what should `29` become? `2` or `11`? If it's the former, you have an easier way - just return the number modulo `9`, with a boundary case for multiples of `9`. – Pradhan Jun 09 '15 at 05:57
  • 2
    possible duplicate of [Sum of Digits using recursion in C](http://stackoverflow.com/questions/18523521/sum-of-digits-using-recursion-in-c) – Michael Foukarakis Jun 09 '15 at 05:59
  • 29 would become 11. I'm unsure what you mean by that - does array.[x][y] take array x and grab the yth char of it? – Marisa Jun 09 '15 at 05:59
  • Computers store numbers in binary format. You need the decimal format to count with digits. Converting to string and back is the easiest way, although not the fastest. For a faster way, I’d use BCD. https://en.wikipedia.org/wiki/Binary-coded_decimal – Melebius Jun 09 '15 at 06:01
  • @Marisa , You can't simply use `[]` on numbers as they are not arrays. You can get each digit of a number using [this](http://stackoverflow.com/a/30606797/3049655) – Spikatrix Jun 09 '15 at 06:18
  • 1
    @Melebius: requiring such a dramatic change in the input as encoding it in BCD is really not helpful to someone struggling with this basic problem. – Jonathan Leffler Jun 09 '15 at 06:19
  • Although it's possible to calculate the [digital root](http://en.wikipedia.org/wiki/Digital_root) of a number recursively, it is hardly necessary to do it that way, nor is it the most obvious way to do it unless you learned LISP before you learned C++. – Jonathan Leffler Jun 09 '15 at 06:20

4 Answers4

2

There is nothing like decimal digits in an int. There are (mostly 32 or 64) binary digits (bits) and the base of 2 is not commensurable with the base of 10. You’ll need to divide your numbers by 10 to get decimal digits.

unsigned int DigitSum(unsigned int input, unsigned int base = 10)
{
    unsigned int sum = 0;
    while(input >= base)
    {
        sum += input % base;
        input /= base;
    }
    sum += input;
    return sum;
}

I used unsigned int. The example cannot be directly used for negative numbers but the modification is not difficult.

Melebius
  • 6,183
  • 4
  • 39
  • 52
1

You can use something like this

#include <iostream>
using namespace std;
int sumofdigits(int);
int main() 
{
    // your code goes here
    int a[5] ={12,14,15,16,17};
    for(int i=0;i<5;i++)
    {
        int m=sumofdigits(a[i]);
        cout <<m<<" ";
    }
        return 0;
}
int sumofdigits(int n)
{

        int sum=0;
        while(n!=0)
       {
            int d=n%10;
            sum=sum+d;
            n=n/10;
        }
        return sum;
}

// % operator is used for calculating remainder.

Sudarshan Taparia
  • 183
  • 1
  • 4
  • 19
0
You can do like this,

#include<iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
  int a[5]={1,2,5,11,12};
  int length = sizeof(a)/sizeof(int);
  for(int i=0;i<length;i++)
  {
        if ( a[i] >9 )
        {
                stringstream ss;
                ss << a[i];
                string a1 = ss.str();
                const  char * s = a1.c_str();
                int sum=0;
                while (*s !='\0')
                {
                        cout<<*s<<endl;
                        sum  += (*s - '0');
                        s++;
                }
                cout<<"num:"<< sum <<"\n";
        }
  }
}
savitha A
  • 3
  • 2
-1
int A[2];
A[0] = 2;
A[1] = 10;
for (int i=0;i<2;i++) {
    if (a[i] > 9) {
        int b = a[i]%10;
        int c = a[i]/10; 
        int d = b+c;
        cout << d;
    }
}

It's only for two digit numbers(10 -99) and for more than that (after 99) we will have to change logic.

grebulon
  • 7,697
  • 5
  • 42
  • 66