array help

Hi im new to c++, as in like 4 weeks new, and need to create a program that will run a 1-D array to read in a list of numbers (1-100) and print them, if it is not a duplicate of an already read number, using the smallest possible array.
If anyone could help that would be great, i have written a small section of code to create the array but am completely unsure of if it's correct or how to continue.




#include <iostream>

using namespace std;

int main()
{
const int SIZE=100;
int array[SIZE],dupes;

cout << "Input " << SIZE << "integers : ";
for (int i=0; i<SIZE; i++)
cin >> array[i];
}
You will need two variables as you add to the array - one to track how many numbers are actually in the array, and another to track how many you have asked the user for. Each time you ask the user for a number, you will have to go through and check the previous entries to make sure it is not already there. If it isn't, put it in the array and add to the count for how many numbers are in the array.
okay thanks, sorry but how would i go about doing that?
you won't even need a separate array..

first, i suggest making a function that returns either if a number is inside an array
( something like )
1
2
3
bool is_in_array( const int* arr, int value, size_t size ) { // or is_unique ?
    // ...
}


then after cin >> array[ i ];, call the function inside w/ if to check if the value entered doesn't exist in the array, (!) if so, then add it to the array, if not then simply do nothing
( you will need a separate index to specify where the next unique number entered will be put in the array)

after all the values has been entered, just print the elements of the array normally (up to the value of array index)
Last edited on
First, start by actually making the two variables I am talking about. You already have one of them, i in the for loop. I suggest you rename dupes to something more descriptive like count.

Also, I forgot to mention before, you should use code tags:
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Will the if statement work because i don't know the values, like i'm entering data and then it will print only if its not a duplicate? So i dont want to be typing every number to check if its a dupe? Its an unknown value being tested against an array to see if its already present?
I don't know what you're asking.
Topic archived. No new replies allowed.