Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 31st, 2006, 8:40 PM   #21
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
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++;
	 
}
codylee270 is offline   Reply With Quote
Old Oct 31st, 2006, 8:52 PM   #22
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
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 << "}" ;
The Dark is offline   Reply With Quote
Old Oct 31st, 2006, 9:16 PM   #23
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
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...
codylee270 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:18 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC