Sort the integers of the sequence according to the sum of their digits.

can anybody help me , how to add digits within and array index..the problem is

Write a program that reads integer n followed by a sequence of n integers. Sort the integers of this sequence according to the sum of their digits.
For example, if the input data is
4
11111 321 39 45

The output will be
11111 321 45 39

Since the sum of their digits is 5, 6, 9 and 12 respectively.
One trick is to read the number as char array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
char numbers[100][17]; //hold 100 number strings of unto 16 chars in length
int sums[100] = {0}; //hold the sums

//read the number of numbers to read
//main loop 

cin >> numbers[curNumber];

for( i=0; i<strlen( numbers[curNumber] ); ++i )
    sums[curNumber] += (toascii( numbers[curNumber][i] ) - toascii( '0' ));

//Sort both arrays

//end main loop 


Of course this is one way of doing it, also it will only cater for 100 numbers entered (as string) and of length not exceeding 16.
Topic archived. No new replies allowed.