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

Recommended Answers

All 16 Replies

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/clibrary/cstdlib/rand/

If you have any code written, then post it, it will be easier to help

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.

If you want an undetermined amount of employees, you won't be able to use an array. Use a vector, instead.

psuedo-code:

create randomAges[N] = {0}:
seedRng();
for (i = 0 : N){
  randomAges[i] = rand();
}

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

randimize();
          .
          .
          .

          for ( i = 0; i != NULL; i++ )  // use stdlib.h or stdio.h ( may be cstdlib or cstdio as the case may be)
        *( 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

for ( i = 0; i != NULL; i++ )  // use stdlib.h or stdio.h ( may be cstdlib or cstdio as the case may be)
        *( 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...

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

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.

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

Thus the reason Turbo C++ is generally considered old and decrepit. Find a new standards-compliant compiler.

Thanks for your advice. I am also using Visual Studio 2010 also but only of Visual C++ ( programming of windows only ).

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

In that case, you got a *infinite loop, either way your code is broken.

>> I am also using Visual Studio 2010 also but only of Visual C++
I use primarily VS2008. Test what you posted on your VS2010, it won't work.

Quote to Fbody and firstlook
Firstly, you both tell me what is the result using vs2008 and/or Vs 2010. ( I know it beforehand )
Also, mention your project option using General, C++ windows, Console.
Also Tell me about your final output whether final output is CRL or .exe based.
If possible, tell me about your current version Of Net-Framework installed on your computer.

Then, tell me is CRL ( Common Run-time Language ) standardized in any sense for C++ standard Implementation Or is it just commercial
trick adopted in Microsoft to make Visual Studio more portable for Windows Environment and also a little bit of Java- Competent. For how long are
you, Mr FBody using VS2008 and posting non-standard C++ Implementation. Have anybody stopped you from posting in this forum. Just search for any standard
C++ Compiler and let it get checked by any professional, who knows better than you.

Yes, this is a non-standard and non-professional way to do that thing, but it is short and compact. My Post have three ways of doing the same thing.

My post has three ways of doing that thing. You can use vector and STL, that code is also compact and standard as well.

I am here to help anyone that has a problem, not to waste my precious time.

Also, please, let me known about any latest - fully standardize C++/C Compiler you find.

-Manoj

Quote to Fbody and firstlook
Firstly, you both tell me what is the result using vs2008 and/or Vs 2010. ( I know it beforehand )
Also, mention your project option using General, C++ windows, Console.
Also Tell me about your final output whether final output is CRL or .exe based.
If possible, tell me about your current version Of Net-Framework installed on your computer.

Then, tell me is CRL ( Common Run-time Language ) standardized in any sense for C++ standard Implementation Or is it just commercial
trick adopted in Microsoft to make Visual Studio more portable for Windows Environment and also a little bit of Java- Competent. For how long are
you, Mr FBody using VS2008 and posting non-standard C++ Implementation. Have anybody stopped you from posting in this forum. Just search for any standard
C++ Compiler and let it get checked by any professional, who knows better than you.

Yes, this is a non-standard and non-professional way to do that thing, but it is short and compact. My Post have three ways of doing the same thing.

My post has three ways of doing that thing. You can use vector and STL, that code is also compact and standard as well.

I am here to help anyone that has a problem, not to waste my precious time.

Also, please, let me known about any latest - fully standardize C++/C Compiler you find.

-Manoj

commented: Drop it already. For someone that doesn't like arrogant people, you're sure arrogant. +0
#include <iostream>
using namespace std;

int main() {
  const int ARY_SIZE = 10;
  int anArray[ARY_SIZE] = {0};

  cout << "Before for loop 1:\n";
  for (int i = 0; i != NULL; ++i) {
    cout << "Populating anArray[" << i << "]" << endl;
    *(anArray + i) = (i * 2);
  }

  cout << "Starting for loop 2:\n";
  for (int i = 0; i != NULL; ++i) {
    cout << "Element anArray[" << i << "] = " << anArray[i] << endl;
  }
  cout << "After for loop 2." << endl;

  cin.get();
  return 0;
}

Compile it and run it. Neither for loop ever runs (in both VS2008 and in Code::Blocks). Now drop it.

commented: just using vs 2008 which has a CRL an non standard extension, so get irretated as give me negative reputation points. +0

[Query]Sorry for earlier duplicate posts to DaniWeb Comminity.[/Query]
well, alex I am back here with a new approach to handle your problem. I was just distracted for nothing.
use srand() functions with time as seed argument instead of randimize() as former is standard and easly avaiable along any C++ Compiler.
example srand(NULL) or srand ( Current time);
I am giving three ways/ approaches to this particular problem.
1. use arrays only if you are a beginner. In that case, make sure to allocate large enough array at coding time. Also, you can recompile
code if need is as such in the future. ( easiest approach, least flexible program is made)
2. use pointers only if you are famlier with pointers ( Arrays are just pointers in memory and to compiler where actual overheads are done
by compiler itself. ) ( somewhat difficult, but gives more comtrol over the things )
3. use vectors an professional approach as it gives infinite ways to experiment. ( use it only if you know fully the working of STL and its use in C++, and of course, vector class and its method )
This is very vast topic and covers a lot of C++ standrads Implementation.

You had not made clear from where you need to get your input ( yes, it must be for some employee or for that matter for some student or any other person )
that logic will give you the condition. Please, make sure to specify your problem more clearly, next time you want any help from DaniWeb or for that matter from me,
if you find my this posting of any value.

Hope, this will help you to get starting in right direction. Please, say thanks to this post if you find it any useful to you.
-Manoj

commented: CLR is irrelevant to the situation. Besides, Code::Blocks doesn't use CLR, that's why I mentioned it. Get your facts straight. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.