1

I want to calculate the sum of digits in each elements in array. The problem is with this code it only calculates the the sum of odd indexes (1,3,5...) in array. And in console it shows some random numbers for even indexes (0,2,4...)

Can anybody tell me what is the problem?

And yes I need to use it as array

Here are output values:

Enter how many numbers you want to calculate sum of digits: 5
Enter those numbers: 12
Enter those numbers: 33
Enter those numbers: 44
Enter those numbers: 22
Enter those numbers: 33
Sum of 0 number is: 4
Sum of 1 number is: 6
Sum of 2 number is: 40
Sum of 3 number is: 4
Sum of 4 number is: 11730950
#include <iostream>


int main(int argc, char** argv) 
{
    int n;
    int temp;
    int pom;

    cout << "Enter how many numbers you want to calculate sum of digits: ";
    cin >> n;

    int numbers[n];
    int sum[n];

    for (int i = 0; i < n; i++)
    {
        cout << "Enter those numbers: ";
        cin >> numbers[i];
    }

    for (int i = 0; i < n; i++)
    {
        while (numbers[i] > 0)
        {
        temp = numbers[i] % 10;
        sum[i]+= temp;
        numbers[i] = numbers[i]/10; 
        }

    }



    for (int i = 0; i < n; i++)
    {
        cout << "Sum of " << i << " number is: " << sum[i] << endl;
    }

    return 0;
}
Samed Škulj
  • 29
  • 1
  • 7
  • use `std:.vector` for dynamic arrays. [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Apr 23 '20 at 13:34
  • 1
    What makes you think it only calculates the sum of the odd numbers? Please quote the input numbers you are using, the output that you see and the output that you expected. – john Apr 23 '20 at 13:34
  • Enter how many numbers you want to calculate sum of digits: 5 Enter those numbers: 12 Enter those numbers: 33 Enter those numbers: 44 Enter those numbers: 22 Enter those numbers: 33 Sum of 0 number is: 4 Sum of 1 number is: 6 Sum of 2 number is: 40 Sum of 3 number is: 4 Sum of 4 number is: 11730950 – Samed Škulj Apr 23 '20 at 13:36
  • 2
    @SamedŠkulj Thanks, well cigien has given you the answer (which I should have been able to spot without the extra information). But in general when asking about code that doesn't work, always include the data you are testing with. – john Apr 23 '20 at 13:38

2 Answers2

5

You need to initialize the sum array, like this:

int sum[n] {};

otherwise, the first time you read from an element of sum you have undefined behaviour.

Also, variable length arrays are not part of standard c++. If you don't know the size of the array at compile time, just use a std::vector.

If you absolutely must use an array, then you will need to dynamically allocate it, like this:

int * arr = new int[n]{};
cigien
  • 57,834
  • 11
  • 73
  • 112
0
#include <iostream>
using namespace std;
int main() {
    int a,temp,sum=0;
    cin>>a;
    int arr[a];
    for(int i=0;i<a;i++)
    {
        cin>>arr[i];
    }
    for(int i=0;i<a;i++)
    {
        sum=0;
        while(arr[i]>0)
        {
            temp=arr[i]%10;
            sum+=temp;
            arr[i]=arr[i]/10;
        }
        cout<<sum<<" ";
    }

}
Yashwant
  • 1
  • 1
  • 1
    1. Both of your loops iterate over the same values, so you can merge them into one loop. 2. Use better names for your variables, even for sample code. – Shashank Garg Aug 31 '22 at 16:46