Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Convert array to pointers

Convert array to pointers

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KARFER
    wrote on last edited by
    #1

    Hello All I have This Code [assignment] and I want the user to control over the maximum size of CSet. using integer pointer instead of integer array and by constructor can specifies the desired size. #include <iostream> #include <conio.h> using namespace std; const int maxCard = 100; class CSet { private: int elems[maxCard]; int card; public: CSet() { card = 0; return; } bool Member (const int); void AddElem (const int); void RmvElem (const int); void Copy (CSet &); bool Equal (CSet &); void intersect (CSet &,CSet &); void Union (CSet &,CSet &); void print (); }; bool CSet::Member(const int elem) { for (register int i = 0;i < card;++i) if (elems[i] == elem) return true; return false; } void CSet::AddElem(const int elem) { if (Member(elem)) return; if (card < maxCard) elems[card++] = elem; else cout <<"Set Overflow"<<endl; } void CSet::RmvElem(const int elem) { for (register int i = 0;i <card;++i) if (elems[i] == elem) { for (;i<card - 1;++i) elems[i] = elems[i + 1]; --card; } } void CSet::Copy(CSet &set) { for (register int i =0;i<card;++i) set.elems[i] = elems[i]; set.card = card; return; } bool CSet::Equal(CSet &set) { if (card != set.card) return false; for (register int i = 0;i < card;++i) if (!set.Member(elems[i])) return false; return true; } void CSet::intersect(CSet & set,CSet &res) { res.card = 0; for (register int i = 0;i <card;++i) if (set.Member(elems[i])) res.elems[res.card++] = elems[i]; return; } void CSet::Union(CSet &set,CSet &res) { set.Copy(res); for (register int i=0;i <card;++i) res.AddElem(elems[i]); return; } void CSet::print() { cout <<"{"; for (int i=0;i<card-1;++i) cout <<elems[i] <<","; if (card > 0) cout <<elems[card - 1]; cout <<"}\n"; return; } int main () { CSet s1,s2,s3; s1.AddElem(10); s1.AddElem(20); s1.AddElem(30); s1.AddElem(40); s2.AddElem(30); s2.AddElem(50); s2.AddElem(10); s2.AddElem(60); cout <<"S1 = "; s1.print(); cout <<"\nS2 = "; s2.print(); cout <<endl; if (s1.Member(20)) cout <<"20 is in s1"<<endl; s1.intersect(s2,s3); cout <<"s1 intersection s2="; s3.print(); s1.Union(s2,s3)

    G 1 Reply Last reply
    0
    • K KARFER

      Hello All I have This Code [assignment] and I want the user to control over the maximum size of CSet. using integer pointer instead of integer array and by constructor can specifies the desired size. #include <iostream> #include <conio.h> using namespace std; const int maxCard = 100; class CSet { private: int elems[maxCard]; int card; public: CSet() { card = 0; return; } bool Member (const int); void AddElem (const int); void RmvElem (const int); void Copy (CSet &); bool Equal (CSet &); void intersect (CSet &,CSet &); void Union (CSet &,CSet &); void print (); }; bool CSet::Member(const int elem) { for (register int i = 0;i < card;++i) if (elems[i] == elem) return true; return false; } void CSet::AddElem(const int elem) { if (Member(elem)) return; if (card < maxCard) elems[card++] = elem; else cout <<"Set Overflow"<<endl; } void CSet::RmvElem(const int elem) { for (register int i = 0;i <card;++i) if (elems[i] == elem) { for (;i<card - 1;++i) elems[i] = elems[i + 1]; --card; } } void CSet::Copy(CSet &set) { for (register int i =0;i<card;++i) set.elems[i] = elems[i]; set.card = card; return; } bool CSet::Equal(CSet &set) { if (card != set.card) return false; for (register int i = 0;i < card;++i) if (!set.Member(elems[i])) return false; return true; } void CSet::intersect(CSet & set,CSet &res) { res.card = 0; for (register int i = 0;i <card;++i) if (set.Member(elems[i])) res.elems[res.card++] = elems[i]; return; } void CSet::Union(CSet &set,CSet &res) { set.Copy(res); for (register int i=0;i <card;++i) res.AddElem(elems[i]); return; } void CSet::print() { cout <<"{"; for (int i=0;i<card-1;++i) cout <<elems[i] <<","; if (card > 0) cout <<elems[card - 1]; cout <<"}\n"; return; } int main () { CSet s1,s2,s3; s1.AddElem(10); s1.AddElem(20); s1.AddElem(30); s1.AddElem(40); s2.AddElem(30); s2.AddElem(50); s2.AddElem(10); s2.AddElem(60); cout <<"S1 = "; s1.print(); cout <<"\nS2 = "; s2.print(); cout <<endl; if (s1.Member(20)) cout <<"20 is in s1"<<endl; s1.intersect(s2,s3); cout <<"s1 intersection s2="; s3.print(); s1.Union(s2,s3)

      G Offline
      G Offline
      GDavy
      wrote on last edited by
      #2

      Making a pointer array is basic stuff. Look into your book where the new and delete operators are explained. Also check the chapter which explains constructors, and the answer to your question should be clear to you. These are the basics for programming C++ so finding the answer on your own is really best for you in this case, just read these 2 chapters. The only hint I'll give you is to use a parameter in the constructor and the elems will be a pointer in stead of an array. Have fun finding the answer, it should be quite easy with the hints I gave you and if you read about the points I mentioned. Regards, Davy

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups