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 16 17 18 19
#include <iostream>
#include <conio.h>
usingnamespace std;
int main()
{
constint SIZE=50;
int array[SIZE];
cout << "Enter a list of numbers: ";
for (int i=0; i<SIZE; i++)
cin >> array[i];
getch();
return 0;
}
You have the basic idea to put the numbers in an array, but there is a problem.
You may end up with fewer numbers in the array than the user enters, therefore using i as a subscript to store into the array won't give you the results you want. Also, you're not checking for duplicates.
Try this instead:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{ constint SIZE=50;
int array[SIZE];
int cnt = 0; // Number of entries in the array
int temp;
cout << "Enter a list of numbers: ";
for (int i=0; i<SIZE; i++)
{ cin >> temp;
if (is_not_duplicate (array, cnt, temp))
array[cnt++] = temp; // Not a duplicate, add to the array
}
getch();
return 0;
}
I leave the implementation of is_not_duplicate to you.