C++ problem: Write a program to create a class IntegerArrayList, which provides
ID: 3762190 • Letter: C
Question
C++ problem:
Write a program to create a class IntegerArrayList, which provides all the functionality on the array. This class should provide the functionality to create and maintain a list of integers using array as the base data structure. The structure of the class should be as follows:
Member Variables:
intArray -> This is the array containing the list of integers.
capacity -> It holds the capacity of the array list.
count -> It keeps the count of the occupied locations in the array.
Constructors:
IntegerArrayList() -> Constructs an empty list of initial capacity of 10.
IntegerArrayList(int capacity) -> Constructs an empty list of initial capacity of capacity.
Functions:
int add(int item) -> adds the item to the next available location, returns the current count of the elements in the array list.
int remove(int location) -> removes the item located at location. Returns the removed item.
bool contains(int item) -> returns true if the list contains item, else returns false.
int get(int location) -> returns the item located at location. If the location is greater than count. Returns -1.
bool -> isFull() returns true if the list is full, i.e. if capacity== count.
bool isEmpty() -> returns true if the array list contains no element.
void printList() -> prints all the elements in the list.
bool set(int location, int item) -> replaces the item located at location with item. Returns true if the operation is successful, else returns false.
Explanation / Answer
#include <iostream>
using namespace std;
class IntegerArrayList
{
private:
int *arr;
int capacity;
int count;
public:
IntegerArrayList()
{
arr= new int[10];
}
IntegerArrayList(int cap)
{
arr = new int[cap];
capacity=cap;
count=0;
}
bool isFull()
{
return count==capacity;
}
int add(int num)
{
if(isFull())
{
cout<<"The array is already full"<<' ';
return count;
}
else
{
arr[count]=num;
count += 1;
return count;
}
}
//Assuming location starts from 1
int remove(int loc)
{
int i;
int ans = arr[loc-1];
for(i=loc-1;i<count-1;i++)
{
arr[i]=arr[i+1];
}
count--;
return ans;
}
bool isEmpty()
{
return count==0;
}
bool contains(int num)
{
int i;
for(i=0;i<count;i++)
{
if(arr[i]==num)
{
return 1;
}
}
return 0;
}
//Assuming location starts from 1
int get(int loc)
{
if(loc>count)
{
return -1;
}
else
{
return arr[loc-1];
}
}
void printList()
{
int i;
for(i=0;i<count;i++)
{
cout<<arr[i]<<" ";
}
cout<<' ';
}
//Assuming location starts from 1
bool set(int loc,int num)
{
if(loc>count)
{
return 0;
}
else
{
arr[loc-1] = num;
return 1;
}
}
};
int main() {
// your code goes here
IntegerArrayList a(10);
cout<<a.isEmpty()<<' ';
cout<<a.isFull()<<' ';
cout<<a.add(1)<<' ';
cout<<a.isEmpty()<<' ';
cout<<a.isFull()<<' ';
cout<<a.add(10)<<' ';
cout<<a.add(5)<<' ';
a.printList();
cout<<a.remove(1)<<' ';
a.printList();
cout<<a.contains(1)<<' ';
cout<<a.contains(5)<<' ';
cout<<a.get(1)<<' ';
cout<<a.set(1,7)<<' ';
a.printList();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.