
Charan T.
asked 12/31/20Write a program that reads in ten numbers and displays the distinct numbers of the array
Write a program that reads in ten numbers and displays the distinct numbers of the array (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.). For this question fix the size of the input array as 10.
2 Answers By Expert Tutors

Aaron T. answered 01/01/21
Princeton Grad for Computer Science Tutoring
Hi Charan, Patrick's answer is also valid but I thought I'd also like to contribute.
I make the following assumptions:
- Since the question indicates a specific array size, I assume this is meant to be implemented in a compiled language with fixed array sizes (like C/C++ or Java) rather than an interpreted language where list/array length is not fixed (like Python)
- I assume that "number" means a signed integer (not a float)
- I decided to have the program prompt for and read user inputs sequentially, rather than have them inputted another way (e.g., as a list of command-line arguments or from a file)
Here is an implementation in C. The formatting, style, and variable names are meant to be illustrative, so I would recommend programming this on your own and using a style that accords with the target programming language and your instructor's tastes. Note that the following does not behave correctly with very large inputs; on my system, anything greater than the maximum for a 32-bit signed integer (2147483647) cause incorrect outputs. To fix this (in C), you'll need to modify the code to use a different function than scanf for input.
Hope this helps!

Patrick B. answered 12/31/20
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#define MAX (32750)
//returns the index of the integer in the array, or -1 if not found
int LinearSearch(int * A, int nSize, int iTarget)
{
int iReturn = -1;
for (int iLoop=0; iLoop<nSize; iLoop++)
{
if (iTarget == A[iLoop])
{
iReturn = iLoop;
break;
}
}
return(iReturn);
}
void Go()
{
int A[32750];
int nCount=0;
int iResponse=1;
int iTarget=-1;
memset(A,0,sizeof(int)*MAX);
while ((nCount<MAX) && (iResponse==1) )
{
printf(" Please input the number :>");
scanf("%d",&iTarget);
int iReturn = LinearSearch(A,nCount,iTarget);
if (iReturn<0)
{
A[nCount++]=iTarget;
}
do
{
printf(" Another ??? 1=YES 0=NO :>");
scanf("%d",&iResponse);
} while ((iResponse !=0) && (iResponse!=1));
}
for (int iLoop=0; iLoop<nCount; iLoop++)
{
printf("%d\n",A[iLoop]);
}
}
int main()
{
Go();
}
//-------------------------------------------------------------------------------
# python
# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
print("Hello world")
def LinearSearch ( A , iTarget):
iReturn=-1
iCount = len(A)
for iLoop in range(int(iCount)):
if int(iTarget)==int(A[iLoop]):
iReturn=int(iLoop)
break
return iReturn
iCount=0
A = [1,2,3]
A.clear()
iResponse=1
while int(iResponse)==1 and int(iCount)<32750:
iNum = input("Please input the # :>")
iReturn = LinearSearch(A,iNum)
if iReturn<0:
A.append(iNum)
iCount=iCount+1
iResponse=-1
while (int(iResponse)>1) or (int(iResponse)<0):
iResponse = input("Another??? 1=YES 0=NO :>")
for iLoop in range(int(iCount)):
print(A[iLoop])
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
Source code uploaded to RESOURCES section; done in C/C++ and java12/31/20