1. ## Duplicate Elimination Program

Use a single-subscripted array to solve the following problem.
Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print
it only if it’s not a duplicate of a number already read. Provide for the “worst case” in which all 20
numbers are different. Use the smallest possible array to solve this problem.
The code I wrote so far:

Code:
```#include <stdio.h>#include <conio.h>

int main() {
int num[20];
printf("Enter 20 numbers:\n");

for (int i=0; i<20; i++) {
scanf_s("%d", num[i]);
if (num[i]>10 && num[i]<100) {
for (int j=0; j<=i; j++) {
if (num[i]==num[j]) {
printf("Duplicate\n");
}
}
}
}

_getch();
return 0;
}```
The code breaks after I input the first value. What's wrong.

2. Lots. conio.h. _getch();. scanf_s() instead of scanf(), not checking its return value, and specifying an integer rather than a pointer to a integer to store the result to. You check the input number, but do nothing if it is wrong. You compare the just-read integer to itself to find it a duplicate of itself. Although you were asked to print each number only on the first time it is seen, you instead print "Duplicate" for each duplicate.

3. Another thing:

Read in 20 numbers, each of which is between 10 and 100, inclusive.
Code:
`if (num[i]>10 && num[i]<100)`
That's not quite right.

4. I use _getch() and scanf_s() because my compiler updated and won't let me use the other version of these.

Code:
```#include <stdio.h>
#include <conio.h>

int main() {
int num[20]; bool found;
printf("Enter 20 numbers:\n");

for (int i=0; i<20; i++) {
found=false;
scanf_s("%d", num[i]);
if (num[i]>=10 && num[i]=<100 && (num[i]<'0' || num[i]>'9')) {
for (int j=0; j<i; j++) {
if (i!=j && num[i]==num[j]) {
found=true;
break;
}
if (found==false) {
printf("%d ", num[i]);
}
}
}
}

_getch();
return 0;
}```
Matticus corrected, thanks.

I'm not sure whether checking the type of input is right or not.

5. Originally Posted by lmanukyan
I use _getch() and scanf_s() because my compiler updated and won't let me use the other version of these.

Matticus corrected, thanks.

I'm not sure whether checking the type of input is right or not.
If there was a time to check the input, it was when scanf_s() was returning. (Are you required to use scanf_s()? If it isn't a school rule, you ought to #define _CRT_SECURE_NO_DEPRECATE) You could, for example, do this for each num:
Code:
```for (;;) {
int ch, result = scanf_s ("%d%c", &num[i], &trail);
if (result != 2 || trail != '\n') {
printf ("Please enter a valid integer.\n");
while ((ch = getchar()) != '\n' && ch != EOF) ;
}
else break;
}```
Code:
`(num[i]<'0' || num[i]>'9')`
As num[i] is an integer, not a character, checks like this are misleading.

The idea would be to process the input in stages. After the above scanf_s() code succeeds, then you can do this:
Code:
` num[i] >= 10 && num[i] <= 100`
and it would be sufficient.