@Jim: Well I want to make it open for a variable amount of elements. Because later on I will have to use File I/O.
To answer your question: no. There is no such short-hand syntax in C++. You have to use a loop.
If you want to take the average of N elements, then:
average = std::accumulate(temperature.begin() + index, temperature.begin() + index + N, 0) / N;
Loop from 0 to M - N (where M is number of elements and N number of elements you want to average)
Or
average = std::accumulate(temperature.begin() + index - N/2, temperature.begin() + index + N/2, 0) / N;
Loop from N/2 to M - N/2 (where M is number of elements and N number of elements you want to average - 1 [ie if you want to average 3 elements, N = 2])
Disclaimer: This is not tested code.
First I suggest you review how a moving average, or Boxcar Averaging actually works. Second if you are going to have an unknown number of data elements you should be using the push_back() method of inserting your data elements instead of trying to use a fixed sized vector.
Jim
I will do Jim's advice in am minute but here's my attempt. The only question I have that I have to adjust the number_of_elements_avg accordingly since that's going to vary on how many point averages I want otherwise it wont run. Since no value is being put to number_of_elements_avg.
Thanks Elysia!Code:for (index ==0; numberofterms-number_of_elements_avg) { average = std::accumulate(temperature.begin()+index,temperature.begin()+index+number_of_elements_avg,0)/number_of_elements_avg; }
It would be better if you posted code that would actually compile...
index == 0 should be int index = 0.
Your are missing index++.
You should initialize number_of_elements_avg to the number you actually want - 1. You can get this from the user or hard-code it. It's up to you.
And be sure to test it!
I found more errors, so again, revised code:
average = std::accumulate(temperature.begin() + index - N/2, temperature.begin() + index + N/2 + 1, 0) / (N + 1);
If we say we want to average 3 elements (N=2), then the code becomes:
average = std::accumulate(temperature.begin() + index - 1, temperature.begin() + index + 2, 0) / 3;
If index = 1, then this will average element 0 (index - 1 = 1 - 1 = 0) to 2 (index + 2 = 1 + 2 = 3). Remember that the second argument shall be the argument just after the last one you want to add.
Divide by 3 after that and you have your avg.