c++11 - C++ dynamic array ADT -


what trying create dynamic array keep track of annual salary increase rate since year of employment. have finished dynamic array. not sure if guys need header file too, here's i've got far.

#include "employee.h" #include <string> #include <iostream> #include <sstream>  //default constructor  employee::employee():person() {     years_employment = 0;     annualsalary_increaserate = null; }  employee::employee(int newid, string newname, dayofyear date, int day, int month, int year, int employ_years, double bsalary, double asalary): person(newid,newname,date), eday(day), emonth(month), eyear(year), years_employment(employ_years), basesalary(bsalary), annualsalary(asalary), annualsalary_increaserate(null) {  } //destructor employee::~employee() {        if (annualsalary_increaserate !=null)     {         delete [] annualsalary_increaserate;         annualsalary_increaserate = null;         years_employment = 0;     } }    //call copy constructor of base class    employee::employee(const employee & emp)    {        years_employment = emp.years_employment;        if (years_employment <= 0)         {           annualsalary_increaserate = null;        }         else         {            annualsalary_increaserate = new double[years_employment];            // copy data            (int = 0; < years_employment; i++)             {                annualsalary_increaserate[i] = emp.annualsalary_increaserate[i];            }        }    } employee employee::operator=(const employee& rhs) {     years_employment = rhs.years_employment;     if (years_employment <= 0)      {         annualsalary_increaserate = null;     }      else      {         annualsalary_increaserate = new double[years_employment];         // copy data         (int = 0; < years_employment; i++)          {             annualsalary_increaserate[i] = rhs.annualsalary_increaserate[i];         }     }    }  ostream& operator<<(ostream& out, const employee& emp) {     out << "---------------------------------\n";     out << "employment years: " << emp.years_employment << endl;     (int = 0; < emp.years_employment; i++)     {         int y = i+1;         double percent = emp.annualsalary_increaserate[i] * 100;         out << "year " << y << " annual salary increase: " << percent << endl;     }     out << endl;     out << "---------------------------------\n";      return out; }  void employee::output() const {    cout << "----------------------------------\n";    cout << "employment years: " << years_employment << endl;     (int = 0; < years_employment; i++)     {         int y = i+1;         double percent = annualsalary_increaserate[i] * 100;         cout << "year " << y << " annual salary increase: " << percent << endl;     }     cout << endl;    cout << "----------------------------------\n"; } 

this need with:

void employee::set_annualsalary_increaserate(double rate[], int yrs) {     (int = 0; < years_employment; i++)     {         double dec = rate[i]/100;         annualsalary_increaserate[i] = dec; //convert percentage decimal     } }  string employee::get_annualsalary_increaserate()const  {     stringstream temp;     string ansal_incrrate;     (int = 0; < years_employment; i++)     {         int y = i+1;         double percent = annualsalary_increaserate[i] * 100;  //convert percentage         temp << "year " << y << " annual salary increase: %" << percent << endl;     }     ansal_incrrate = temp.str();      return ansal_incrrate; } 

i have tested other functions , works fine, typed out parts count , parts involves dynamic array. though program compile, crashes when run it. running in debug mode, crashes on line:

annualsalary_increaserate[i] = dec; 

with error: program received signal sigegv, segmentation fault im not quite sure whats going on. i'm still new dynamic arrays , after googling bunch of sites i'm getting more confused. because need delete[] array? or else. thank in advance

for understanding of goes wrong here, important understand dynamic array is, , constant array is.

  • a dynamic array change it's size during it's lifetime. is, don't need declare specific size. increase it's size during it's lifetime.

  • a constant array array upon creation allocates space , therefore cannot expanded on size. of course keep separate variable indicating number of slots use, reducing used length of array. can never exceed it's maximum size.

you getting segmentation fault because exceeding length of array. if want avoid easiest use dynamic array. dynamic arrays come in couple of different flavours:

  • std::queue used buffer insert elements in front, , extract them @ back. in lot of cases need.

  • std::deque double ended queue. identical latter, diffence being extraction , insertion can both done @ each end.

  • std::vector need in particular case. insertion can done @ place, extraction can done @ place , size of course dynamic.

  • std::list same vector, better @ inserting elements in random places. use if have granular insertion pattern.

please take care if pass dynamic array argument, in cases it's required pass reference or pointer dynamic array.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -