•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 375,207 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,314 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 3029 | Replies: 6
![]() |
Hi ladies and gentlemen,
Gotta question,
I have a program in wich I make a small array of 100 random numbers wich are smaller then 1000!
Like this:
# define MAX 100
void srand(int tabel[MAX]);
void srand(int tabel[MAX])
{
for (int i=0;i<MAX;i++)
do
tabel[i]=rand();
while (tabel[i]>1000);
}
This of course is only one part of the program and as you can see this array is made with a function! Now, this way I get random numbers but there are several numbers in that array wich are shown twice or even more times, how can I change this program so it doesn't use the same number twice?
Can anyone please help me out?
Thanks in advance!
Gotta question,
I have a program in wich I make a small array of 100 random numbers wich are smaller then 1000!
Like this:
# define MAX 100
void srand(int tabel[MAX]);
void srand(int tabel[MAX])
{
for (int i=0;i<MAX;i++)
do
tabel[i]=rand();
while (tabel[i]>1000);
}
This of course is only one part of the program and as you can see this array is made with a function! Now, this way I get random numbers but there are several numbers in that array wich are shown twice or even more times, how can I change this program so it doesn't use the same number twice?
Can anyone please help me out?
Thanks in advance!
One solution can be, before inserting the new no. in the array, scan the array (iterate through it), to see whether the no. is already in the array or not, if not, insert it and yes, loop again.
You can either iterate by simple loop or if you manage to create the array in sorted order, then you can use binary search (which is much faster) to serve the purpose.
If you can use STL, then you can use vector and find algorithm, which can make your life much easy.
You can either iterate by simple loop or if you manage to create the array in sorted order, then you can use binary search (which is much faster) to serve the purpose.
If you can use STL, then you can use vector and find algorithm, which can make your life much easy.
Regards,
Ejaz.
Ejaz.
The simple and not so great way of iterating through the array to see if it is a duplicate and discarding it may go something like this.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define SIZE 100
#define MAX 1000
int main(void)
{
int array[SIZE];
size_t i,j;
srand(time(NULL));
for ( i = 0; i < sizeof array / sizeof *array; ++i )
{
REDO: array[i] = rand() % MAX;
for ( j = 0; j < i; ++j )
{
if ( array[i] == array[j] )
{
goto REDO;
}
}
printf("%3d%c", array[i], i % 10 == 9 ? '\n' : ' ');
}
return 0;
}
/* my output
138 327 659 825 356 700 248 195 615 253
318 123 215 333 396 813 196 230 376 725
139 446 985 816 383 427 557 411 523 277
270 943 869 200 690 154 345 912 239 94
937 374 882 665 762 938 681 956 961 673
838 119 526 234 549 932 304 579 617 755
578 409 543 766 727 571 471 48 574 394
614 973 421 528 142 804 115 530 338 788
691 688 883 878 887 350 661 64 998 749
572 216 513 89 441 243 952 209 468 959
*/
Thanks very much Dave :p
This way I can see how you did it, and find a way to do it in my programm
Got a few questions tough, I don't want to just copy your solution without knowing why you did certain things and what the meaning of some of them is :!:
1) srand(time(NULL)); Ive read in a tutorial on this forum about it, does this make certain that the random numbers are allways different? Why are there two )'s at the end, think this was not intentional right
2) sizeof array / sizeof *array, can you give me a brief explanation why you divide these
3) REDO, I understand the English words and can see what it does, but can you tell me what it is
Meaning, could I use another word to get my variable into the iteration again
IT's probably and most certainly the goto that does it right, but just want to be certain
4) When you use array[ j ], this is just the next number in the array wich comes after [ i ] correct and could you use something like this:
if ( array[i] == array[i] -1) because this way you are referring to the previous number in the array aswell correct
Anyway, thanks again for the example, it's going to help alot, if you don't have time to explain these questions, I understand :!:
This way I can see how you did it, and find a way to do it in my programm
Got a few questions tough, I don't want to just copy your solution without knowing why you did certain things and what the meaning of some of them is :!:
1) srand(time(NULL)); Ive read in a tutorial on this forum about it, does this make certain that the random numbers are allways different? Why are there two )'s at the end, think this was not intentional right
2) sizeof array / sizeof *array, can you give me a brief explanation why you divide these
3) REDO, I understand the English words and can see what it does, but can you tell me what it is
Meaning, could I use another word to get my variable into the iteration again
IT's probably and most certainly the goto that does it right, but just want to be certain
4) When you use array[ j ], this is just the next number in the array wich comes after [ i ] correct and could you use something like this:
if ( array[i] == array[i] -1) because this way you are referring to the previous number in the array aswell correct
Anyway, thanks again for the example, it's going to help alot, if you don't have time to explain these questions, I understand :!:
•
•
•
•
Originally Posted by JoBe
I don't want to just copy your solution without knowing why you did certain things and what the meaning of some of them is :!:
•
•
•
•
Originally Posted by JoBe
1) srand(time(NULL)); Ive read in a tutorial on this forum about it, does this make certain that the random numbers are allways different? Why are there two )'s at the end, think this was not intentional right![]()
srand(time(NULL));
•
•
•
•
Originally Posted by JoBe
2) sizeof array / sizeof *array, can you give me a brief explanation why you divide these![]()
•
•
•
•
Originally Posted by JoBe
3) REDO, I understand the English words and can see what it does, but can you tell me what it isMeaning, could I use another word to get my variable into the iteration again
IT's probably and most certainly the goto that does it right, but just want to be certain
![]()
REDO: array[ i ] =rand() % MAX; for ( j = 0; j < i; ++j ) { if ( array[ i ] == array[ j ] ) { goto REDO; }
•
•
•
•
Originally Posted by JoBe
4) When you use array[ j ], this is just the next number in the array wich comes after [ i ] correct and could you use something like this:
if ( array[ i ] == array[ i ] -1) because this way you are referring to the previous number in the array aswell correct![]()
for ( i = 0; i < sizeof array / sizeof *array; ++i )
{
REDO: array[ i ] = rand() % MAX;
for ( j = 0; j < i; ++j )
{![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
- number of times each number in array occurs? (C)
- Errors in counting number of elements of the array greater and lesser than average (C)
- to secomd smallest number in array (Java)
- print a number in ordered form (C++)
- twenty integers in an array (Java)
- Building an array for dice program (C#)
- sorting an array of string (C)
- How do I create a program using an Array ? (C++)
Other Threads in the C Forum
- Previous Thread: PThreads: question on basic problem (or POSIX Threads)
- Next Thread: How can I write a program with 3 classes?



Linear Mode