SlideShare a Scribd company logo
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
#include
#include
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
#include
#include
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
#include
#include
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<

More Related Content

PDF
helpInstructionsAdd the function max as an abstract function to .pdf
PDF
In C++Add the function min as an abstract function to the classar.pdf
PDF
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
DOCX
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
PDF
I really need help with my C++ assignment. The following is the info.pdf
PDF
template-typename T- class Array { public- ---------------------------.pdf
PDF
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
PDF
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
helpInstructionsAdd the function max as an abstract function to .pdf
In C++Add the function min as an abstract function to the classar.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
I really need help with my C++ assignment. The following is the info.pdf
template-typename T- class Array { public- ---------------------------.pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf

Similar to Write a class ArrayList that represents an array of integers. Init.pdf (20)

PPT
lec6.ppt
PDF
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
PPT
Engineering lecture ppt by venay magen
PDF
Header file for an array-based implementation of the ADT bag. @f.pdf
DOCX
C++ program Revising the Array-Based List ADT Given the data structure.docx
PDF
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
DOCX
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
DOCX
When we first examined the array based and node based implementations.docx
DOC
Oops lab manual2
PDF
Pleae help me with this C++ task to the required result by edit or f.pdf
PDF
Sec 06 Basics Data Structure & Array Implementation .pdf
PDF
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
PDF
Pleae help me with this C++ question, ill upvote thanks.Write the .pdf
PDF
C++ normal assignments by maharshi_jd.pdf
DOCX
I am trying to fill out a program where the method definitions will b.docx
PDF
Everything needs to be according to the instructions- thank you! SUPPO.pdf
PPT
Operator overloading Object Oriented Programming
PDF
How do I declare the following constructors in my .h file Below.pdf
PDF
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
PPTX
Class list data structure
lec6.ppt
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
Engineering lecture ppt by venay magen
Header file for an array-based implementation of the ADT bag. @f.pdf
C++ program Revising the Array-Based List ADT Given the data structure.docx
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
When we first examined the array based and node based implementations.docx
Oops lab manual2
Pleae help me with this C++ task to the required result by edit or f.pdf
Sec 06 Basics Data Structure & Array Implementation .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
Pleae help me with this C++ question, ill upvote thanks.Write the .pdf
C++ normal assignments by maharshi_jd.pdf
I am trying to fill out a program where the method definitions will b.docx
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Operator overloading Object Oriented Programming
How do I declare the following constructors in my .h file Below.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Class list data structure
Ad

More from footworld1 (20)

PDF
Describe 3 functions of the cellular membrane and how are these func.pdf
PDF
Discuss the three main types of intellectual capitalSolutionth.pdf
PDF
Describer in detail the innate immune response to both an intercellu.pdf
PDF
Describe the difference between tropic and non-tropic hormones.S.pdf
PDF
C++ ProgrammingGivenWrite a closest Pair FunctionDeliverable.pdf
PDF
Can someone explain to me what the learning curve equation (Tn=T1n^.pdf
PDF
Both influenza virus and (IFV), and Measles Virus (MV) are envel.pdf
PDF
All of the following are characteristic to fatigue fracture surfaces.pdf
PDF
A cancer biology topic Describte how ChIP (chromatin immunoprecipit.pdf
PDF
You get a call from a customer who reinstalled Windows on his comput.pdf
PDF
You are running the Windows installation setup for Windows 7 on a co.pdf
PDF
Write a program to decipher messages encoded using a prefix code, gi.pdf
PDF
With the biological species concept, the process of speciation is fre.pdf
PDF
what is the relationship between lnx and 1xSolutionThe relat.pdf
PDF
Which of these conclusions is supported by plant phylogenies (evolut.pdf
PDF
Who are some other individuals who might want to use the informa.pdf
PDF
Which account below is not a subdivision of owners equityAccumu.pdf
PDF
What is the nucleoid region of a prokaryotic cell a membrane bound .pdf
PDF
What is heartwood What is sapwood What is the most abundant tissu.pdf
PDF
Use arrays to store data for analysis. Use functions to perform the .pdf
Describe 3 functions of the cellular membrane and how are these func.pdf
Discuss the three main types of intellectual capitalSolutionth.pdf
Describer in detail the innate immune response to both an intercellu.pdf
Describe the difference between tropic and non-tropic hormones.S.pdf
C++ ProgrammingGivenWrite a closest Pair FunctionDeliverable.pdf
Can someone explain to me what the learning curve equation (Tn=T1n^.pdf
Both influenza virus and (IFV), and Measles Virus (MV) are envel.pdf
All of the following are characteristic to fatigue fracture surfaces.pdf
A cancer biology topic Describte how ChIP (chromatin immunoprecipit.pdf
You get a call from a customer who reinstalled Windows on his comput.pdf
You are running the Windows installation setup for Windows 7 on a co.pdf
Write a program to decipher messages encoded using a prefix code, gi.pdf
With the biological species concept, the process of speciation is fre.pdf
what is the relationship between lnx and 1xSolutionThe relat.pdf
Which of these conclusions is supported by plant phylogenies (evolut.pdf
Who are some other individuals who might want to use the informa.pdf
Which account below is not a subdivision of owners equityAccumu.pdf
What is the nucleoid region of a prokaryotic cell a membrane bound .pdf
What is heartwood What is sapwood What is the most abundant tissu.pdf
Use arrays to store data for analysis. Use functions to perform the .pdf
Ad

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Presentation on HIE in infants and its manifestations
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Institutional Correction lecture only . . .
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
GDM (1) (1).pptx small presentation for students
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
Anesthesia in Laparoscopic Surgery in India
Microbial disease of the cardiovascular and lymphatic systems
Presentation on HIE in infants and its manifestations
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
STATICS OF THE RIGID BODIES Hibbelers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
O5-L3 Freight Transport Ops (International) V1.pdf
RMMM.pdf make it easy to upload and study
Supply Chain Operations Speaking Notes -ICLT Program
VCE English Exam - Section C Student Revision Booklet
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Institutional Correction lecture only . . .
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf

Write a class ArrayList that represents an array of integers. Init.pdf

  • 1. 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 #include #include using namespace std; class ArrayList { private: int *array; int capacity, asize; public: int size(); ArrayList();
  • 2. 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++)
  • 3. 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]; } }
  • 4. 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
  • 5. #include #include #include 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) {
  • 6. 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];
  • 7. } } 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
  • 8. 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 #include #include 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); };
  • 9. // 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]; }
  • 10. 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];
  • 11. 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<