Random number generation from an array in C programming

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 12
Reputation: kishore84 is an unknown quantity at this point 
Solved Threads: 0
kishore84 kishore84 is offline Offline
Newbie Poster

Random number generation from an array in C programming

 
0
  #1
Feb 2nd, 2009
Hello friends,

Need help in generating a random number from an array.Once a number is generated, the number has to be deleted from the array so that only the remaining numbers can be generated from the array.And it should go on until all the numbers from the array are deleted.

Hope replies asap from you friends.
Last edited by Ancient Dragon; Feb 2nd, 2009 at 10:15 pm. Reason: remove bold tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Random number generation from an array in C programming

 
0
  #2
Feb 2nd, 2009
Which part are you stuck on?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: kishore84 is an unknown quantity at this point 
Solved Threads: 0
kishore84 kishore84 is offline Offline
Newbie Poster

Re: Random number generation from an array in C programming

 
0
  #3
Feb 2nd, 2009
I am able to generate a random number from an array but was unable to delete the number that have already appeared once.....I was stuck at this point
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Random number generation from an array in C programming

 
0
  #4
Feb 2nd, 2009
>but was unable to delete the number that have already appeared once.

http://www.daniweb.com/forums/thread38345.html
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,976
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 288
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Random number generation from an array in C programming

 
0
  #5
Feb 2nd, 2009
Show us what code you already have. It might enlighten us on the problem you are encountering.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: kishore84 is an unknown quantity at this point 
Solved Threads: 0
kishore84 kishore84 is offline Offline
Newbie Poster

Re: Random number generation from an array in C programming

 
0
  #6
Feb 2nd, 2009
the problem is the erase command may not work in C.Need help in C coding
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: kishore84 is an unknown quantity at this point 
Solved Threads: 0
kishore84 kishore84 is offline Offline
Newbie Poster

Re: Random number generation from an array in C programming

 
0
  #7
Feb 2nd, 2009
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. int pick()
  7. {
  8. int arr[5]={2,3,4,5,6};
  9. int random;
  10. srand(time(NULL));
  11. random = arr[rand() % 5 ];
  12. return random;
  13. }
  14.  
  15. int main()
  16. {
  17. pick();
  18. int i = 0;
  19. while(i<5)
  20. {
  21. printf("random number from the array %d\n",pick());
  22. }
  23. getch();
  24. }
This is code i have written and stuck up on how to delete the number that have already appeared once
Last edited by Ancient Dragon; Feb 2nd, 2009 at 10:02 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: kishore84 is an unknown quantity at this point 
Solved Threads: 0
kishore84 kishore84 is offline Offline
Newbie Poster

Re: Random number generation from an array in C programming

 
0
  #8
Feb 2nd, 2009
sorry its the for loop in the place of while loop

for(i=0;i<5;i++) in the place of while(i<5)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
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: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Random number generation from an array in C programming

 
0
  #9
Feb 2nd, 2009
Originally Posted by kishore84 View Post
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int pick()
{
int arr[5]={2,3,4,5,6};
int random;
srand(time(NULL));
random = arr[rand() % 5 ];
return random;
}

int main()
{
pick();
int i = 0;
while(i<5)
{
printf("random number from the array %d\n",pick());
}
getch();
}

This is code i have written and stuck up on how to delete the number that have already appeared once
Not 100% sure what you are trying to do and what you mean by "delete", but this code snippet might be useful:

http://www.daniweb.com/code/snippet1034.html

Also, what "erase" command are you referring to in your earlier post. Here's an "erase" command from the vector library.

http://www.cplusplus.com/reference/s...tor/erase.html

You would have to have a vector rather than an array to use it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: kishore84 is an unknown quantity at this point 
Solved Threads: 0
kishore84 kishore84 is offline Offline
Newbie Poster

Re: Random number generation from an array in C programming

 
0
  #10
Feb 2nd, 2009
Vernondozier thanks for ur valuable info in the first link.Its informative.But the same functinality cannot be implemented in C as we are using namespace in here.Please help me out.

Hope reply from you asap.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC