By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

An array program

P: 1
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
Sep 29 '06 #1
Share this Question
Share on Google+
1 Reply


P: 41
Expand|Select|Wrap|Line Numbers
  1.         int arr[20];
  2.         memset(arr, 0, 20*sizeof(int));
  3.         int cnt=0;
  4.         int n;
  5.         while(cnt<20)
  6.         {
  7.             cout<<"Input value "<<cnt+1<<" :";
  8.             while(true)
  9.             {
  10.                 cin>>n;
  11.                 bool existed=false;
  12.                 for(int i=0; i<cnt; i++)
  13.                 {
  14.                     if(n==arr[i])
  15.                     {
  16.                         existed=true;
  17.                         break;
  18.                     }
  19.                 }
  20.                 if(existed==false)
  21.                 {
  22.                     arr[cnt++]=n;
  23.                     break;
  24.                 }
  25.             }
  26.         }
  27.         cout<<endl<<"Result"<<endl;
  28.         for(int i=0; i<20; i++)
  29.             cout<<arr[i]<<endl;
  30.  
Sep 30 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.