hi, i need to create a 1D array of numbers and print them as they are entered, however the number should only be printed if it is not a duplicate already in the array - the numbers must be between 1 and 100 inclusive.
i'm new to c++ and have little idea of how to do this, if anyone could help that'd be great. this is what i have already?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main()
{
constint SIZE=100;
int array[SIZE];
cout << "Enter a list of numbers:
for (int i=0; i<SIZE; i++)
cin >> array[i];
}
make a find function or use the one from <algorithm>
after cin >> array[ i ], check if it's between 1 && 100, then do a search in the array using the function you made, do whatever is appropriate for the return value of that
bool isInArray( int arr[], unsigned size, int value ) // i think it is more appropiate name than find
{
for( unsigned i = 0; i < size; ++i ) {
if( /* ____ */ )
returntrue;
}
returnfalse;
}
then in your for loop, it should look something like :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
constint SIZE=100;
int array[SIZE];
int array_index = 0; // to specify where the next unique number will be stored
for( int i = 0; i < SIZE; i++ ) {
cin >> array[ i ];
if( /* input is between 1 && 100 */ ) {
if( /*number is in array (use the function isInArray) */ ) {
// do nothing
} else {
// store array[i] in array[ array_index ]
// increment array_index
}
}
}