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.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
If you want an undetermined amount of employees, you won't be able to use an array. Use a vector, instead.
packetpirate
Junior Poster in Training
60 posts since Jun 2010
Reputation Points: 10
Solved Threads: 3
psuedo-code:
create randomAges[N] = {0}:
seedRng();
for (i = 0 : N){
randomAges[i] = rand();
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
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...
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
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 newstandards-compliant compiler.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
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.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
>> 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.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
#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.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393