Adding digits of array values

Hey guys!

I'm new to C++ and i was trying to understand how to work with arrays. The idea I have is
1) I wanted a user to input an array, the program to output the array
2) Double all the values in the array and output that
3) And for each of the doubled values, add the digits of the doubled number (1 digit would remain the same), then output the new numbers as well.

(e.g. if the Nth array value was 8, then 8*2=16. Then the program would perform 1+6=7)

I put my code to show my progress, feel free to point out any errors along the way!

Any help is appreciated!

p.s. The nested loop didn't work :(

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
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>

using namespace std;

int maxNum;
int num[20];


int main()
{
  cout << "Enter an Array" << endl;
  for (int i=0;i<20;i++)
  {
      cin >> num[i];
      maxNum++;
      if (num[i]==-1)
      break;
  }
  
  cout <<"Your array is: " << endl;
  
  for (int i=0;i<maxNum-1;i++)
  cout << num[i];
  
  cout << endl;
  
    cout << "Your doubled array is:" << endl;
    
    for (int j=0;j<maxNum-1;j++)
    {
    num[j]*=2;
    cout << num[j];
    }
    
    cout << endl;
    
    cout << "When the digits of each seat are added..." << endl;
    
    for (int k=0;k<maxNum;k++)
    {
       for (int l=0;l<maxNum;l++)
       {
           int sum[20];
           while (num[k]!=0)
           {
           sum[l]=sum[l]+num[k]%10;
           num[k]=num[k]/10;
           }
       }
       cout << sum[l];
       
    
    }
    
    
    
    cout << endl;
}


Last edited on
In C++ c-style arrays are a kind of obsolete - don't waste time learning them.
You should better learn to use std::vector:
https://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

And for each of the doubled values, add the values together, then output the new numbers as well.
Not sure what you want to do.
Lets say the original array is [1, 2, 3, 4]
Doubled array is [2, 4, 6, 8]
What value would you like to add ?
hahaha, it's a necessary step for my course, unfortunately.

It was more like, say the array was [5, 6, 7, 8]
The doubled values are [10, 12, 14, 16]

Then you would add the digits together and create new numbers in the array.

So the final array is [1+0, 1+2, 1+4, 1+6]
which gives [1, 3, 5, 7]

I apologise, that was poorly worded.
OK I understand now.
Easiest would be to create a function int sum_of_digits(int num) and call it for each value in the doubled array and store the value in the final array.
If you are not allowed to use functions then try this;
1
2
3
4
5
6
7
8
9
10
11
12
  cout << "When the digits of each seat are added..." << endl;
  for (int k=0;k<maxNum;k++)
  {
     int num = num[k];
     int sum = 0;
     while (num !=0 )
     {
           sum += num % 10;
           num = num / 10;
      }
      num[k] = sum
   }
Topic archived. No new replies allowed.