learmingslowly
learmingslowly

Reputation: 13

How do I print the following array to show number 1 - 1000000

I want to initialize an array and fill it from 1-1000000. How do I then print the array?

#include<iostream>


using namespace std;


const int holder = 1000000;
int main()
{
    int i = 0;
    int nums[holder] = {0};

    for( int i = 0; i < holder; i++)
    {
        nums[i] = i+1;
    }
return 0;
}

Upvotes: 0

Views: 194

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409356

How about something like this:

// First create a vector containing holder elements
std::vector<int> nums(holder);

// Then set each element to the number from 1 to holder, inclusive
std::iota(begin(nums), end(nums), 1);

Then to print it:

// Print each number in the vector, separated by newlines
for (auto num : nums)
{
    std::cout << num << '\n';
}

Many parts of this answer should really be part of any decent beginners book. The only "new" thing would be the std::iota call.

Upvotes: 1

Related Questions