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;
}
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