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
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;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
