Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7823317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:12:08+00:00 2026-06-02T08:12:08+00:00

I am having problems with an assignment. The assignment is to create an IntegerSet

  • 0

I am having problems with an assignment. The assignment is to create an IntegerSet
an IntegerSet is an array of 100 elements that represents the numbers from 0 to 99
for example if the number 5 is present in set a then a[5] = 1, an empty set is an array of zeroes.

I created a class called IntegerSet and here is the code in integerSet.cpp

#include "integerset.h"
#include <iostream>
using std::cout;
using std::endl;

IntegerSet::IntegerSet(){
    int temp[100] = {0};
    set = temp;
}

IntegerSet::IntegerSet(int * setPtr) {
    set = setPtr;
}

void IntegerSet::insertElement(int toInsert) {
    if(toInsert < 100 && toInsert >= 0) {
        set[toInsert] = 1;
    }
}

void IntegerSet::deleteElement(int toDelete) {
    if (toDelete < 100 && toDelete >= 0 ) {
        set[toDelete] = 0;
    }
}

IntegerSet * IntegerSet::unionOfSets(IntegerSet * otherPtr) {
    int newSet[100] = {0};
    for(int i = 0; i < 100; i++ ) {
        if (this->set[i] == 1 || otherPtr->set[i] == 1) {
            newSet[i] = 1;
        }
    }
    return new IntegerSet(newSet);
}

IntegerSet * IntegerSet::intersectionOfSets(IntegerSet* otherPtr) {
    int newSet[100] = {0};
    for(int i = 0; i < 100 ; i++) {
        if(this->set[i] == 1 && otherPtr->set[i] == 1){
            newSet[i] = 1; 
        }
    }
    return new IntegerSet(newSet);
}

bool IntegerSet::isEmpty(){
    for(int i = 0 ; i < 100 ; i++) {
        if(set[i] == 1) {
            return false;
        }
    }
    return true;
}  

bool IntegerSet::isEqualTo(IntegerSet * otherPtr) {
    for(int i = 0; i < 100 ; i++) {
        if(this->set[i] != otherPtr->set[i]) {
            return false;                
        }        
    }
    return true;
}

void IntegerSet::printSet() {
    if(isEmpty()) {
        cout << "---" << endl;
    } else { 
        for(int i = 0; i < 100 ; i++) {
            if(set[i] == 1) {
                cout << i << ' ';
            }
        }
        cout << endl;        
    }
}   

IntegerSet::~IntegerSet() { 
    delete[] set;
}     

The class has private member int * set;.

This is the main function used to test my class:

#include <iostream>
#include<new>
#include "integerset.h"

using std::cout;
using std::endl;

int main(int argc, char *argv[])
{
    IntegerSet * set1Ptr = new IntegerSet();
    set1Ptr->insertElement(1);
    set1Ptr->insertElement(2);
    set1Ptr->insertElement(3);
    set1Ptr->insertElement(50);
    IntegerSet * set2Ptr = new IntegerSet();
    set2Ptr->insertElement(0);
    set2Ptr->insertElement(3);
    set2Ptr->insertElement(2);
    set2Ptr->insertElement(51);
    set2Ptr->insertElement(100);
    set2Ptr->insertElement(99);
    IntegerSet * set3Ptr = set1Ptr->unionOfSets(set2Ptr);
    IntegerSet * set4Ptr = set1Ptr->intersectionOfSets(set2Ptr);
    cout << "First Set" << endl;
    set1Ptr->printSet();
    cout << "Second Set" << endl;
    set2Ptr->printSet();
    cout << "Equal ? : " << set1Ptr->isEqualTo(set2Ptr) << endl;
    cout << "Intersection : " << endl;
    set4Ptr->printSet();
    cout << "Union : " << endl;
    set3Ptr->printSet();
    system("PAUSE");
    return EXIT_SUCCESS;
}

This the output I get when running

First Set
16 19 35 45 46 54 66 84
Second Set
0 1 10 12 13 14 19 35 45 46 54 66 84
Equal ? : 1
Intersection :
16 19 35 45 46 54 66 84 98
Union :
0 1 10 12 13 14 19 35 45 46 54 66 84 98

I tried tracing and I have no idea what’s wrong, so any help is really appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:12:10+00:00Added an answer on June 2, 2026 at 8:12 am
    IntegerSet::IntegerSet(){
       int temp[100] = {0};
       set = temp;
    }
    

    That is creating a local array of 100 elements and storing a pointer to it in the member set. The problem here is that the lifetime of temp is restricted to the constructor, and as soon as the constructor exits, the array is destroyed and what you have is a dangling pointer (a pointer to a block of memory that is not valid).

    If you need to use pointers then you should dynamically allocate the memory and make sure to release it in the destructor. If not, you can declare the array as a member attribute of your type and avoid the pointers altogether.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problems with the Virtualprotect() api by windows. I got an assignment from
I have an assignment that I have to create a randomly sized 3D array,
I am having some problems with an assignment. The case project is: Create an
im having problems starting a codeigniter project, the problem is that when i do
im having problems on how to save the content of an array in an
I have started having problems with my VPS in the way that it would
I'm having problems with const char array being stored to a struct, then when
I'm working on a homework assignment that asks me to create a calculator that
I am having problems with my java assignment. I have been working on it
Seem to be having an issue with std::auto_ptr and assignment, such that the object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.