943,015 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 210
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 2nd, 2010
0

How we can randomly generate and store the age of N employees

Expand Post »
How we can randomly generate and store the age of N employees in Arrays plssssss
Last edited by alex k; Sep 2nd, 2010 at 7:45 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alex k is offline Offline
6 posts
since Aug 2010
Sep 2nd, 2010
0
Re: How we can randomly generate and store the age of N employees
You can use while loop and terminate it with -1, if the number of employees is not known, else you can just use a neat for loop. And for random number generation have a look at this link:
http://www.cplusplus.com/reference/c.../cstdlib/rand/

If you have any code written, then post it, it will be easier to help
Reputation Points: 16
Solved Threads: 18
Junior Poster
Software guy is offline Offline
151 posts
since May 2008
Sep 2nd, 2010
-1
Re: How we can randomly generate and store the age of N employees
Is N a pre-determined value, or is it an unknown value that requires a sentinel value to terminate the input loop?

If it's a pre-determined value, write a for loop that runs from 0 to (N-1) then use the RNG to create the ages. If it's an unknown, a while loop would be better.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Sep 2nd, 2010
0
Re: How we can randomly generate and store the age of N employees
If you want an undetermined amount of employees, you won't be able to use an array. Use a vector, instead.
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
packetpirate is offline Offline
60 posts
since Jun 2010
Sep 2nd, 2010
0
Re: How we can randomly generate and store the age of N employees
psuedo-code:
C++ Syntax (Toggle Plain Text)
  1. create randomAges[N] = {0}:
  2. seedRng();
  3. for (i = 0 : N){
  4. randomAges[i] = rand();
  5. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,859 posts
since Dec 2008
Sep 3rd, 2010
-3
Re: How we can randomly generate and store the age of N employees
well, alex use rand() and randamize() function. ( as given in one of the previous post).
use arrays only if you are a beginner.
otherwise use vectors or pointers.
For pointers, I am giving a helpful code snippet
  1. randimize();
  2. .
  3. .
  4. .
  5.  
  6. for ( i = 0; i != NULL; i++ ) // use stdlib.h or stdio.h ( may be cstdlib or cstdio as the case may be)
  7. *( age + i ) = rand();
Here, the advantage is that the poor compilers will have to take care of all the memory management , you are left with other issues.
Also, this progam will run, most of the times without any modification on your part if variable number is to be handled.
use vector, only if developing a commercial project as it support STL and will give you ample options to modify/ improve the code if client requires to do so.
Hope, this will help. If you need more assistance then reply in the post.
*Manoj
Reputation Points: 6
Solved Threads: 8
Light Poster
manojwali is offline Offline
40 posts
since Sep 2010
Sep 3rd, 2010
0
Re: How we can randomly generate and store the age of N employees
Quote ...
C++ Syntax (Toggle Plain Text)
  1. for ( i = 0; i != NULL; i++ ) // use stdlib.h or stdio.h ( may be cstdlib or cstdio as the case may be)
  2. *( age + i ) = rand();
This will never run. In a proper C++ implementation NULL is defined as int 0. Your initial looping condition will be false. As a result, the loop gets completely bypassed and never does anything.

If you're going to post something for someone to C/P, at least know what you're posting...
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Sep 3rd, 2010
-1
Re: How we can randomly generate and store the age of N employees
Click to Expand / Collapse  Quote originally posted by Fbody ...
This will never run. In a proper C++ implementation NULL is defined as int 0. Your initial looping condition will be false. As a result, the loop gets completely bypassed and never does anything.

If you're going to post something for someone to C/P, at least know what you're posting...
For your reference, I may said that In Turbo C/C++ 3.1 version, NULL is defined as -1, and that compiler and many other compiler are not standardize ( which I think you mean buy proper C++ implementation and by the way that compiler was and is running till now and at that time the final draft for standardizing of C++ was not complete. )
-Manoj
Reputation Points: 6
Solved Threads: 8
Light Poster
manojwali is offline Offline
40 posts
since Sep 2010
Sep 3rd, 2010
0
Re: How we can randomly generate and store the age of N employees
Click to Expand / Collapse  Quote originally posted by manojwali ...
For your reference, I may said that In Turbo C/C++ 3.1 version, NULL is defined as -1, and that compiler and many other compiler are not standardize ( which I think you mean buy proper C++ implementation and by the way that compiler was and is running till now and at that time the final draft for standardizing of C++ was not complete. )
-Manoj
Thus the reason Turbo C++ is generally considered old and decrepit. Find a new standards-compliant compiler.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Sep 3rd, 2010
-1
Re: How we can randomly generate and store the age of N employees
Click to Expand / Collapse  Quote originally posted by Fbody ...
This will never run. In a proper C++ implementation NULL is defined as int 0. Your initial looping condition will be false. As a result, the loop gets completely bypassed and never does anything.

If you're going to post something for someone to C/P, at least know what you're posting...
Well, FBody tell me among Turbo C++, Microsoft Visual C++ 6, Borland C++ Compilers, how many are standarized.
Yes, Dev CPP is a lot standarized then all of them.
This type of code works for me.
-Manoj
Reputation Points: 6
Solved Threads: 8
Light Poster
manojwali is offline Offline
40 posts
since Sep 2010

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: From binary to Caesar cipher decoding
Next Thread in C++ Forum Timeline: declaration syntax error?





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


Follow us on Twitter


© 2011 DaniWeb® LLC