-4

How can I enter numbers into an array such that duplicate entries are ignored?

For example, if I put 6 and then 3 into the array, attempting to then insert 6 into the the array should cause 6 to be rejected (since it is already in the array).

#include <iostream>

using namespace std;
int main()
{
  int x,y;
  int number;
  int arr[5];

  for (x=0; x<5; )
  {
    cout<<"enter a number:"<<endl;
    cin>>number;
    bool replace = True;
    for (y=0; y<x; y++)
    {
       if (number != arr[y])
       {
         cout << "try next time" << endl;
         replace = False;
         break;
       }
    }

    if (replace)
    {
      arr[x] = number;
      x++;
    }
  }
  return 0;
}
unDeadHerbs
  • 1,306
  • 1
  • 11
  • 19
jboy
  • 11
  • 3
  • 2
    @jboy - if this is homework, please add the appropriate tag by editing your question. The community will also be much more receptive if you post code showing what you have tried so far. – Steve Townsend Dec 13 '10 at 16:15
  • 1
    ok ill show my code help me pls.. – jboy Dec 13 '10 at 16:20
  • I reformatted the code and noted 2 things - 1. You didn't cut/paste because the indenting is invalid. 2. There are extraneous 'flags' lines that will cause non-compiles. Fix these two items and then we can help to figure it out. – KevinDTimm Dec 13 '10 at 16:26
  • 1
    What is 'False', 'True' ? Posting uncompilable and ever-changing code is very annoying. – Steve Townsend Dec 13 '10 at 17:28
  • 1
    @jboy: Steve is actually helping you when editing your code. Try to learn from it. If this really is an exam question... Wow.. – default Dec 13 '10 at 17:34
  • 1
    @Steve - mea culpa - I see the OP has no grasp of c++ and I jump around too much and so recommended boolean... True/False instead of bool... true/false – KevinDTimm Dec 13 '10 at 17:34
  • 1
    @KevinDTimm - no problem. Seems like OP needs to ask his professor for some C++ bootstrapping guidance. Let's hope this is not the final exam. – Steve Townsend Dec 13 '10 at 17:36
  • @Steve - If you follow my post exactly, you'll see that I create a running program. But I think the bootstrapping is what's necessary as the lack of knowledge is a huge barrier to understanding any of this. – KevinDTimm Dec 13 '10 at 17:37
  • Eh? You talk as if everybody knows what you're talking about. What is your question? – Etienne de Martel Dec 13 '10 at 17:57
  • He's assuming all of us will copy paste the code, compile and run it I guess :p – Andreas Wong Dec 13 '10 at 17:57
  • @Etienne - this relates to a prolonged effort to get a decent question asked earlier today. Code is at least formatted properly and compilable now :-) The problem of "please tell me the code" remains. – Steve Townsend Dec 13 '10 at 17:58
  • @jboy - what do you mean by 'rejected'. How are you rejecting duplicate inputs? – Steve Townsend Dec 13 '10 at 17:59
  • There are 2 failures in the code (but yes, it would be better if the original question was asked) – KevinDTimm Dec 13 '10 at 18:00
  • Is it that number is misspelled? – KLee1 Dec 13 '10 at 18:03
  • @KLee1 - no, that's just a label. – KevinDTimm Dec 13 '10 at 18:04
  • @steve- i mean you cant inut the same number again that what it is.. – jboy Dec 13 '10 at 18:06
  • 2
    Folks, I know this kind of thing is both rude and all too common, but we want Stack Overflow to be a welcoming place. Accordingly, you should **politely** refuse to answer such questions. I'm looking at Blender and rerun in particular; Frustrated gets a pass for being silly. – dmckee --- ex-moderator kitten Dec 13 '10 at 21:55

9 Answers9

4

std::set<int> would do what you want. This is not indexable, though.

You could use Boost.MultiIndex to give you random access and enforce uniqueness on the same underlying list of values.

btw - asking directly for code is not recommended practice.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • what i mean is if you type the same number again it will deny becoz the number you imput is already in the index,this is my code : – jboy Dec 13 '10 at 16:18
  • 4
    @jboy - The word you're looking for is 'because' – KevinDTimm Dec 13 '10 at 16:22
  • im just a student and i want to figured out whats wrong in my codes so i can fix it,just this time pls... – jboy Dec 13 '10 at 16:23
  • 1
    @jboy - posting code in comments is tough to make clear and hard to find for new responders. Paste the code into your question text, select it and then format it by pressing the '101010' icon. Thanks. – Steve Townsend Dec 13 '10 at 16:23
  • @steve- where can i find that icon? – jboy Dec 13 '10 at 16:31
  • @jboy - you should see it above the top of the textbox when you are editing your question. There are 'quote' and 'URL' icons too – Steve Townsend Dec 13 '10 at 16:36
  • pls teach me the missing code – jboy Dec 13 '10 at 16:49
  • This is not a 'homework for hire' site. You need to do the work yourself. What's not working in your code? Flow of control looks reasonable apart from the strange 'flags' which obviously won't compile. I suggest you forget about this question and post a new one that clearly describes what is not working from your point of view, with compilable code. "It is not working" is an invitation to more downvotes. "I wind up with duplicate entries" is better. Add more output inside your loops to dump the full array on each loop. This is what programmers do. – Steve Townsend Dec 13 '10 at 16:54
  • @steve-how?just teach me step by step how so i can solve it on my own.. – jboy Dec 13 '10 at 17:01
  • the codes now run but if i input the same number it still run,it supose to reject the number if i put the same number. – jboy Dec 13 '10 at 17:03
