I am new to programming, my vectors dataX and dataY for phase 2 of the program are returning the same values every time. I do not know why. Please help. I have spent several hours trying to figure it out, is the error in the calculations, or something to do with rand()? Please help.
#include "stdafx.h"
#include "iostream"
#include <vector>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <ctime>
#include <random>
using namespace std;
int phaseTwoSimulate(double &Xpos, double &Ypos, int diameter, double stepSize, int randomHelper) {
int repeateTimes = 0;
while (pow(Xpos - 200, 2) + pow(Ypos - 200, 2) < pow(diameter, 2)) {
int direction = rand() % 8;
repeateTimes = repeateTimes + 1;
switch (direction) {
case 0://north
Ypos = Ypos - stepSize;
break;
case 1://east
Xpos = Xpos + stepSize;
break;
case 2://south
Ypos = Ypos + stepSize;
break;
case 3://west
Xpos = Xpos - stepSize;
break;
case 4://north east
Xpos = Xpos - sqrt(pow(stepSize, 2) / 2);
Ypos = Ypos + sqrt(pow(stepSize, 2) / 2);
break;
case 5://south east
Xpos = Xpos + sqrt(pow(stepSize, 2) / 2);
Ypos = Ypos + sqrt(pow(stepSize, 2) / 2);
break;
case 6://south west
Xpos = Xpos + sqrt(pow(stepSize, 2) / 2);
Ypos = Ypos - sqrt(pow(stepSize, 2) / 2);
break;
case 7://north west
Xpos = Xpos - sqrt(pow(stepSize, 2) / 2);
Ypos = Ypos - sqrt(pow(stepSize, 2) / 2);
break;
default:
cout << "error in randomizer" << endl;
exit(5);
break;
}
}
return repeateTimes;
}
void phaseTwoWalk(int &loopCount, vector<int>&dataX, vector<int>&dataY) {
int totalXpos = 0;
int totalYpos = 0;
loopCount = loopCounter();
int endXpos = 0;
int endYpos = 0;
double Xpos = 201;
double Ypos = 201;
double stepSize = 0;
int diameterCircle = getDiameter(stepSize);
dataX.clear();
dataY.clear();
for (int repititions = 0; repititions < loopCount; repititions++) {
int randomHelp = 0;
repititions = phaseTwoSimulate(Xpos,Ypos,diameterCircle,stepSize,randomHelp);
dataX.push_back(Xpos);
dataY.push_back(Ypos);
randomHelp++;
int Xpos = 201;
int Ypos = 201;
}
}
void phaseTwoCalculations(vector<int>dataX, vector<int>dataY) {
double dataSizeX = dataX.size();
double dataSizeY = dataY.size();
int modeX = 0;
int modeY = 0;
double maxX = *max_element(dataX.begin(), dataX.end());
double minX = *min_element(dataX.begin(), dataX.end());
double maxY = *max_element(dataY.begin(), dataY.end());
double minY = *min_element(dataY.begin(), dataY.end());
double middleX = dataSizeX / 2;
double middleY = dataSizeY / 2;
double sumX = accumulate(dataX.begin(), dataX.end(), 0);
double sumY = accumulate(dataY.begin(), dataY.end(), 0);
sort(dataX.begin(), dataX.end());
sort(dataY.begin(), dataY.end());
modeX = findMode(dataX);
modeY = findMode(dataY);
cout << "The center of the circle is 201,201" << endl;
cout << "The mean variances from starting position are (x,y): " << (fabs((sumX / dataSizeX)) - 201) <<
"," << (fabs((sumY / dataSizeY) - 201)) << endl;
cout << "The mean of ending x,y positions are: " << sumX / dataSizeX << "," <<
sumY / dataSizeY << endl;
cout << "The minimum ending x,y positions are: " << minX << "," << minY << endl;
cout << "The maximum ending x,y positions are: " << maxX << "," << maxY << endl;
cout << "The medians ending x,y positions are: " << dataX[middleX] << "," << dataY[middleY] << endl;
cout << "The mode of ending x,y positions are: " << modeX << "," << modeY << endl;
//add the number of steps average, repititions.
cout << "Press enter to exit." << endl;
cin.ignore();
cin.get();
exit(1);
}
int main()
{
int repititions;
vector<int>phaseOneData;
vector<int>phaseTwoDataX;
vector<int>phaseTwoDataY;
srand(time(NULL));
if (phase == 2) {
phaseTwoWalk(repititions, phaseTwoDataX, phaseTwoDataY);
phaseTwoCalculations(phaseTwoDataX, phaseTwoDataY);
}
Aucun commentaire:
Enregistrer un commentaire