943,713 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1140
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

Click to Expand / Collapse  Quote originally posted by salman213 ...
would this be better or worse ???

C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

GOOD POINT, never noticed that.

so how about this

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
salman213 is offline Offline
32 posts
since Jun 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kaleshwar is offline Offline
8 posts
since Jun 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

Click to Expand / Collapse  Quote originally posted by salman213 ...
GOOD POINT, never noticed that.

so how about this

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

Click to Expand / Collapse  Quote originally posted by kaleshwar ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Use of getc()/putc() for reading/writing chars, floats, etc. from binary files?
Next Thread in C++ Forum Timeline: how to make system() work in DOS Box





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC