Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELP WITH C++ PROJECT!! I am trying to get this code to work... cerr < < t=========Test #14: Star Classes =========== ; Starlist *

HELP WITH C++ PROJECT!!

I am trying to get this code to work...

cerr << " \t=========Test #14: Star Classes =========== "; Starlist * sl = new Starlist(); assert(sl->getCurrentNumPlanets() == 0); id_list[0] = sl->addPlanet(); assert(sl->getCurrentNumPlanets() == 1); p = sl->getPlanet(id_list[0]); int pos = p->getPos(); sl->orbit(); assert(p->getPos() == (pos + 1) % 360);

Starvector * sv = new Starvector(); assert(sv->getCurrentNumPlanets() == 0); id_list[1] = sv->addPlanet(); assert(sv->getCurrentNumPlanets() == 1); p = sv->getPlanet(id_list[1]); pos = p->getPos(); sv->orbit(); assert(p->getPos() == (pos + 1) % 360);

//add 200 more planets to each for(int i = 0; i < 200; i+=2){ id_list[i] = sv->addPlanet(); id_list[i+1] = sl->addPlanet(); } //delete 10 random planets in the sun for(int i = 0; i < 50; i++){ int rand_id = (rand() % (200)); if(id_list[rand_id] == -1){ continue; }else if(sv->removePlanet(id_list[rand_id]) || sl->removePlanet(id_list[rand_id])){ id_list[rand_id] = -1; }else{ assert(false); } }

sv->printStarInfo(); sl->printStarInfo();

cerr << " \t\tTest #14 Passed... ";

HERE IS MY STARVECTOR.CPP

#include "Starvector.h" #include #include using namespace std; //Type ** obj = new type*[size];

Starvector::~Starvector(){//the destructor is a member function which destructs or deletes an object. // Delete the pointers inside of the array delete vector; }

Starvector::Starvector(){ vector = new Vector(); } bool Starvector::removePlanet(long id){ for(int i = 0; i < (int)vector->size(); i++){ if((vector->read(i))->getID() == id){ vector->remove(i); return true; } } return false; }

long Starvector::addPlanet(){ Planet* temp = new Planet(rand()%3001); vector->insert(vector->size(), temp); return temp->getID(); }

Planet * Starvector::getPlanet(long id){ for(int i = 0; i < (int)vector->size(); i++){ if((vector->read(i))->getID() == id) return vector->read(i); } return NULL;

}

void Starvector::printStarInfo(){ cout<<"The star currently has "<< vector->size() << "Planets."<size(); i++){ Planet* curr = vector->read(i); cout << "Planet " << curr->getType() << curr->getID() << " is " << curr->getDistance() << " million miles away at position " << curr->getPos() << " around the star." << endl; }

} void Starvector::orbit(){ for(int i = 0; i < (int)vector->size(); i++){ (vector->read(i))->orbit(); } } unsigned int Starvector::getCurrentNumPlanets(){ return vector->size(); }

Here is my Starlist.cpp

#include "Starlist.h" #include #include using namespace std;

Starlist::~Starlist(){ delete data; }

Starlist::Starlist(){ this->data = new List(); } bool Starlist::removePlanet(long id){ for(int i = 0; i < (int)data->size(); i++){ if(data->read(i)->getID() == id){ return data->remove(i); } } return false; }

long Starlist::addPlanet(){ Planet * p = new Planet(rand()%3001); data->insert(data->size(), p); return p->getID(); }

Planet * Starlist::getPlanet(long id){ for(int i = 0; i < (int)data->size(); i++){ if(data->read(i)->getID() == id){ return data->read(i); } } return NULL; }

void Starlist::printStarInfo(){ cout<<"The star currently has "<< data->size() << "planets."<size(); i++){ Planet* curr = data->read(i); std::cout << "\tPlanet " << curr->getType() << curr->getID() <<" is " << curr->getDistance() << " million miles away at position "<< curr->getPos()<< " around the star."<< std::endl; } }

unsigned int Starlist::getCurrentNumPlanets(){ return this->data->size(); }

void Starlist::orbit(){ for(int i = 0; i < (int)data->size(); i++){ (data->read(i))->orbit(); } }

I am getting this error in the terminal..

=========Test #14: Star Classes ===========

Makefile:12: recipe for target 'run' failed make: *** [run] Segmentation fault (core dumped)

I have been working on this for the past 2 hours and have no idea what i am doing wrong..

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 10g Insider Solutions

Authors: Arun R. Kumar, John Kanagaraj, Richard Stroupe

1st Edition

0672327910, 978-0672327919

More Books

Students also viewed these Databases questions

Question

2-10 Why is the term manufacturing expenses a misnomer?

Answered: 1 week ago

Question

a sin(2x) x Let f(x)=2x+1 In(be)

Answered: 1 week ago

Question

Describe a persuasive message.

Answered: 1 week ago

Question

Identify and use the five steps for conducting research.

Answered: 1 week ago

Question

List the goals of a persuasive message.

Answered: 1 week ago