My plan was to make a random generator for a d20 roll and to "x" number of rolls whilst adding each roll to a vector. Then I wanted to make a standard deviation function that would output the standard deviation of the rolls stored in the passed vector. I keep getting build errors and I can't figure out why.
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cmath>
#include <vector>
usingnamespace std;
float stdDev(vector<int> list){
//find the mean
int sum=0;
float mean=0;
for(int n=0; n < list.size(); n++){
sum += list[n];
}
mean = sum/(list.size());
cout << "The mean value of the rolls is: " << mean << endl;
//find the standard deviation
vector<float> variances;
float varianceSum=0;
for(int i=0; i < list.size(); i++){
float deviation = pow((list[i]-mean),2);
variances.push_back(deviation);
}
for(int n=0; n < variances.size(); n++){
varianceSum += variances[n];
}
double stdSqr = (varianceSum)/(list.size());
double STD = sqrt((varianceSum)/(list.size()));
cout << "The standard deviation of the rolls is: " << STD << endl;
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
srand(static_cast<unsignedint>(time(0)));
cout << "How many times would you like to roll the 1d20?" << endl;
int rollNum;
vector<int> ROLLS;
cin >> rollNum;
for(int i=0; i<=rollNum; i++){
int randomNum = rand();
int roll = (randomNum %20) +1;
ROLLS.push_back(roll);
cout << "Roll #" << i << " is: " << roll << endl;
}
stdDev(ROLLS);
system("pause");
return 0;
}
if by the output you meant the following, then sure:
1 2 3 4 5 6 7 8
1
1>c:\Users\Owner\documents\visual studio 2010\Projects\dice roller\dice roller\dice roller.vcxproj : error MSB4014: The build stopped unexpectedly because of an internal failure.
1>c:\Users\Owner\documents\visual studio 2010\Projects\dice roller\dice roller\dice roller.vcxproj : error MSB4014: Microsoft.Build.Exceptions.BuildAbortedException: Build was canceled. MSBuild.exe could not be launched as a child node as it could not be found at the location "C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe". If necessary, specify the correct location in the BuildParameters, or with the MSBUILD_EXE_PATH environment variable.
1>c:\Users\Owner\documents\visual studio 2010\Projects\dice roller\dice roller\dice roller.vcxproj : error MSB4014: at Microsoft.Build.BackEnd.NodeManager.AttemptCreateNode(INodeProvider nodeProvider, NodeConfiguration nodeConfiguration)
1>c:\Users\Owner\documents\visual studio 2010\Projects\dice roller\dice roller\dice roller.vcxproj : error MSB4014: at Microsoft.Build.BackEnd.NodeManager.CreateNode(NodeConfiguration configuration, NodeAffinity nodeAffinity)
1>c:\Users\Owner\documents\visual studio 2010\Projects\dice roller\dice roller\dice roller.vcxproj : error MSB4014: at Microsoft.Build.Execution.BuildManager.PerformSchedulingActions(IEnumerable`1 responses)
1>c:\Users\Owner\documents\visual studio 2010\Projects\dice roller\dice roller\dice roller.vcxproj : error MSB4014: at Microsoft.Build.Execution.BuildManager.HandleNewRequest(Int32 node, BuildRequestBlocker blocker)
1>c:\Users\Owner\documents\visual studio 2010\Projects\dice roller\dice roller\dice roller.vcxproj : error MSB4014: at Microsoft.Build.Execution.BuildManager.IssueRequestToScheduler(BuildSubmission submission, Boolean allowMainThreadBuild, BuildRequestBlocker blocker)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Ran it on my Mac and it worked. Just changed the _tmain to main and got rid of the windows header.
1 2 3 4 5 6 7 8 9 10
$ ./a.out
How many times would you like to roll the 1d20?
4
Roll #0 is: 12
Roll #1 is: 18
Roll #2 is: 12
Roll #3 is: 6
Roll #4 is: 3
The mean value of the rolls is: 10
The standard deviation of the rolls is: 5.2345
Just made two changes. Again I am on a MAC. You are also getting one more roll than I think you want. I wanted 4 rolls and got 5. That is because a <= what should be <
//-----------------------------------
// Removed this
//#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cmath>
#include <vector>
usingnamespace std;
float stdDev(vector<int> list){
//find the mean
int sum=0;
float mean=0;
for(int n=0; n < list.size(); n++){
sum += list[n];
}
mean = sum/(list.size());
cout << "The mean value of the rolls is: " << mean << endl;
//find the standard deviation
vector<float> variances;
float varianceSum=0;
for(int i=0; i < list.size(); i++){
float deviation = pow((list[i]-mean),2);
variances.push_back(deviation);
}
for(int n=0; n < variances.size(); n++){
varianceSum += variances[n];
}
double stdSqr = (varianceSum)/(list.size());
double STD = sqrt((varianceSum)/(list.size()));
cout << "The standard deviation of the rolls is: " << STD << endl;
return 0;
}
//----------------------------------------------------
// Changed from
// int _tmain(int argc, _TCHAR* argv[])
// to
int main(int argc, char** argv)
{
srand(static_cast<unsignedint>(time(0)));
cout << "How many times would you like to roll the 1d20?" << endl;
int rollNum;
vector<int> ROLLS;
cin >> rollNum;
for(int i=0; i<=rollNum; i++){
int randomNum = rand();
int roll = (randomNum %20) +1;
ROLLS.push_back(roll);
cout << "Roll #" << i << " is: " << roll << endl;
}
stdDev(ROLLS);
return 0;
}
Oh okay, well thank you very much for taking a look at it. It works now and I just tweaked the mean finding code to be a float divided by a float--not an int / float.