First, the goal is to return a zero at the end.The following program tests whether the method you created works properly.Running this program does not return a zero at the end.This is probably a problem with append() or resize(), and the purpose of the append method is to include the value passed immediately after the last element.If adding an element causes the total number of elements to be greater than or equal to the size of the array, pass the resize method twice the size of the current array, create an array of that size, and place the value passed to the new array immediately after the last element.The resize method is intended to create an array of values that are passed.There is no error message after compiling.However, 0 is not returned.I think they've been working on something.If anyone understands, please take care of me.
ArrayInt.h
#ifndefLAB1B_ARRAYINT_H
#define LAB1B_ARRAYINT_H
# include <iostream>
# include <sstream>
using std::cout;
using std::endl;
using std::string;
classArrayInt{
private:
int*array;
intarraySize;
elements;
public:
ArrayInt();
ArrayInt(int size);
~ArrayInt();
int getSize();
void resize (int size);
void append (int value);
US>string listElements();
};
#endif//PRA_WEEK1_ARRAYINT_H
ArrayInt.cpp
#include "ArrayInt.h"
ArrayInt:: ArrayInt()
{
// create an array that size of 10
arraySize = 10;
array = new int [arraySize];
}
ArrayInt:: ArrayInt(int size)
{
// create an array that size of size
// if size <1
// size = 10;
arraySize = size;
if (size <1)
{
arraySize = 10;
}
array = new int [arraySize];
}
ArrayInt::~ArrayInt()
{
delete [ ] array;
}
int ArrayInt::getSize()
{
// return the current size of the array
return arraySize;
}
void ArrayInt::resize (int size)
{
// if size<getSize(if the new size is smaller than current size)
// do nothing (don't change anything)
if(size<=arraySize)
{
return;
}
// resize the array to the new size that is passed in
// create new array to store previous values init
int * newArray = new int [size];
// move the previous values to new array
for (inti=0; i<elements;i++)
{
newArray[i] = array[i];
}
// update the array size
arraySize = size;
// we don't need the old array anymore
delete [ ] array;
// array points to newArray so that the array gets new spaces
array = newArray;
}
void ArrayInt::append(int value)
{
static int counter = 0;
elements = counter;
// if the next location > = current size
if(elements>=arraySize)
{
// create a new array of size*2
arraySize = arraySize*2;
resize(arraySize);
}
// adding the value that is passed in into the next available space in the array
array [elements] = value;
// increasing the counter and elements after append value
counter++;
elements++;
cout<<"append method:"<value<<"is added to the array"<<endl;
}
US>string ArrayInt::listElements()
{
string numList;
std::stringstreams;
// return string that contains all numbers in the array numbers should be separated by comma
// if the array is empty
// returns the string "Empty Array"
if (elements<1)
{
return "Empty Array";
}
for (inti=0; i<elements;i++)
{
ss<<array[i]<<", ";
}
ss>>numList;
return numList;
}
main.cpp
#include<iostream>
# include "ArrayInt.h"
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
constint START=7;
constint UPDATE=12;
std::cout<<"Testing setSize and auto expansion on apps"<<std::endl;
std::cout<<"Also tests listElements"<<std::endl<<std::endl;
ArrayIntroom (START);
std::cout<<"Starting size should be"<<START<"and is"<room.getSize()<<std::endl;
room.resize (UPDATE);
std::cout<<"After resize, size should be"<<UPDATE<<"and is"<room.getSize()<<std:::endl;
std::cout<<std::endl<<"Now going to fill array and see if expansion"<<std::endl;
for (inti=0;i<UPDATE;i++)
{
room.append(2*i+1);
}
std::cout<<"Filled with 12 values, no problem"<<std::endl;
std::cout<<"Size should still be"<<UPDATE<"and is"<room.getSize()<<std::endl;
std::cout<<std::endl<<"Add one more"<<std::endl;
room.append(25);
std::cout<<"Size should now be"<2*UPDATE<"and is"<room.getSize()<<std::endl;
std::cout<<std::endl<<"Should show: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25"<std::endl;
std::cout<<"Array returns:"<room.listElements()<";
std::cout<<std::endl<<std::endl;
return 0;
}
As you can see by following the debugger, the causes are as follows:
//if the next location>=current size
if(elements>=arraySize)
{
// create a new array of size*2
arraySize = arraySize*2;
resize(arraySize);
}
arraySize=arraySize*2;
causes arraySize
to go from 12
to 24
.
Then, resize(24);
is called, but the conditional statement at the beginning of resize
is as follows:
if (size<=arraySize)
{
return;
}
The size
passed by the argument is 24
and arraySize
is 24
, so you meet the above conditions and return
without increasing the memory allocated to array
.
The rest is write to unassigned memory and buffer overflow.
© 2024 OneMinuteCode. All rights reserved.