Specifications of the program C++ Answers ONLY! Your program should not use any
ID: 3889223 • Letter: S
Question
Specifications of the program
C++ Answers ONLY!
Your program should not use any pre-existing classes such as string or vector classes!
NO GLOBAL VARIABLES!
==================
Description:
Develop a program that mimics some of the functionalities of an ArrayList in Java. Your program should maintain a pointer array of doubles and be able to perform the following functions:
insert(int index, double num, double *&arr, int &size)
Adds an element (num) to the array (arr) at a given position (index) and updates the size.
You may allow the user to add to the immediate end of the array (at position n+1 for an array of n elements) but not past. You should print an error message if they try to print beyond these bounds.
remove(int index, double *&arr, int &size)
Removes an element from the array (arr) at a given position (index) and updates the size.
If index is out of the bounds of the array then an error message should be printed.
get(int index, double *arr, int size)
Returns the element at the given position (index).
Should check if the index given is outside the bounds of the array. If it is out of bounds an error message should be printed.
clear(double *&arr, int &size)
Clears all elements of the array (arr) and updates the size (size) to be 0.
find(double num, double *arr, int size)
Returns the first index in which a given element (num) is found in the array (arr). If not found -1 is returned.
equals(double *arr1, int size1, double *arr2, int size2)
Returns true if the contents of the two arrays are equal and false if they are not equal.
init(double *arr, int size)
Populates the elements of the array (arr) with input from the user (or via file redirection).
print(double *arr, int size)
Prints the elements of the array.
Explanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
void insert(int index, double num, double *&arr, int &size)
{
if(index>size || index<0)
{
cout<<"Index is out of bounds!"<<endl;
}
else
{
double next = arr[index];
double prev = num;
for(int i=index; i<size; i++)
{
arr[i] = prev;
prev = next;
next = arr[i+1];
}
arr[size] = prev;
size++;
}
}
void remove(int index, double *&arr, int &size)
{
if(index>=size || index<0)
{
cout<<"Index is out of bounds!"<<endl;
}
else
{
for(int i=index; i<size-1; i++)
{
arr[i] = arr[i+1];
}
size--;
}
}
double get(int index, double *arr, int size)
{
if(index>=size || index<0)
{
cout<<"Index is out of bounds!"<<endl;
}
else
{
return arr[index];
}
}
void clear(double *&arr, int &size)
{
for(int i=0; i<size; i++)
delete &arr[i];
size = 0;
}
int find(double num, double *arr, int size)
{
for(int i=0; i<size; i++)
if(arr[i] == num)
return i;
return -1;
}
bool equals(double *arr1, int size1, double *arr2, int size2)
{
if(size1 == size2)
{
for(int i=0; i<size1; i++)
if(arr1[i] != arr2[i])
return false;
return true;
}
else return false;
}
void init(double *arr, int size)
{
for(int i=0; i<size; i++)
{
cout<<" Enter data for index "<<i<<": ";
double num;
cin>>num;
arr[i] = num;
}
}
void print(double *arr, int size)
{
for(int i=0; i<size; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
int main() {
int size = 5;
double *arr = new double[size];
init(arr, size);
print(arr, size);
insert(0, 5, arr, size);
print(arr, size);
remove(2, arr, size);
print(arr, size);
cout<<"Index of 11 is "<<find(11, arr, size);
return 0;
}
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.