Hey Guys,
I am wondering if you guys can help me fix some of the errors I am getting. I want to do a running average for example like three points.
Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
//Running Mean for 3 points with even weights
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <numeric>
using namespace std;
int main ()
{
float numberofterms;
float number_of_elements_avg;
flot tempstorage;
vector<float> temperature; //Define a vector of 5 floats
vector<float> movingavg;
int i; //Loop counter
cout<<"Enter the number of terms you want: " <<endl;
cin>>numberofterms;
cout<<"Enter how many pt average you would like to take: "<<endl;
cin>>number_of_elements_avg;
for (i=0; index<numberofterms; i++)
{
float average;
cout<<"Enter temperature you want to input in Celsius"<<(index+1);
cout<<": ";
cin>>tempstorage;
temperature.push_back(tempstorage);
if (i=0)
{
movingavg[index]=temperature[index];
cout<<"Temperature [1]= "<<temperature[0]<<" C"<<endl;
continue;
}
else if (i=numberofterms-1)
{
movingavg[index]=temperature[index];
cout<<"Temperature ["<<numberofterms<<"]= "<<temperature[numberofterms-1]<<" C"<<endl;
continue;
}
for (int j=1; j<numberofterms; numberofterms-number_of_elements_avg; j++);
{
average = accumulate(temperature.begin() + index - number_of_elements_avg/2, temperature.begin() + index + number_of_elements_avg/2 + 1, 0) / (number_of_elements_avg + 1);
}
//NOT PART OF THE CODE average= (temperature[index-1]+temperature[index]+temperature[index+1]/3);
//NOT PART OF THE CODE movingavg[index]=average;
cout<<"Temperature ["<<index+1<<"] = "<<average<<" C"<<endl;
cout<<"The original temperature ["<< index+1<<"]"<<temperature[index]<<"] C has been replaced." <<endl;
}
system ("PAUSE");
return 0;
}
|
Errors that I am getting that I may have resolved:
The errors just wouldn't line up with the line numbers in the posted code, so here are the warnings I got when compiling:
Warning 1 warning C4553: '==' : operator has no effect; did you intend '='? 24
Warning 2 warning C4244: 'argument' : conversion from 'float' to 'unsigned int', possible loss of data 42
Warning 3 warning C4258: 'index' : definition from the for loop is ignored; the definition from the enclosing scope is used 47
Warning 4 warning C4244: 'argument' : conversion from 'float' to '__w64 int', possible loss of data 47
Warning 5 warning C4258: 'index' : definition from the for loop is ignored; the definition from the enclosing scope is used 47
Warning 6 warning C4244: 'argument' : conversion from 'float' to '__w64 int', possible loss of data 47
Warning 7 warning C4258: 'index' : definition from the for loop is ignored; the definition from the enclosing scope is used 51
Warning 8 warning C4258: 'index' : definition from the for loop is ignored; the definition from the enclosing scope is used 52
Warning 9 warning C4258: 'index' : definition from the for loop is ignored; the definition from the enclosing scope is used 52
Warning 10 warning C6246: Local declaration of 'index' hides declaration of the same name in outer scope. For additional information, see previous declaration at line '16' of '': Lines: 16 45
Warning 11 warning C6001: Using uninitialized memory 'index': Lines: 12, 13, 14, 15, 16, 18, 19, 20, 21, 24 24
Warning 12 warning C4700: uninitialized local variable 'index' used 24
First warnings says you messed up the loop initialization.
Second warnings says you are using a float as index to your vector. An index can only be a whole number. Element 3.5 does not exist, after all.
Warnings 3, 5, 7-9 comes from your messed up index variable and because you use the same index variable in both loops.
Warning 4 and 6 comes from your line calculating the average. You can only have a whole number of elements you want to average. You can't average 3.5 elements.
Warning 10 says you are re-using the same variable name for the second loop as for the first.
Warning 11 and 12 says you are using index but never initialized it.
Can you guys help me? Thanks.