From: Per W. <pw...@ia...> - 2004-09-11 17:55:15
|
Modify rule 2 below to be system("PAUSE"); About the random generation. It is very, very wrong. 1) You say you need 6 random numbers, but you run your generation loop 49 times (1..49). 2) You never assign an random number anywyere... Your loop contains: temp = nums[i]; nums[i] = nums[r] = temp; In the first assignment, nums[i] hasn't been initialized yet, so temp will be set to an unknown value (which isn't the same as setting it to a random value). In the second assignment, you take temp (which has an undefined value) and assign to the i:th and the r:th element... I would assume that you were planning on filling the array with the values 1..49, and then perform 49 permutations to get the values 1..49 in random order, to make sure that the 6 values you print doesn't contain any repetition. for (i = 1; i < 50; i++) nums[i] = i; for (i = 1; i < 50; i++) { r = (rand() % 49) + 1; temp = nums[i]; nums[i] = nums[r]; nums[r] = temp; } One other thing. I think it is better to always use all elements of an array, i.e. use index 0..48 instead of creating the array one element larger and skip nums[0]. /Per W On Sat, 11 Sep 2004, Scott Simontis wrote: > Three solutions: > 1. Run your program from a console prompt > 2. Include cstdlib and add the line system("APUSE); before return 0 > 3. Create a useless variable and add a cin statement for it before the > return 0 > > --Scott Simontis-- > > > > Meyer von Landenhausen wrote: > > > hi please take a look at the source: > > > > #include <ctime> > > #include <iostream> > > using namespace std; > > > > int main() > > { > > char buffer[3]; > > int i,r,temp; > > int nums[50]; > > > > > > for(i = 1; i < 50; i++) > > { > > r = (rand() % 49) + 1; > > temp = nums[i]; nums[i] = nums[r] = temp; > > } > > > > > > cout << "Your six lucky numbers are...\n"; > > cout << itoa(nums[1], buffer, 10) << " "; > > cout << itoa(nums[2], buffer, 10) << " "; > > cout << itoa(nums[3], buffer, 10) << " "; > > cout << itoa(nums[4], buffer, 10) << " "; > > cout << itoa(nums[5], buffer, 10) << " "; > > cout << itoa(nums[6], buffer, 10) << " "; > > cout << endl; > > return 0; > > } > > > > > > i cant run without that win2k pro automaticly closes the apllication > > after i ran it - what kind of code should i inserted therefore i take > > a look first at the code before it closes it!? > > > > btw the prog shall creat 6 different 2-digit long numbers. > > > > thx a lot guys > > > > _________________________________________________________________ > > It's fast, it's easy and it's free. Get MSN Messenger today! > > http://www.msn.co.uk/messenger > > > > > > > > ------------------------------------------------------- > > This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170 > > Project Admins to receive an Apple iPod Mini FREE for your judgement on > > who ports your project to Linux PPC the best. Sponsored by IBM. > > Deadline: Sept. 13. Go here: http://sf.net/ppc_contest.php > > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170 > Project Admins to receive an Apple iPod Mini FREE for your judgement on > who ports your project to Linux PPC the best. Sponsored by IBM. > Deadline: Sept. 13. Go here: http://sf.net/ppc_contest.php > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |