0

I want to sort chopsticks by weight and I want the matching pair of chopstick at the end of program. Should I use sort algorithm? I want to sort it by weight means (e.g 110, 90, 110, 90) so in this case I got 2 pair. One having weight 90 and other having weight 110, but how can I track that? I know I have to use counter but how I don not know.

#include<iostream>
#include<algorithm>
#include<vector> 

using namespace std; 

bool myChopsticks (int i, int j) 
{
return (i<j);
} 

int main() 
{ 
int myintergers []= {90, 110, 90, 110}; 

vecotr<int> myvector(myintergers, myintergers+4); 

vector<int>::iterator it; 
sort (myvector.begin(), myvector.end()+2);
 cout << "myvector contains:"; 

for (it=myvector.begin(); it!=myvector.end(); ++it) 

cout << " " << *it; cout << endl;
 return 0;
}
14
  • What do you mean by matching pair of chopstick. Commented Sep 15, 2010 at 14:34
  • I am thinking this will be the code Commented Sep 15, 2010 at 14:34
  • 2
    Is this homework? Post your code so far so we can comment in a meaningful way. Commented Sep 15, 2010 at 14:35
  • Much better question than the last - but you should still tag the question with homework as I have done. You will get the best responses by showing what you have thought about and why (and posting as much code as you have) Commented Sep 15, 2010 at 14:36
  • 2
    @Cool: the statement sort (myvector.begin(), myvector.end()+2); has a problem. You should remove the +2 because that tries to sort nonexistent elements past the end of the vector. Commented Sep 15, 2010 at 15:23

3 Answers 3

1

Yes, I think so.

By sorting the chopsticks, you will have them in order. Then, it will be much easier (with just a single iteration through the ordered set) to find the pairs with equal weight.

Sign up to request clarification or add additional context in comments.

Comments

0

It seems enough to store a list of chopsticks (possibly in a vector<>) then either iterate through them to find the match(es) and display it.

Alternately a std:mapping implements a binary tree for you that should make the search for matches very efficient.

Comments

0

If you know there is only one matched pair, then use a std::map<weight, chopstick> and insert at given point after doing a find(newWeight) call. If find() returns an exact match at any point then you are done.

If there is more than one possible matching pair then use std::multimap<weight, chopstick> and then iterate the resulting container to count() the number of matches for each distinct value.

If you don't need the actual chopsticks at the end, but just a count of matching pairs, then you can use std::set<weight> or std::multiset<weight> instead.

3 Comments

How to use count() in above program?
I imagine this homework is intended to help you learn independently as well as research. I don't intend to actually write your code. If you would care to update your question with any alternate attempts, I am sure people will be glad to help you workout why it's not working. Good luck.
Here's a pointer to get you started on the multimap container: msdn.microsoft.com/en-us/library/1ka6hya8(v=VS.71).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.