Hi i'm relatively new to C++ and I've been asked to randomly fill an array with 100 positive integers with values between 0 and 99. Could someone please help me?
this is what i have so far and i know it's terribly wrong.
#include <iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
const int aSize=10;
int main()
{
a[i]=rand();
srand(time(0));
int a[aSize];
for (int i=0;i<aSize;i++)
cout<<endl;
#include <iostream>
#include<ctime>
#include<cstdlib>
usingnamespace std;
constint aSize=10; //I thought you wanted 100 integers not 10?
int main()
{
a[i]=rand(); //where does 'a' come from?
srand(time(0));
int a[aSize];
for (int i=0;i<aSize;i++)
cout<<endl; //why are you looping this statement?
cout<<a[i]; //where does 'i' come from?
return 0;
}
How I would do it: main function
seed the random generator
define a constant integral type for size of array with value 100
make array with size declared above
loop through array
for current array index set random value modulus 100
...
- "aSize" and "array" should be inside of main.
- you need to srand at the beginning of main.
- %99 doesn't quite fulfill the whole range.
After that I think it *should* be fine. Does it actually work?
If I were you, I would ask myself these questions:
1). Why do you need ctime?
2). Is there a way of presenting array[aSize] without writing aSize?
3). What range of variables does %99 (mod 99) give? Does adding 0 to this do anything?
4).What do the logical operators do? Are they needed here?