Help with program
-
I'm having trouble getting the following program to compile. I'm trying to write a program that will generate a random number (say our of 150 numbers for example). What am I doing wrong? My error message is 2601. I'm defining something wrong it appears.
#include <stdlib.h> #include <iostream.h> void main() { int nrand (int n) { int x = RAND_MAX / n; int r; do r = rand() / x; while (r >= n); cout< Thanks, Dave :-D _"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson_
-
I'm having trouble getting the following program to compile. I'm trying to write a program that will generate a random number (say our of 150 numbers for example). What am I doing wrong? My error message is 2601. I'm defining something wrong it appears.
#include <stdlib.h> #include <iostream.h> void main() { int nrand (int n) { int x = RAND_MAX / n; int r; do r = rand() / x; while (r >= n); cout< Thanks, Dave :-D _"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson_
can you reformat the listing so it is easier to read ? (use the > < buttons below ) also, use
srand
to regenerate the random number generator (look in msnd for a full example)
Maximilien Lincourt "Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )
-
I'm having trouble getting the following program to compile. I'm trying to write a program that will generate a random number (say our of 150 numbers for example). What am I doing wrong? My error message is 2601. I'm defining something wrong it appears.
#include <stdlib.h> #include <iostream.h> void main() { int nrand (int n) { int x = RAND_MAX / n; int r; do r = rand() / x; while (r >= n); cout< Thanks, Dave :-D _"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson_
Some of the most immediate problems are your #includes aren't correct. Also (unless you have a custom stdlib.h) there is a typo - your first include should be stdlib, not cstdlib. the #includes should look like #include <stdlib.h> (actually, you probably had it correct, but because they look like HTML tags to the server processor, they got stripped). Also namespace is a C++ specific feature but in C++ you can't have locally defined functions (your int nrand(int n). You'll need to break out your nrand and call it from your main. Create a NRand class and header file and save them in their own source files. #include the header in your main source file. Then you'll need to call the class with an integer. I didn't even look at the logic contained in your loop. Try to get a good compile and then check back in!
-
I'm having trouble getting the following program to compile. I'm trying to write a program that will generate a random number (say our of 150 numbers for example). What am I doing wrong? My error message is 2601. I'm defining something wrong it appears.
#include <stdlib.h> #include <iostream.h> void main() { int nrand (int n) { int x = RAND_MAX / n; int r; do r = rand() / x; while (r >= n); cout< Thanks, Dave :-D _"The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson_
Look at my modification:
#include
#includeusing namespace std;
int nrand (int n){
int x = RAND_MAX / n;
int r;do r = rand() / x;
while (r >= n);
cout<Allan
-
Look at my modification:
#include
#includeusing namespace std;
int nrand (int n){
int x = RAND_MAX / n;
int r;do r = rand() / x;
while (r >= n);
cout<Allan
Hint: if you have a space before and after the < > they get not deleted by the stupid HTML-interpreter at CP. I did find a few things in your program. First: In your
main()
, you need to seed the random-generator, or you will get the same list of random numbers over and over. Usesrand()
for that. You can find an example how to do it in the MSDN. Second: You should do thecout
in your main, after the call to yournrand
-function. Separates the logic in natural parts:nrand()
gets you a number, but does not say what to do with it, andmain()
uses the number to do whatever must be done, but does not bother how to generate it. Third: Your main sould be of return typeint
, and you doreturn 0;
if all went well, otherwise something different (right now cant see why you would want to return anything other than 0.
Who is 'General Failure'? And why is he reading my harddisk?!?
-
Hint: if you have a space before and after the < > they get not deleted by the stupid HTML-interpreter at CP. I did find a few things in your program. First: In your
main()
, you need to seed the random-generator, or you will get the same list of random numbers over and over. Usesrand()
for that. You can find an example how to do it in the MSDN. Second: You should do thecout
in your main, after the call to yournrand
-function. Separates the logic in natural parts:nrand()
gets you a number, but does not say what to do with it, andmain()
uses the number to do whatever must be done, but does not bother how to generate it. Third: Your main sould be of return typeint
, and you doreturn 0;
if all went well, otherwise something different (right now cant see why you would want to return anything other than 0.
Who is 'General Failure'? And why is he reading my harddisk?!?