An array program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • buddy
    New Member
    • Sep 2006
    • 1

    An array program

    I am having trouble writing a program that uses a one-dimensional array that reads 20 numbers each of which is between 10 and 100. As each number is read, it has to be validated and stored in the array only if it is not a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide the "worst case" in which all 20 numbers are different. Use the smallest possible array to solve this problem.

    Buddy
  • ltgbau
    New Member
    • Sep 2006
    • 41

    #2
    Code:
    		int arr[20];
    		memset(arr, 0, 20*sizeof(int));
    		int cnt=0;
    		int n;
    		while(cnt<20)
    		{
    			cout<<"Input value "<<cnt+1<<" :";
    			while(true)
    			{
    				cin>>n;
    				bool existed=false;
    				for(int i=0; i<cnt; i++)
    				{
    					if(n==arr[i])
    					{
    						existed=true;
    						break;
    					}
    				}
    				if(existed==false)
    				{
    					arr[cnt++]=n;
    					break;
    				}
    			}
    		}
    		cout<<endl<<"Result"<<endl;
    		for(int i=0; i<20; i++)
    			cout<<arr[i]<<endl;

    Comment

    Working...