Hi,
I'm coding a program which indicates how many times a number ranging from 0 to max is casted (max is an integer chosen by the user). I'm using the same random number generator as in "C++ without fear". However, when compiled, the program outputs right and wrong results . What's wrong?
Code:
#include <iostream>
#include <ctime>
#include <cmath>
#include <stdlib.h>
using namespace std;
int rand_0toN1(int n);
int main() {
for (;;) {
int n, i, r, max;
srand(time(NULL));
cout << "The highest number to be generated: "; cin >> max;
int hits[max];
cout << "Enter how many trials: "; cin >> n;
for (i = 0; i < n; i++) {
r = rand_0toN1(max);
hits[r]++;
}
for (i = 0; i < max; i++) {
cout << i << ": " << hits[i] << " Accuracy: ";
double results = hits[i];
cout << results / (n / max) << endl;
}
}
return 0;
}
int rand_0toN1(int n) {
return rand() % n;
}
#include <iostream>
#include <ctime>
#include <cmath>
#include <stdlib.h>
using namespace std;
int rand_0toN1(int n);
int hits[0];
int main() {
for (;;) {
int n, i, r, max;
srand(time(NULL));
cout << "The highest number to be generated: "; cin >> max;
cout << "Enter how many trials: "; cin >> n;
for (i = 0; i < n; i++) {
r = rand_0toN1(max);
hits[r]++;
}
for (i = 0; i < max; i++) {
cout << i << ": " << hits[i] << " Accuracy: ";
double results = hits[i];
cout << results / (n / max) << endl;
}
}
return 0;
}
int rand_0toN1(int n) {
return rand() % n;
}
You're right. I needed to initialize the array from outside the main function. Thanks
that is declaring the array with size 0 (I'm not sure if that is legal or not).
It's not legal to specify a static array (or dynamic array) of size 0. This is actually used as a way to implement compile-time assertions.
Note however, that the OP's original program uses neither; it uses a variable length array, which is a language extension. IMO, it's better to just use a std::vector instead. In that case the initialization is also very simple.
Code:
std::vector<int> hits(max, 0);
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
cout << "The highest number to be generated: "; cin >> max;
int hits[max];
It is illegal in C++ to declare an array using a variable as the number of items in the array.
Code:
int main()
{
int x = 10;
int hits[x]; // error
}
This code should not compile if compiled using ANSI C++. More than likely, you're using gcc or an IDE that uses the gcc compiler underneath. If so, you must change the compiler settings:
-Wall -pedantic
Once you do that, then you will see that your code will not compile. If this is a C++ course or homework, then you must change it to compile ANSI C++ and not use language extensions, otherwise you may get points taken off for using non-C++ code.
Once you see that your code doesn't compile, the real alternative is to use std::vector<> as a dynamic array of hits.
Code:
#include <iostream>
#include <ctime>
#include <cmath>
#include <stdlib.h>
#include <vector>
using namespace std;
int rand_0toN1(int n);
int main() {
for (;;) {
int n, i, r, max;
srand(time(NULL));
cout << "The highest number to be generated: "; cin >> max;
std::vector<int> hits(max, 0);
cout << "Enter how many trials: "; cin >> n;
//...
Bookmarks