# Summation of elements of unknown array in group

Dear all,

I have unknown array of 500 numbers. Based on the criteria i need to sum 100 elements sometimes , sometimes 20 elements like that…

sum 1 : 100 elements of unknown array

sum2 : 20 elements of unknown array

sum3: 80 elements of unknown array

sum4: 100 elements of unknown array

sum5 : 100 elements of unknown array

and i need to print out the sums .

How do i can do that ?

best regards,

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

I don’t see how this is related to ROOT at all, but anyway, what do you mean by “unknown array”? Which format are these arrays (C arrays, std::vector, std::array) ?

yes, this is not root related .

c arrays.

``````int sum2 = 0;
int arr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
for (int i = 0; i < 20; ++i) {
sum2 += arr[i];
}
``````

or:

``````int sum2 = 0;
int arr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
for (int &v : arr) {
sum2 += v;
}
``````

This is not the case.

suppose the array is arr={1,1,1,1,1,1 , 0.1,0.1,0.1,0.1,0.1,0.1, -1,-1,-1,-1,-1,-1]

arr={xcordinate,x1coordinate,ycoordinate,y1coordinate,zcoordinate,z1coordinate,…so on }

so if (xcoordinate-x1coordinate ==0.0 && ycoordinate-y1coordinate ==0 && zcoordinate-z1coordinate=0)

{ do the sum}

So here, first 6 numbers are 1 , second 6 numbers are 0.1 and third 6 numbers are -1.
I have to do summation in a single loop of first 6 numbers , 2nd 6 numbers and third 6 numbers.

so result will be like

1
1
1
1
1
1
sum 6

0.1
0.1
0.1
0.1
0.1
0.1
sum 0.6

-1
-1
-1
-1
-1
-1
sum -6

the problem is in the transition when 1 changes to 0.1 or 0.1 changes to -1.

how do i group first 6 numbers , second 6 numbers , third 6 numbers ?

thanks

Here is a solution for your example:

``````   int j = 0;
float sum = {0.0, 0.0, 0.0};
float arr = {1.,1.,1.,1.,1.,1.,0.1,0.1,0.1,0.1,0.1,0.1,-1.,-1.,-1.,-1.,-1.,-1.};
sum = arr;
for (int i = 1; i < 18; ++i) {
if (arr[i-1]-arr[i] == 0.0) {
sum[j] += arr[i];
} else {
++j;
sum[j] += arr[i];
}
}
std::cout << "sum = " << sum << "\n";
std::cout << "sum = " << sum << "\n";
std::cout << "sum = " << sum << "\n";
``````

And it gives:

``````C:\Users\bellenot\rootdev>root -l sum_array.C
root 
Processing sum_array.C...
sum = 6
sum = 0.6
sum = -6
root 
``````

But I don’t have a solution which is valid for all possible use cases…

Better one:

``````   int j = 0;
float arr = {1.,1.,1.,1.,1.,1.,0.1,0.1,0.1,0.1,0.1,0.1,-1.,-1.,-1.,-1.,-1.,-1.};
std::vector<float> sum = {arr};
for (int i = 1; i < 18; ++i) {
if (arr[i-1]-arr[i] == 0.0) {
sum[j] += arr[i];
} else {
++j;
sum.push_back(arr[i]);
}
}
j = 0;
for (float &v : sum) {
std::cout << "sum[" << j << "] = " << sum[j] << "\n";
++j;
}
``````

Thank you very much for the codes. I will try to implement them and will let you know.

Here in the code the number of elements are known but in the real case i do not the number of elements in the array.

``````float arr[] = {0., 0., 0.};
std::cout << (sizeof(arr) / sizeof(arr)) << "\n";
``````