I have a problem with the execution of this program. I do not know why it ends up running so abruptly ("The program stopped working"). The results of it are what I hope, however that happens. And I tried many things to avoid it and found that what causes the error is the function void IntArr::addElement(int qtty,int *vec);
because if I remove it, the program ends perfectly and without any error. Analyze the function and for me it's fine, I do not know what is slipping from my hands.
The function that what must do is to pass an array with the amount of elements that I want from it and add them to another array.
PS: the original program was well divided by files (class.h, class.cpp, main.cpp), nothing more than to copy them here I had to paste everything together.
I would really appreciate your help. Greetings!
#include <iostream>
using std::cout;
using std::endl;
#define PRESS_KEY std::cout<<"\nPresione Enter para continuar . . .\n";std::cin.get();
class IntArr{
private:
int * p;
int size;
int used;
//Verificador
void redimensionador(int cant);
public:
//Constructores
IntArr (int sz);
IntArr (int sz,int qtty,int *vec);
//Destructor
~IntArr();
//Visualizadores
void prtArr (void) const;
void prtArr (int cant);
//Accesores
int getSize(){return size;};
int getUsed(){return used;};
//Operaciones
void addElement(int xx);
void addElement(int qtty,int *vec);
};
//Constructores
IntArr::IntArr(int sz){
size = sz;
used = 0;
p = new int[size];
}
IntArr::IntArr(int sz,int qtty,int* vec){
if(qtty>sz){
sz = qtty;
}
size = sz;
used = qtty;
p = new int[size];
p = vec;
}
//Destructor
IntArr::~IntArr(){
delete []p;
}
//Visualizadores
void IntArr::prtArr(void) const{
if(used == 0){
cout<<endl<<"El array no tiene elementos."<<endl;
}
else{
cout<<endl<<"Array: ";
for(int i=0;i<used;i++){
cout<<p[i]<<", ";
}
cout<<endl;
}
}
void IntArr::prtArr(int cant){
if(used == 0){
cout<<endl<<"El array no tiene elementos."<<endl;
}
else{
cout<<endl<<"Array: ";
for(int i=0;i<cant;i++){
cout<<p[i]<<", ";
}
cout<<endl;
}
}
//Operaciones
double IntArr::getAvg(){
double acum = 0;
for(int i=0;i<used;i++){
acum += p[i];
}
return (acum/used);
}
void IntArr::addElement(int xx){
redimensionador(1);
p[used] = xx;
used++;
}
void IntArr::addElement(int qtty,int *vec){
int j=0;
redimensionador(qtty);
for(int i=used;i<(used+qtty);i++){
p[i] = vec[j];
j++;
}
used += qtty;
}
//Verificador
void IntArr::redimensionador(int cant){
if(cant+used>size){
if(cant > 5){
size += cant;
}
else{
size += 5 + cant;
}
}
}
int main(int argc, char *argv[]){
int v_aux[]= {0,5,10,15,20,25,30,35,40};
IntArr A(10,sizeof(v_aux)/sizeof(int),v_aux);
cout<<" size:"<<A.getSize()<<endl<<" used:"<<A.getUsed()<<endl;
A.prtArr();
A.addElement(77);
cout<<" size:"<<A.getSize()<<endl<<" used:"<<A.getUsed()<<endl;
A.prtArr();
A.addElement(11);
cout<<" size:"<<A.getSize()<<endl<<" used:"<<A.getUsed()<<endl;
A.prtArr();
A.addElement(8,v_aux);
cout<<" size:"<<A.getSize()<<endl<<" used:"<<A.getUsed()<<endl;
A.prtArr();
PRESS_KEY;
}