C Program help What the program does I need to write a class

C++ Program help:

What the program does: I need to write a class ArrayList that represents an array of integers. Initially it has a capacity of 1 and is “empty”.
At any time, it can be partially full, so it keeps track of its current occupied size, as well as its capacity
(true size).

I have written a code but i need help writing two more methods. Thank you.

1 a. I need to write a function “erase(int m)”. This function deletes the leftmost occurrence of the number ‘m’ from
the array by shifting everything to its right by one position to the left. Note: the size of the
occupied part of the array reduces by 1. If the array’s occupancy reduces to less than 50%, make
the array half as big similar to how you expanded it.


b. A function “string toString()” that returns a string that contains the contents of the array list in
order within “[ ]”.

This is my code so far with my main method to test if the code works

#include
#include
using namespace std;
// class arralist
class ArrayList
{
private:
int *array;
int capacity,asize;
public:
int size();
ArrayList();
int & operator [](unsigned int);
void push_back(int);
};
// 1.a: Default constructor used to unitialize the array with capacity=1
ArrayList::ArrayList()
{
asize=1;
capacity=1;
array=new int(capacity);
array[asize-1]=0;
}
// This function returns the actual size of array
int ArrayList::size()
{ return asize;}
// 1.b:The overloaded [] oprtaor return element at ith position in array
int & ArrayList::operator [](unsigned int i)
{
if(i>=asize)
{cout<<\"invalid reference \";
exit(0);
}
return *(array+i);
}
//1.c: This function push m at the end of the array
void ArrayList::push_back(int m)
{
// this condition checks if size is beyond capacity or not. if so double the capacity.
if(asize==capacity)
{
capacity=2*capacity;
int *temp=new int(capacity);
for(int i=0;i temp[i]=array[i];
int *array=new int(capacity);
for(int i=0;i array[i]=temp[i];
}
array[asize]=m;
asize=asize+1;
}

//---------------------------

//You can test above class by using main() below


int main()
{
ArrayList arr; // call ro default constructor

for (int i=1;i<=50;i++)
{
arr.push_back(i); // call to push_back()
}

cout << \"Should contain numbers 1..50, is \";

for (int i=1;i

{
cout<<\" \"< }
return 0;

}

Solution

#include <iostream>
#include <stdlib.h>
#include <sstream>
using namespace std;
// class arralist
class ArrayList
{
private:
   int *array;
   int capacity, asize;
public:
   int size();
   ArrayList();
   int & operator [](unsigned int);
   void push_back(int);
   void erase(int m);
   string toString();
};
// 1.a: Default constructor used to unitialize the array with capacity=1
ArrayList::ArrayList()
{
   asize = 0;
   capacity = 1;
   array = new int[capacity];
}
// This function returns the actual size of array
int ArrayList::size()
{
   return asize;
}
// 1.b:The overloaded [] oprtaor return element at ith position in array
int & ArrayList::operator [](unsigned int i)
{
   if(i >= asize)
   {
       cout << \"invalid reference \";
       exit(0);
   }
   return *(array + i);
}
//1.c: This function push m at the end of the array
void ArrayList::push_back(int m)
{
// this condition checks if size is beyond capacity or not. if so double the capacity.
   if(asize == capacity)
   {
       capacity = 2 * capacity;
       int *temp = new int[capacity];
       for(int i = 0; i < asize; i++)
           temp[i] = array[i];
       array = new int[capacity];
       for(int i = 0; i < asize; i++)
           array[i] = temp[i];
   }
   array[asize] = m;
   asize = asize + 1;
}
void ArrayList::erase(int m){
   int pos = -1;
   for(int i = 0; i < asize; i++){
       if(array[i] == m){
           pos = i;
           break;
       }
   }
   if(pos == -1){
       cout << \"Element not presen\ \";
   }
   else{
       for(int i = pos; i < asize - 1; i++){
           array[i] = array[i + 1];
       }
   }
   asize--;
   if(asize == capacity / 2)
   {
       capacity = capacity / 2;
       int *temp = new int[capacity];
       for(int i = 0; i < asize; i++)
           temp[i] = array[i];
       array = new int[capacity];
       for(int i = 0; i < asize; i++)
           array[i] = temp[i];
   }
}
string ArrayList::toString(){
   string str = \"[\";
   for(int i = 0; i < asize; i++){
       ostringstream oss;
       oss << array[i];
       str += oss.str();
       if(i != asize - 1){
           str += \", \";
       }
   }
   str += \"]\";
   return str;
}

//---------------------------
//You can test above class by using main() below

int main()
{
   ArrayList arr; // call ro default constructor
   for (int i = 1; i <= 50; i++)
   {
       arr.push_back(i); // call to push_back()
   }
   cout << \"Should contain numbers 1..50, is \";
   cout << arr.toString();
   cout << \"\ \";
   return 0;
}

C++ Program help: What the program does: I need to write a class ArrayList that represents an array of integers. Initially it has a capacity of 1 and is “empty”
C++ Program help: What the program does: I need to write a class ArrayList that represents an array of integers. Initially it has a capacity of 1 and is “empty”
C++ Program help: What the program does: I need to write a class ArrayList that represents an array of integers. Initially it has a capacity of 1 and is “empty”
C++ Program help: What the program does: I need to write a class ArrayList that represents an array of integers. Initially it has a capacity of 1 and is “empty”

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site