0,1,2,3,…

IN:={Ø,{Ø},{Ø,{Ø}},{Ø,{Ø},{Ø,{Ø}}},…}

covariance January 11, 2012

Filed under: Uncategorized — abi007 @ 6:30 pm

// reading a text file
#include
#include
#include
#include
#include
using namespace std;

typedef struct data{
int a;
int b;
};

int main () {
vector vektor;
string line;
ifstream myfile (“c:\\text.txt”);
int s=0;
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
string str1,str2;
str1=line;
size_t pos;
pos = str1.find(” “)+1;
str2=str1.substr(pos);

stringstream ss(str1.substr(0,pos-1)),st(str2);
int s1,s2;
ss>>s1;
st>>s2;

data neu;
neu.a=s1;
neu.b=s2;

vektor.push_back(neu);

cout<<"vektor:"<<vektor[s].a<<" "<<vektor[s].b<<endl;
s++;
}
myfile.close();
}
else cout << "Fehler";
cout<<"Anzahl:\t"<<vektor.size()<<endl;

int t;
t=0;
for(int i=0; i<vektor.size(); i++){
t=t+vektor[i].a;
}
cout<<"Durchschnitt Vek.a:\t"<<t/vektor.size()<<endl;
double a_av=t/vektor.size();
t=0;
for(int i=0; i<vektor.size(); i++){
t=t+vektor[i].b;
}
cout<<"Durchschnitt Vek.b:\t"<<t/vektor.size()<<endl;
double b_av=t/vektor.size();
double u;
u=0;
for(int i=0; i<vektor.size(); i++){
u=u+(vektor[i].a-a_av)*(vektor[i].b-b_av);
}
cout<< "empirische Kovarianz:\t"<<u/vektor.size()<<endl;
cout<< "geschaetzte Kovarianz:\t"<<u/(vektor.size()-1)<<endl;
cin.get();
return 0;
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.