arrays and index [closed]

£可爱£侵袭症+ 提交于 2019-12-13 09:44:56

问题


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;
}

回答1:


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.




回答2:


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)




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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; 



回答7:


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;)



回答8:


Take a closer look at where you increment x.




回答9:


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;
}


来源:https://stackoverflow.com/questions/4430835/arrays-and-index

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!