In: Computer Science
(duplicate Elimination) use a one-dimensional array to slove the following program. read 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print if only if its 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 slove this problem. sort the array in ascending order and print the elements of the sorted array. This much be done in C language and describe each line by including comments beside your statements
Source Code:
#include <stdio.h>
void printDistinct(int arr[], int n)
{
// Pick all elements one by one
for (int i=0; i<n; i++)
{
// Check if the picked element is already printed
int j;
for (j=0; j<i; j++)
if (arr[i] == arr[j] || arr[i]<10 || arr[i]>100)
break;
if(arr[i]<10 && arr[i]>100)
break;
// If not printed earlier, then print it
if (i == j)
printf("%d ",arr[i]);
}
}
// Driver program to test above function
int main()
{
int arr[20];
printf("Enter 20 numbers between 10-100\n");
for(int i=0;i<20;i++)
{
scanf("%d",&arr[i]);
}
int n = sizeof(arr)/sizeof(arr[0]);
printDistinct(arr, n);
return 0;
}
Note: Program is written in C and all the given conditions are set.
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! ===========================================================================