-
August 4th, 2011, 10:02 AM
#1
[RESOLVED] What's wrong?
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;
}
-
August 4th, 2011, 10:18 AM
#2
Re: What's wrong?
You might want to try initializing all elements of the "hits" array to 0.
I don't know if your declaration does that, since it is non-standard.
-
August 4th, 2011, 10:35 AM
#3
Thank
Is that what you meant?
Code:
#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
-
August 4th, 2011, 10:37 AM
#4
Re: [RESOLVED] What's wrong?
No, the problem is you're calling srand inside your loop. You should only call it once.
-
August 4th, 2011, 10:47 AM
#5
Re: [RESOLVED] What's wrong?
this is not correct ...
that is declaring the array with size 0 (I'm not sure if that is legal or not).
In your "for(;;)" loop, you need to initialize the array to 0 ...
Code:
int hits[max];
for (i=0; i<max; ++i)
hits[i] = 0;
As mentioned, you only need to call srand() once at the start of the program.
But your usage is OK.
-
August 4th, 2011, 11:27 AM
#6
Re: [RESOLVED] What's wrong?
Originally Posted by Philip Nicoletti
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
-
August 4th, 2011, 12:02 PM
#7
Re: Thank
Originally Posted by CodingFreak
Is that what you meant?
No, this:
Code:
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:
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;
//...
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|