4

you have too many x++'s and you don't preset arr (maybe more style than error)

how do you know it's not working? (put some debug code inside of if (number == arr[y]) and if (replace)

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
2

What you really want is a set. Sets cannot contain duplicate elements.

Here is a reference to the set in C++.

Just use the set as a container for your numbers. When you try to add a duplicate, it will be automatically rejected.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
1

You don't want an array but a datastructure called Hashtable for that;

Alternatively, you might want to look up a datastructure called associative array.

Falcon
  • 3,150
  • 2
  • 24
  • 35
1

You shouldn't use arrays for this. You should use, for example, std::set. Or, if you need to have an array as your data structure, you could encapsulate the array (e.g. realized through std::vector) in a class and define specific functions to access the array elements. Additionally, you could hold a std::set to provide a fast check for existing elements.

Flinsch
  • 4,296
  • 1
  • 20
  • 29
  • 1
    It's homework - pointing him towards std:set is counterproductive in that he's doing what the assignment tells him. hopefully std:set will come into play next week :) – KevinDTimm Dec 13 '10 at 16:35
  • 1
    @KevinDTimm: I dunno, it's already December. I would have thought most schools would be in exam mode by now. `std:set` might not come until next term in January. – FrustratedWithFormsDesigner Dec 13 '10 at 16:39
  • You're right, they are. But to most college students, January is next week (even if next term is a month away) – KevinDTimm Dec 13 '10 at 16:41
  • but here in us its our exam week and this is our exam about array,make a progrm in arrays and this is wat our teacher thought us – jboy Dec 13 '10 at 16:55
1

Should be :

int arr[5] = {0,0,0,0,0};

Remove the x++ from the following line:

for (x=0;x<5;x++)

Then:

bool replace=true;
for (y=0;y<x;y++)
{
   if (number == arr[y])
   {
      replace=false;
      break;
   }
}

if (replace)
{
      arr[x]=number;
      x++;
}

Finally, remove the :

else if(number == arr[x])
{
    arr[x]=number;

cout << "try next time"<<endl;
}

You can insert :

cout << "try next time"<<endl;

before the

replace=false; 
KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
  • it run but without xtt in for (x=0 its error – jboy Dec 13 '10 at 16:42
  • sorry, that doesn't help (especially since the code in your post IS NOT what you are using - it would never compile) – KevinDTimm Dec 13 '10 at 16:43
  • i try it but it was errors,do i also delete the flgs? – jboy Dec 13 '10 at 16:51
  • #include using namespace std; int main() { int x,y; int number; int arr[5]; for (x=0;x<=4;x++) { cout<<"enter nmber"<>number; for (y=0;x<=y;y++) { if (number == arr[x]) { arr[x]=number; } else if(number == arr[x]) { arr[x]=number; } } } return 0; } heres my code in my compiler – jboy Dec 13 '10 at 16:58
  • so what are those things ht is missing... – jboy Dec 13 '10 at 17:04
  • so whats the right code? – jboy Dec 13 '10 at 17:11
  • update the original post - what you have in the comments is indecipherable. – KevinDTimm Dec 13 '10 at 17:20
  • i edit it and it say the tru and false was not declared in the scoop – jboy Dec 13 '10 at 17:23
  • 1) it should be bool true/false not boolean True/False. 2) post your REAL code in the original post so it can be read. – KevinDTimm Dec 13 '10 at 17:25
  • i post it,its still error,how to make it run? – jboy Dec 13 '10 at 17:33
  • stll the true and false is not on the scoop – jboy Dec 13 '10 at 17:42
  • 1
    I don't think I can help you any more. When I follow the instructions above, I create a compilable executable which conforms to your requirements. Since you don't post the changed code when you report more errors, I don't know what you're attempting to compile. Since the code is being modified I would recommend that each iteration be posted on pastebin and pointed to from your post. – KevinDTimm Dec 13 '10 at 17:45
  • @jboy: and the link to that question is...? – FrustratedWithFormsDesigner Dec 13 '10 at 18:15
  • @Frustrated - http://stackoverflow.com/questions/4431836/arrays-and-index-2/4431896 – KevinDTimm Dec 13 '10 at 18:53
0

Take out the x++ in the for loop, That way you will only increment that count when you enter a new number.

Also, if you want to only run the loop five times, your outer for loop should be only to x<5.

All in all your outer loop should read:

for (x=0;x<5;)
KLee1
  • 6,080
  • 4
  • 30
  • 41
  • He only gets half the modifications he's supposed to get from the OP (these are the 2 that have been missed, referred to in my comment above and one of them in my answer below - I was hoping he would note the mistakes in his 'code review') – KevinDTimm Dec 13 '10 at 18:09
0

Take a closer look at where you increment x.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
-1

It looks like you want to read in a sequence of numbers eliminating any duplicates. It also appears that the maximum number of unique numbers is 5.

int n = 0;  /* The number of unique numbers read in so far */
for {;;}
  cout << "enter nmber" << endl;
  cin >> number;
  for (x=0; x < n; ++x) {
    if (number == arr[x]) goto L1;  /* I love messing with peoples head by using this goto */
  }
  arr[n] = number;
  ++n;
  if (n == 5) break;
L1:
  continue;
}
ann
  • 576
  • 1
  • 10
  • 19
Larry
  • 1