# Thread: Question about using an vector iteration loop in C++---Running Mean with three values

1. ## Question about using an vector iteration loop in C++---Running Mean with three values

I am trying to figure out a general form of this hypothetical example using C++.

I have a vector with any three numbes. I want the first numbers and third numbers in the vectors to not be changed, however for the second number, I would like the number to be average with the first number, the second number, and the third number in the vector which will replace the second number in the final output.

I was wondering if iostream <vector> approach is necessary?

Code:
```#include <iostream>
#include<vectors>?
using namespace std;

int main()

{

const int numberofterms=3
double Temperature[numberofterms]={22,24,16.2}
float average

If (first vector number and third vector number are chosen:) THEN

The first vector and the third vector are kept.

Else The second vector will be replaced by the average.

average = (Temperature[0]+Temperature [1]+Temperature [2])/numberofterms;

}```
The displaying part would not be a problem but just the way to write this in C++ I am having a really difficult time to find examples of what I am looking for. Thanks again.!

2. What do you mean by "iostream <vector> approach"?

3. I see some of the examples using iostream <vector> is that necessary?

4. What examples, and where?

5. Sorry, but I don't understand what you mean by "iostream <vector>". Basically, if you have a vector of 3 numbers named x, you can use them by accessing x[0], x[1] and x[2]. You can print them like that too.

6. I guess he means something like:

Code:
```#include <iostream>
#include <vector>```

And nop that wouldnt be necessary, since its just 3(size fixed) values. You might as well use an array of size 3.

a[0], a[1], a[2] would be the elements.

Code:
`a[2] = (a[0]+a[1]+a[2])/3; // for the mean`

7. let me show you what I am talking about: for loop and vectors - C++ Forum

8. Oh, as in use a loop to print the elements of a vector? Sure, you can use that. You don't have to, but you can.

9. so how would the if and else loops work in this case @R41D3N?

10. just tell the user to enter the element he wants to switch with the average ... lets say x(0/1/2)

Code:
`a[x] = ....`

You could work vectors the same way, if thats what you are aiming for ...

11. Code:
```#include <iostream>

#include<vectors>?
using namespace std;

int main()

{

const int numberofterms=3
double T[numberofterms]={22,24,16.2}

float average

If (T[1], T[3])

cout<<T[1]<<endl;
cout<<T[2]<<endl;

Else If (which should assume T[2] if I am correct)
T[2] = (T[0]+T[1]+T[2])/3

}```
of course System("PAUSE") Return0.

12. actually I meant for line 24: T[2] = (T[0]+T[1]+T[2])/numberofterms

13. At least that's my way of "understanding" what you are trying to say. I know its wrong. LOL.

14. Originally Posted by Rod Micael
[
of course System("PAUSE") Return0.
dont use system() . If you want to hold the display screen, better use cin.get();

the way I suggested it, you wouldnt need an if-else block. Rather than user choosing which 2 to keep as is, let them choose which element to modify into the average. So as to get the index.

15. Originally Posted by R41D3N
dont use system() . If you want to hold the display screen, better use cin.get();

the way I suggested it, you wouldnt need an if-else block. Rather than user choosing which 2 to keep as is, let them choose which element to modify into the average. So as to get the index.
Code:
```#include <iostream>

#include<vectors>?
using namespace std;

int main()

{

const int numberofterms=3
double T[numberofterms]={22,24,16.2}

float average

If (T[1], T[3])

cout<<T[1]<<endl;
cout<<T[2]<<endl;

Else If

T[2] = (T[0]+T[1]+T[2])/numberofterms

cin.get()
return 0;

}```
Would this be a correct approach?