Write a class ArrayList that represents an array of integers

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).
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).

However the output for the prime numbers 1 -50 is incorrect. Displaying numbers like 0, 625 for prime numbers.


This is my code with the main method below
#include <iostream>
#include <stdlib.h>
#include <sstream>
using namespace std;

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 present\ \";
}
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;
}


This is my code with the main method below
#include <iostream>
#include <stdlib.h>
#include <sstream>
using namespace std;

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 present\ \";
}
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;
}


search (Ctr . 23, 24, 25, 26, 27, 2a , a,. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 41, 44· 46. 44, 07, 40, 43, s 01 Should contain only 1, Should eantain only 1, i·[11 Prime umbers between 1 and 50 12, 3, 5. 7. 11, 13,

Solution

#include <iostream>
#include <stdlib.h>
#include <sstream>
using namespace std;

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();
void primes(int range);
};
// 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 present\ \";
}
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;
}
void ArrayList::primes(int range){
   for(int i = 2; i <= range; i++){
bool isPrimeNum = 0;
//check number prime or not
for(j = 2; j <= i/2; j++){
// check divisibility condition..
if(i % j == 0){
isPrimeNum = 1;
break;
}
}
  
if(isPrimeNum==0 && N!= 1)
cout<<i<<\" \";
}
}

//---------------------------
//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 << \"\ \";
cout<<\"Primes: \"<<arr.primes(50);
return 0;
}

 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 kee
 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 kee
 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 kee
 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 kee
 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 kee
 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 kee
 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 kee

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site