Thread: Trying to fill an int array

  1. #1
    Registered User boojus's Avatar
    Join Date
    Oct 2003
    Posts
    9

    Question 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;
    }
    Last edited by boojus; 11-21-2003 at 02:10 AM.

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    Re: Trying to fill an int array

    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!

  3. #3
    Registered User boojus's Avatar
    Join Date
    Oct 2003
    Posts
    9
    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.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. problem with sorting
    By pinkpenguin in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 11:06 AM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM