![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Dark, thank you very much - it did the trick. But could you explain you other method a little more. I'm very confused on how that works. Also, I've actually updated my overloaded += b/c the random number generator is allowing non-unique values to be added into the set. I just used a simple if to test if the number was already in the set, but now I get junk numbers in the original set, ex:
{5,2,17,8, -75834454567076, 4,5 ,7, -80938543} Here's the function, and Dark, THANK YOU FOR ALL OF YOUR HELP! template <class element_type>
void MySet<element_type>::operator +=(element_type addedElement) //Postcondition - adds int value into activating set if max size isn't achieved - unique values only
// Void- no return
{
assert( (cardinality() + 1) <= maxSetSize);
if(isFull())
cout << endl << "Action can't be completed. Set is full." << endl;
else if(!inSet(addedElement)) //Don't add an item that already exists
Container[setSize] = addedElement;
setSize++;
} |
|
|
|
|
|
#22 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
In your posted code, look at what happens if the number is already in the set.
For the print loop, it is easier to tell if a value is the first to be printed than it is to tell if it is the last to printed. So checking whether to print a comma before a number is easier than checking whether to print a comma after a number. e.g.: // Prints in { 1, 2, 3 } format keeping a stray "," from the end
cout << "{";
bool isFirst = true; // Flag to see if we have printed anything yet
for( int k = 0; k < size; k++)
{
if (arr[k] == true)
{
if (isFirst)
isFirst = false; // Don't print a comma before the first item
else
cout << ","; // Not the first item so print a comma
cout << this->Container[k];
}
}
cout << "}" ;a better way again might be to count the true vals during the loop // Prints in { 1, 2, 3 } format keeping a stray "," from the end
cout << "{";
int trueVals = 0;
for( int k = 0; k < size; k++)
{
if (arr[k] == true)
{
if (trueVals != 0)
cout << ","; // Not the first item so print a comma
cout << this->Container[k];
trueVals++;
}
}
cout << "}" ; |
|
|
|
|
|
#23 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Ahhhh! Now that does make sense... and as for my previous post, i just moved setSize++ into the second if, where it's supposed to be...
Dark, you are my hero... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Combining languages | titaniumdecoy | Other Programming Languages | 12 | Jul 13th, 2006 3:03 PM |
| Compiling Maverik 6.2 (from C) | megamind5005 | C | 16 | May 3rd, 2006 6:41 PM |
| libraries | matko | C | 1 | Jan 22nd, 2006 3:12 PM |
| Jackpot game | zorin | Visual Basic | 3 | Jun 10th, 2005 2:19 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 5:12 PM |