Iv been trying for a while now to learn C++, but it seems and inpossible task. I have a few questions about the following code.
What is going on with hits suddenly having letters and crap inside it like r, and i?
And whats all the dividing about (n / 10)? Or the last line of code return rand() % n; correct me if IM wrong, but than n is the functions n and not the data entered my me with cin>> correct? Anyway I fill like shooting myself trying to learn this programming language. IM in the fifth chapter of C++ without fear. The code is below.#include #include #include #include
using namespace std;
int rand_0toN1(int n);
srand(time(NULL)); // Set seed for random numbers.
cout << "Enter number of trials to run and press ENTER: ";
cin >> n;
// Run n trials. For each trial, get a number from 0 to 9
// and then increment the corresponding element in the
// hits array.
for (i = 1; i <= n; i++)
r = rand_0toN1(10);
// Print all the elements in the hits array, along with
// the ratio of hits to the EXPECTED hits (n / 10).
for (i = 0; i < 10; i++)
cout << i << ": " << hits[i] << " Accuracy: ";
cout << static_cast<double>(hits[i]) / (n / 10) << endl;
// Random 0-to-N1 Function.
// Generate a random integer from 0 to N-1.
int rand_0toN1(int n)
return rand() % n;