Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to run my C++ program with my professor\'s main.C file. it has worke

ID: 3806400 • Letter: I

Question

I am trying to run my C++ program with my professor's main.C file. it has worked previously with a smaller main file but this larger main file that was released I am receiving a bus error 10 when attempting to compile. I am pretty sure this has to do with an error in the memory requested (The program is separately compiled) The main file is a .C file but that should be no issue when compiling.

"header.h":

#include

using namespace std;

"number.h":

#define NUM_OF_SETS 5

"set.h":

#include "header.h"

class Node
{  
public:
   //int position;
   int value;
   Node* next;

};

class Set
{  
private:
   Node* head;
   //Node* current;

public :
   Set();
   Set(const Set& s);   // copy constructor
   ~Set();

   void clear();
   void addToSet(int i);
   void removeFromSet(int i);
   int cardinality();
   int is_member(int i);
  
   void operator=(const Set& s); // copy a set
   Set operator&(const Set& s);   // intersection
   Set operator|(const Set& s);   // union
   void printSet();
  

};

"set.cpp":

#include "header.h"
#include "set.h"

Set::Set() //constructor
{
   head = NULL; //no items in list yet  
}


Set::Set(const Set& s)
{
   Node *temp, *current;
temp = s.head;

while(temp)
{
/* create a new node by copying the information from the temp */
Node *n = new Node;
n->value = temp->value;
n->next = NULL;

if(head == NULL)
{
    head = n;
    current = n;
}

else
{
   current->next = n; current = current->next;}
    temp = temp->next;
}

}

Set::~Set () { }

void Set::clear()
{
if (head)
{
   head = NULL;
}

}

void Set::addToSet(int i)
{  

   Node* current = head;
bool duplicate = false;

while(current && !duplicate)
{
   if(current->value == i)
       {
           duplicate = true; break;
       }
else if(current->next)
   {
       current = current->next;
   }
else break;
}

   if(!duplicate)
{

   Node *n = new Node;
   n->value = i;
   n->next = NULL;

   if(current)
       {
           current->next = n;
       }
   if(head == NULL)
       {
           head = n;
       }
    }
}

void Set::removeFromSet(int i)
{
   Node *n = head;
   Node *prev = NULL;

   while(n)
   {
       if( n->value == i)
       {
           if(prev)
           {
               prev->next = n->next;
               n->next = NULL;
               delete n;
           }
           else
           {
               prev = head;
               head = head->next;
               prev->next = NULL;
               delete prev;
           }
           break;
       }
       else
       {
           prev = n;
           n = n->next;
       }
   }
}

int Set::cardinality()
{
Node *n = head;
int count = 0;
while(n)
{
count++;
n = n->next;
}

   return count;
}

int Set::is_member(int i)
{
   Node *n = head;

   while(n)
   {
       if(n->value == i)
       {
           return 1;
       }
       else
       {
           n = n->next;
       }
   }

   return 0;
}

void Set::operator=(const Set& s)
{

    Node *n = s.head;
    clear();

   while(n)
    {
       addToSet(n->value);
       n = n->next;
   }
  
}

Set Set::operator&(const Set& s)
{
   Set i_set; //create a new empty set

   Node *n = s.head;

   while(n)
   {
       if(is_member(n->value) == 1)
       {
           i_set.addToSet(n->value);
       }

       n = n->next;
   }
  
   return i_set;
}

Set Set::operator|(const Set& s)
{
   Set u_set(s); //create a new set using the copy constructor

   Node *n = head;

   while(n)
   {
       u_set.addToSet(n->value);
       n = n->next;
   }

   return u_set;
}

void Set::printSet()

{
   Node *current = head;

   while(current)
   {
       cout << current->value << " " ;
       current = current->next;
   }

   cout << endl;
}

"main.C":

#include "set.h"
#include "number.h"
#include "header.h"
#include

int main()
{

Set sets[NUM_OF_SETS];

int i,rnd1, rnd2, rnd3, rnd4;

for( i = 0; i < NUM_OF_SETS * 5; i++)
{
   rnd1 = random() % 160;
rnd2 = random() % NUM_OF_SETS;
sets[rnd2].addToSet(rnd1);
}
  
for( i = 0; i < NUM_OF_SETS; i++ )
{
   rnd1 = random() % 160;
rnd2 = random() % NUM_OF_SETS;
sets[rnd2].removeFromSet(rnd1);
}
  
// sets[3] = sets[1] & sets[2];
// sets[3] = sets[1] | sets[2];
for( i = 0; i < NUM_OF_SETS * 400000; i++)
{
rnd1 = random() % NUM_OF_SETS;
rnd2 = random() % NUM_OF_SETS;
rnd3 = random() % NUM_OF_SETS;
   rnd4 = random() % 100;
if( rnd4 < 49 )
sets[rnd3] = sets[rnd1] & sets[rnd2];
else
sets[rnd3] = sets[rnd1] | sets[rnd2];
}
  

/* And for some "sampling" of output */
for( i = 0; i < 10; i++)
{
   rnd1 = random() % 160;
rnd2 = random() % NUM_OF_SETS;
cout << "sets[" << rnd2 << "] =";
sets[rnd2].printSet();
cout << endl;
}
return 0;

}

Explanation / Answer

This error may be coming because of using random() function. See this function is completely valid one and you can use it, but as far as I think, if possible, you should use the rand() function here. The thing is, I am not able to see the header file names you wrote here because they are written in between < and > so they are considered as HTML tag by the web browser but you may have used stdlib.h header file here. random() and rand() both are its functions.

The reason not to use the random() function is, its signature is long int random(void) in its declaration so its return type is long int and you are assigning it to lower type without casting so this may cause this error. So it is true that it may compile, but may fail to execute as this issue comes at run time when random() is returning a number. So, I replaced random() with rand() in this code, then I tried to compile and execute this and see the output I got:

Output:

sets[3] =113 91 142 102 69 106 122 9 109 131 24

sets[2] =113 91 142 102 69 106 122 9 109 131 24

sets[0] =113 91 142 102 69 106 122 9 109 131 24

sets[2] =113 91 142 102 69 106 122 9 109 131 24

sets[0] =113 91 142 102 69 106 122 9 109 131 24

sets[2] =113 91 142 102 69 106 122 9 109 131 24

sets[0] =113 91 142 102 69 106 122 9 109 131 24

sets[1] =113 91 142 102 69 106 122 9 109 131 24

sets[4] =113 91 142 102 69 106 122 9 109 131 24

sets[0] =113 91 142 102 69 106 122 9 109 131 24

I have not gone through exact code working but this means if I am getting an output, this bus error is removed. So modify this thing and your code will work fine.

Please comment if there is any query. Thank you. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote