Stopping same number from appearing twice?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Stopping same number from appearing twice?

 
0
  #11
Jun 21st, 2008
Originally Posted by salman213 View Post
would this be better or worse ???

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int check (unsigned int checkvariable[]);
  6.  
  7. int main()
  8. {
  9. srand(time(NULL));
  10.  
  11. unsigned int lotteryBall[9];
  12.  
  13. for(int i = 0; i <=7; ++i)
  14. {
  15. lotteryBall[i] = rand() % 48 + 1;
  16.  
  17. cout << lotteryBall[i] << " ";
  18.  
  19. if(i == 7)
  20. {
  21. do {
  22. lotteryBall[i+1] = rand() % 48 + 1;
  23. }while (check(lotteryBall));
  24. cout << "*" << lotteryBall[i+1] << "*";
  25. }
  26.  
  27. }
  28.  
  29. cin.get();
  30.  
  31. return 0;
  32. }
  33.  
  34. int check ( unsigned int checkvariable[])
  35. {
  36. for(int a=0;a<=7;++a)
  37. {
  38. if (checkvariable[a]==checkvariable[8])
  39. {return true;
  40. break;}
  41. else if (a==7)
  42. return false;
  43. }
  44.  
  45. }

You have some strange formatting here. If you line things up, it'll be easier to read. I changed lines 13 and 20 from 48 to 10 to drastically increase the odds of getting repeats and I got them. You want to call your check function each time you generate a new random number (not necessary the first time), not just for the last ball. Also, you have a function that returns an integer returning true and false. While that's not an error, you should probably have the function return a boolean value.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int check (unsigned int checkvariable[]);
  5.  
  6. int main()
  7. {
  8. srand(time(NULL));
  9. unsigned int lotteryBall[9];
  10.  
  11. for(int i = 0; i <=7; ++i)
  12. {
  13. lotteryBall[i] = rand() % 10 + 1;
  14. cout << lotteryBall[i] << " ";
  15.  
  16. if(i == 7)
  17. {
  18. do
  19. {
  20. lotteryBall[i+1] = rand() % 10 + 1;
  21. }
  22. while (check(lotteryBall));
  23.  
  24. cout << "*" << lotteryBall[i+1] << "*";
  25. }
  26. }
  27.  
  28. cin.get();
  29. return 0;
  30. }
  31.  
  32. int check ( unsigned int checkvariable[])
  33. {
  34. for(int a=0;a<=7;++a)
  35. {
  36. if (checkvariable[a]==checkvariable[8])
  37. {
  38. return true;
  39. break;
  40. }
  41. else if (a==7)
  42. return false;
  43. }
  44. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Re: Stopping same number from appearing twice?

 
0
  #12
Jun 21st, 2008
GOOD POINT, never noticed that.

so how about this

  1. #include <iostream>
  2. using namespace std;
  3. bool check (unsigned int checkvariable[], int a);
  4. int main()
  5. {
  6. srand(time(NULL));
  7. unsigned int lotteryBall[9];
  8. for(int i = 0; i <=8; ++i)
  9. {
  10. do
  11. {
  12. lotteryBall[i] = rand() % 10 + 1;
  13. }
  14. while (check(lotteryBall, i));
  15.  
  16. if(i == 8)
  17. cout << "*" << lotteryBall[i] << "*";
  18. else
  19. cout << lotteryBall[i] << " ";
  20.  
  21. }
  22. cin.get();
  23. return 0;
  24. }
  25.  
  26. bool check (unsigned int checkvariable[], int a)
  27. {
  28. for(int b=0;b<=a;b++)
  29. {
  30. if (a==b)
  31. {
  32. return false;
  33. break;
  34. }
  35. if(checkvariable[b]==checkvariable[a])
  36. {
  37. return true;
  38. break;
  39. }
  40. }
  41. }



ONE QUESTION: I THOUGHT I READ THAT IF YOU WANTED TO USE THE SRAND() FUNCTION YOU MUST INCLUDE THE DIRECTIVE: ctime. In this case I did not and it still works??
Last edited by salman213; Jun 21st, 2008 at 6:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 8
Reputation: kaleshwar is an unknown quantity at this point 
Solved Threads: 0
kaleshwar kaleshwar is offline Offline
Newbie Poster

Re: Stopping same number from appearing twice?

 
0
  #13
Jun 21st, 2008
wouldn't something like this be better? I mean if c++ gives the find function why not use it?

#include <iostream>
using namespace::std;

int main()
{
	srand(time(NULL));
	unsigned int lotteryBall[7];
	unsigned int tempBall;
	unsigned int* search;
	for(int i=0;i<7;i++)
	{
		tempBall=rand() % 10 + 1;
		search= find(lotteryBall, lotteryBall+7,tempBall);
		if(search==lotteryBall+7)
		{
		//number is not found
		lotteryBall[i]=tempBall;
		cout<< lotteryBall[i]<<' ';
		}
		else{i--;}
	}
	cin.get();
	return 0;
}

about ctime I am not sure but I do not include it and it works as well(at least with g++ on linux).
Last edited by kaleshwar; Jun 21st, 2008 at 7:46 pm. Reason: code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Stopping same number from appearing twice?

 
0
  #14
Jun 21st, 2008
Originally Posted by salman213 View Post
GOOD POINT, never noticed that.

so how about this

  1. #include <iostream>
  2. using namespace std;
  3. bool check (unsigned int checkvariable[], int a);
  4. int main()
  5. {
  6. srand(time(NULL));
  7. unsigned int lotteryBall[9];
  8. for(int i = 0; i <=8; ++i)
  9. {
  10. do
  11. {
  12. lotteryBall[i] = rand() % 10 + 1;
  13. }
  14. while (check(lotteryBall, i));
  15.  
  16. if(i == 8)
  17. cout << "*" << lotteryBall[i] << "*";
  18. else
  19. cout << lotteryBall[i] << " ";
  20.  
  21. }
  22. cin.get();
  23. return 0;
  24. }
  25.  
  26. bool check (unsigned int checkvariable[], int a)
  27. {
  28. for(int b=0;b<=a;b++)
  29. {
  30. if (a==b)
  31. {
  32. return false;
  33. break;
  34. }
  35. if(checkvariable[b]==checkvariable[a])
  36. {
  37. return true;
  38. break;
  39. }
  40. }
  41. }



ONE QUESTION: I THOUGHT I READ THAT IF YOU WANTED TO USE THE SRAND() FUNCTION YOU MUST INCLUDE THE DIRECTIVE: ctime. In this case I did not and it still works??
Yeah, that program works well. srand is from cstdlib, not ctime. NULL is in ctime and cstdlib. I don't know why it works without cstdlib, but it does. Still, probably best to #include it anyway to be safe. If you don't #include iostream and add using namespace std , you seem to run into problems with this program regarding srand. Not sure why.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Stopping same number from appearing twice?

 
0
  #15
Jun 21st, 2008
Originally Posted by kaleshwar View Post
wouldn't something like this be better? I mean if c++ gives the find function why not use it?

#include <iostream>
using namespace::std;

int main()
{
	srand(time(NULL));
	unsigned int lotteryBall[7];
	unsigned int tempBall;
	unsigned int* search;
	for(int i=0;i<7;i++)
	{
		tempBall=rand() % 10 + 1;
		search= find(lotteryBall, lotteryBall+7,tempBall);
		if(search==lotteryBall+7)
		{
		//number is not found
		lotteryBall[i]=tempBall;
		cout<< lotteryBall[i]<<' ';
		}
		else{i--;}
	}
	cin.get();
	return 0;
}

about ctime I am not sure but I do not include it and it works as well(at least with g++ on linux).
This program gave good results too.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 929 | Replies: 14
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC