# Trying to fill an int array

• 11-21-2003
boojus
Trying to fill an int array
I'm trying to fill an integer array with descending values starting at a max of whatever the user enters. so if they enter 5, the array should be a[0]=1, a[1]=2, a[2]=3, a[3]=4, and a[4]=5. the rest of the postions, i forget what they are called, are unimportant, so i guess a[i] has to equal a null? right now its not working. i try to display the array but it doesnt give me the values i was hoping for. anyway here is what i have:
Code:

```#include <iostream> #include <algorithm> using namespace std; int main() {         char input[4];         int a[101], numInput, i=0;         cout << "Enter a number ranging from 9-100: ";         cin.getline(input, 4);         numInput = atoi(input);         while(numInput < 9 || numInput >100)         {                         cout << "Enter a number ranging from 9-100: ";                         cin.getline(input,4);                         numInput = atoi(input);         } /**********THIS PART DOESN'T WORK****************/         i = numInput;         while(i>=1)         {                 a[i-1]=i;                 i--;         }         cout << a <<endl; /**********************************************/ //while( next_permutation( a.begin() , a.end() ) ) {   //cout << input << endl; } return 0; }```
• 11-21-2003
Plastyksouljah
Re: Trying to fill an int array
Quote:

Originally posted by boojus
I'm trying to fill an integer array with descending values starting at a max of whatever the user enters. so if they enter 5, the array should be a[0]=1, a[1]=2, a[2]=3, a[3]=4, and a[4]=5. the rest of the postions, i forget what they are called, are unimportant, so i guess a[i] has to equal a null? right now its not working. i try to display the array but it doesnt give me the values i was hoping for. anyway here is what i have:
Code:

```#include <iostream> #include <algorithm> using namespace std; int main() {         char input[4];         int a[101], numInput, i=0;         cout << "Enter a number ranging from 9-100: ";         cin.getline(input, 4);         numInput = atoi(input);         while(numInput < 9 || numInput >100)         {                         cout << "Enter a number ranging from 9-100: ";                         cin.getline(input,4);                         numInput = atoi(input);         } /**********THIS PART DOESN'T WORK****************/         i = numInput;         while(i>=1)         {                 a[i-1]=i;                 i--;         }         cout << a[i-1]  <<endl; /**********************************************/ //while( next_permutation( a.begin() , a.end() ) ) {   //cout << input << endl; } return 0; }```

That should work, at least it makes sense in my head!:D
• 11-21-2003
boojus
hmm.
maybe it does work. i guess the array was quite big so when i tried to cout<<a; it was to big maybe and it gave me some weird value. well it does work how i need it to.
• 11-21-2003
grib
cout << a << endl; will output the address of the array a, this is likely not what you wanted. To output an array you can use std::copy like so
std::copy(vints.begin(),vints.end(),std::ostream_i terator<int>(std::cout," "));

where vints is a vector<int>, to do the same thing for an array we note that a pointer to an array is a random access iterator.

std::copy(a,&a[sizeof(a)/sizeof(a[0])],std::ostream_iterator<int>(std::cout," "));

Here we have a pointer to the start of the array and a pointer one past the end. Note that this will NOT work for a dynamically allocated array. You need to keep track of the size yourself and ether pass a pointer to a+size or use copy_n and pass the size as a paramiter.

I usually like to keep something like this around
Code:

```template<class InputIterator> bool write_csv(InputIterator ii, InputIterator end, std::ostream &os=std::cout) {     os << '{';     if(ii != end) {         os << *ii;         while(++ii != end) os << ", " << *ii;     }     os << '}';     return os.good(); } template<class Container> bool write_csv(const Container &c, std::ostream &os=std::cout) {     return write_csv(c.begin(),c.end(),os); }```
This provides nicely formated comma seperated values for any container, container-like structure, or anything I can get a pair of iterators out of. Finally if you intend to go through every permutation of a 100 element array I Really want to know where you buy your computers.