版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42787086/article/details/83929122
Omit vech.h and vech.cpp
randwalk.cpp (altered)
// randwalk.cpp -- using the Vector class
// compile with the vect.cpp file
#include <iostream>
#include <cstdlib> // rand(), srand() prototypes
#include <ctime> // time() prototype
#include "vect.h"
// an meaningful test shall fix value of "steps" and "target"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0)); // seed random-number generator
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
cout << "Enter target distance (q to quit): ";
while (cin >> target)
{
cout << "Enter step length: ";
if (!(cin >> dstep))
break;
//Newly added:run N loops,use new to create an double array storing each "steps"
cout << "Enter value for number N(times you wanna test): ";
int N;
cin >> N;
double*pt = new double[N];
double max, min;
double sum = 0;
double average = 0;
for (int i = 0; i < N; i++)
{
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
}
// store steps of each test into array
pt[i] = steps;
// reset to start next loop
steps = 0;
result.reset(0.0, 0.0);
}
// calculate min,max
max = pt[0];
min = pt[0];
for (int i = 0; i < N; i++)
{
min = (min < pt[i] ? min : pt[i]);
max = (max > pt[i] ? max : pt[i]);
sum += pt[i];
}
// calculate average
average = sum / N;
// dispay max,min,average
cout << "\nAfter " << N << " tests, report is as blow: \n"
<< "Max: " << max << " steps.\n"
<< "Min: " << min << " steps.\n"
<< "Average: " << (int)average << " steps.\n\n";
delete[]pt;
cout << "Enter target distance (q to quit): ";
}
cout << "Bye!\n";
/* keep window open
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
*/
return 0;
}