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

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-19-2012
Rod Micael
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.!
• 06-19-2012
laserlight
What do you mean by "iostream <vector> approach"?
• 06-19-2012
Rod Micael
I see some of the examples using iostream <vector> is that necessary?
• 06-19-2012
Elysia
What examples, and where?
• 06-19-2012
laserlight
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.
• 06-19-2012
R41D3N
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`
• 06-19-2012
Rod Micael
let me show you what I am talking about: for loop and vectors - C++ Forum
• 06-19-2012
laserlight
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.
• 06-19-2012
Rod Micael
so how would the if and else loops work in this case @R41D3N?
• 06-19-2012
R41D3N
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 ...
• 06-19-2012
Rod Micael
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.
• 06-19-2012
Rod Micael
actually I meant for line 24: T[2] = (T[0]+T[1]+T[2])/numberofterms
• 06-19-2012
Rod Micael
At least that's my way of "understanding" what you are trying to say. I know its wrong. LOL.
• 06-19-2012
R41D3N
Quote:

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.
• 06-19-2012
Rod Micael
Quote:

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?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last