In: Computer Science
(C programming) Use a one-dimensional 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.
Your solution must include a function called isUnique() that returns 1 (true) if the number input is unique and 0 (false) otherwise . The implementation of this function must follow AFTER the implementation of main () in your .c file.
Output should be look like:
TEST SET 1
1 unique number
===============
Enter # 1 : 9
The number enetered is not in the valid range of 10 to 100
Enter # 1 : 101
The number enetered is not in the valid range of 10 to 100
Enter # 1 : 10
The number: 10 is unique
Enter # 2 : 10
Enter # 3 : 10
Enter # 4 : 10
Enter # 5 : 10
Enter # 6 : 10
Enter # 7 : 10
Enter # 8 : 10
Enter # 9 : 10
Enter # 10 : 10
Enter # 11 : 10
Enter # 12 : 10
Enter # 13 : 10
Enter # 14 : 10
Enter # 15 : 10
Enter # 16 : 10
Enter # 17 : 10
Enter # 18 : 10
Enter # 19 : 10
Enter # 20 : 10
All of the unique numbers found are:
10
TEST SET 2
20 unique numbers
=================
Enter # 1 : 10
The number: 10 is unique
Enter # 2 : 20
The number: 20 is unique
Enter # 3 : 30
The number: 30 is unique
Enter # 4 : 9
The number enetered is not in the valid range of 10 to 100
Enter # 4 : 101
The number enetered is not in the valid range of 10 to 100
Enter # 4 : 40
The number: 40 is unique
Enter # 5 : 50
The number: 50 is unique
Enter # 6 : 60
The number: 60 is unique
Enter # 7 : 70
The number: 70 is unique
Enter # 8 : 80
The number: 80 is unique
Enter # 9 : 90
The number: 90 is unique
Enter # 10 : 100
The number: 100 is unique
Enter # 11 : 95
The number: 95 is unique
Enter # 12 : 85
The number: 85 is unique
Enter # 13 : 75
The number: 75 is unique
Enter # 14 : 65
The number: 65 is unique
Enter # 15 : 55
The number: 55 is unique
Enter # 16 : 45
The number: 45 is unique
Enter # 17 : 35
The number: 35 is unique
Enter # 18 : 25
The number: 25 is unique
Enter # 19 : 15
The number: 15 is unique
Enter # 20 : 11
The number: 11 is unique
All of the unique numbers found are:
10 20 30 40 50
60 70 80 90 100
95 85 75 65 55
45 35 25 15 11
Code
#include<stdio.h>
int isUnique(int a[],int b,int n);
int main()
{
int n;
int a[20]; //create array for unique elements
int p=0;
for(int i=1;i<=20;i++)
{
while(1)
{
printf("Enter # %d :",i);
scanf("%d",&n);
if(n<10 || n>100)
{
printf("The number enetered is not in the valid range of 10 to 100\n");
}
else
break;
}
if(isUnique(a,p,n)) //calling function
{
printf("The number: %d is unique\n",n);
a[p]=n;
p++;
}
}
printf("All of the unique numbers found are: ");
for(int i=0;i<p;i++)
{
printf("%d ",a[i]);
if(i%4==0 && i>0)
printf("\n ");
}
return 0;
}
int isUnique(int a[],int b,int n)
{
for(int i=0;i<=b;i++)
{
if(a[i]==n)
return 0;
}
return 1;
}
Sample run
.