| | |
Stopping same number from appearing twice?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
would this be better or worse???
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int check (unsigned int checkvariable[]); int main() { srand(time(NULL)); unsigned int lotteryBall[9]; for(int i = 0; i <=7; ++i) { lotteryBall[i] = rand() % 48 + 1; cout << lotteryBall[i] << " "; if(i == 7) { do { lotteryBall[i+1] = rand() % 48 + 1; }while (check(lotteryBall)); cout << "*" << lotteryBall[i+1] << "*"; } } cin.get(); return 0; } int check ( unsigned int checkvariable[]) { for(int a=0;a<=7;++a) { if (checkvariable[a]==checkvariable[8]) {return true; break;} else if (a==7) return false; } }
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)
#include <iostream> using namespace std; int check (unsigned int checkvariable[]); int main() { srand(time(NULL)); unsigned int lotteryBall[9]; for(int i = 0; i <=7; ++i) { lotteryBall[i] = rand() % 10 + 1; cout << lotteryBall[i] << " "; if(i == 7) { do { lotteryBall[i+1] = rand() % 10 + 1; } while (check(lotteryBall)); cout << "*" << lotteryBall[i+1] << "*"; } } cin.get(); return 0; } int check ( unsigned int checkvariable[]) { for(int a=0;a<=7;++a) { if (checkvariable[a]==checkvariable[8]) { return true; break; } else if (a==7) return false; } }
•
•
Join Date: Jun 2008
Posts: 32
Reputation:
Solved Threads: 0
GOOD POINT, never noticed that.
so how about this
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??
so how about this
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; bool check (unsigned int checkvariable[], int a); int main() { srand(time(NULL)); unsigned int lotteryBall[9]; for(int i = 0; i <=8; ++i) { do { lotteryBall[i] = rand() % 10 + 1; } while (check(lotteryBall, i)); if(i == 8) cout << "*" << lotteryBall[i] << "*"; else cout << lotteryBall[i] << " "; } cin.get(); return 0; } bool check (unsigned int checkvariable[], int a) { for(int b=0;b<=a;b++) { if (a==b) { return false; break; } if(checkvariable[b]==checkvariable[a]) { return true; break; } } }
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.
•
•
Join Date: Jun 2008
Posts: 8
Reputation:
Solved Threads: 0
wouldn't something like this be better? I mean if c++ gives the find function why not use it?
about ctime I am not sure but I do not include it and it works as well(at least with g++ on linux).
#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
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
GOOD POINT, never noticed that.
so how about this
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; bool check (unsigned int checkvariable[], int a); int main() { srand(time(NULL)); unsigned int lotteryBall[9]; for(int i = 0; i <=8; ++i) { do { lotteryBall[i] = rand() % 10 + 1; } while (check(lotteryBall, i)); if(i == 8) cout << "*" << lotteryBall[i] << "*"; else cout << lotteryBall[i] << " "; } cin.get(); return 0; } bool check (unsigned int checkvariable[], int a) { for(int b=0;b<=a;b++) { if (a==b) { return false; break; } if(checkvariable[b]==checkvariable[a]) { return true; break; } } }
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??
using namespace std , you seem to run into problems with this program regarding srand. Not sure why. •
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
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).
![]() |
Other Threads in the C++ Forum
- Previous Thread: Use of getc()/putc() for reading/writing chars, floats, etc. from binary files?
- Next Thread: how to make system() work in DOS Box
Views: 929 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





???
