i am writing a program that is delcaring an array of 100 intger numbers
and filling the array with random numbers us rand(). all i have so far is.


#include <iostream>

using namespace std;

int main()
{
int random_integer = rand();

Dani AI

Generated

: declare the 100-element array inside main, right after the opening {, as suggested. The literal 100 is the compile-time size here; it becomes the array bound. A minimal example that seeds the PRNG and fills a 100-element int array is shown below.

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    std::srand(static_cast<unsigned>(std::time(nullptr)));
    int numbers[100];

    for (int i = 0; i < 100; ++i)
        numbers[i] = std::rand();

    std::cout << numbers[0] << '\n';
    return 0;
}

Notes and cautions: include <cstdlib>/<ctime> when using srand/rand, and call srand once before filling the array. Standard C++ requires a compile-time constant for a fixed array bound; for safer, more flexible code prefer std::array<int, 100> or std::vector<int>(100). For better randomness and range control, prefer C++11 <random> (for example std::mt19937 + std::uniform_int_distribution) rather than rand() or rand() % n (which can introduce bias). Always ensure loop indices stay within 0..99 to avoid out-of-bounds writes.

Recommended Answers

All 3 Replies

Good start.

What's your question?

where do i declare 100 in the program

You would declare the array in main( ) (assuming this is just a simple program). It should follow the opening { of main.

Then declare the variable to count through the loop that you will use to fill the array, then start the loop, using that counter as the array index.

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.