#include <stdlib.h>
#include <iostream.h>
#include <time.h>
float gleichverteilte_folge(int u, int v, int g)
{
return (u + 1 + rand()%((v-u)*g))/((float)g);
}
int randmarkov(float vara, float varb, int startvar)
{
if(startvar == 0)
{
if (gleichverteilte_folge(0,1,100) <= vara) return 1;
else return 0;
}
if(startvar == 1)
{
if (gleichverteilte_folge(0,1,100) <= varb) return 0;
else return 1;
}
}
void runsim(float vara, float varb, int startvar, int turns)
{
int loop[]={100, 200, 500, 1000};
int count, state, countloop;
float h0[turns][sizeof(loop)/sizeof(loop[0])];
float sum[sizeof(loop)/sizeof(loop[0])], average[sizeof(loop)/sizeof(loop[0])];
float paverage=0.0;
srand(time(NULL));
cout << "Experiment: " << "\t";
for(int i=0;i<sizeof(loop)/sizeof(loop[0]);i++)
{
cout << "n" << loop[i] << ":" << "\t";
}
cout << "\n";
for (int numturns=0;numturns<turns;numturns++)
{
count = 0;
countloop = 0;
state = startvar;
cout << "\n" << "\t" << numturns+1 << "\t";
for(int numloop=1;numloop<=loop[(sizeof(loop)/sizeof(loop[0]))-1];numloop++)
{
state = randmarkov(vara, varb, state);
if (state == 0)
count++;
if (numloop == loop[countloop])
{
h0[numturns][countloop]=(float)count/numloop;
cout << h0[numturns][countloop] << "\t";
countloop++;
}
}
}
cout <<'\n' << '\n'<<"Mittelwert: "<<'\t';
for (int value_1=0;value_1<(sizeof(loop)/sizeof(loop[0]));value_1++)
{
for (int value_2=0;value_2<turns;value_2++)
{
sum[value_1]=0;
}
}
for (int value_1=0;value_1<(sizeof(loop)/sizeof(loop[0]));value_1++)
{
for (int value_2=0;value_2<turns;value_2++)
{
sum[value_1]=sum[value_1]+h0[value_1][value_2];
}
}
for (int value=0;value<(sizeof(loop)/sizeof(loop[0]));value++)
{
average[value] = (1.0 / turns)* sum[value];
cout <<average[value]<<'\t';
}
cout <<'\n' << '\n'<<"Standartabweichung: "<<'\t';
for (int value_1=0;value_1<(sizeof(loop)/sizeof(loop[0]));value_1++)
{
for (int value_2=0;value_2<turns;value_2++)
{
sum[value_1]=0;
}
}
for (int value_1=0;value_1<(sizeof(loop)/sizeof(loop[0]));value_1++)
{
for (int value_2=0;value_2<turns;value_2++)
{
sum[value_1]=sum[value_1]+((h0[value_1][value_2]- average[value_1])*(h0[value_1][value_2]- average[value_1]));
}
}
for (int value=0;value<(sizeof(loop)/sizeof(loop[0]));value++)
{
paverage = (1.0 / (turns-1))* sum[value];
cout << sqrt(paverage) <<'\t';
}
}
main()
{
cout <<"Markus Köberl 0130163"<<'\n'<<"Susanne Schöberl 0130374"<<'\n'<<'\n'<<'\n';
float vara = 1.0/2.0;
float varb = 1.0/3.0;
int startvar = 0;
int turns = 8;
runsim(vara, varb, startvar, turns);
}