Question
C++ Only Description: Develop a program that mimics some of the functionalities of an ArrayList in Java. Your program should maintain a pointer array of
C++ Only
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:
1.) 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.
2.) 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.
3.) 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.
4.) clear(double *&arr, int &size)
Clears all elements of the array (arr) and updates the size (size) to be 0.
5.) 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.
6.) 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.
7.) init(double *arr, int size)
Populates the elements of the array (arr) with input from the user (or via file redirection).
8.) print(double *arr, int size)
Prints the elements of the array.
A header will be provided in which all function prototypes will be given. You may add to this file, but do not change the portion that is provided!
Additional Specifications:
Your program should not use any pre-existing classes such as string or vector classes!
NO GLOBAL VARIABLES!
Each function needs to be properly commented.
Make sure your program compiles and runs on one of the Linux machines.
Your program should consist of one source file Array.h. They must be named exactly as indicated.
Example Output:
Use these files:
Array.h
/* * Prototypes * * */ //Do not modify in between lines /**************************************************************/ void insert(int index, double num, double *&arr, int &size); void remove(int index, double *&arr, int &size); double get(int index, double *arr, int size); void clear(double *&arr, int &size); int find(double num, double *arr, int size); bool equals(double *arr1, int size1, double *arr2, int size2); void init(double *arr, int size); void print(double *arr, int size); /**************************************************************/
main0.cpp
#include
#include
#include "Array.h"
using namespace std;
int main (int argc, char **argv)
{
int size(0);
cout
cin >> size;
double *arr;
arr = new double[size];
init(arr,size);
cout
print(arr,size);
cout
insert(1, 9.3, arr, size);
cout
print(arr,size);
cout
remove(5, arr, size);
cout
print(arr,size);
cout
cout
cout
int ans = find(9.3,arr,size);
if(ans != -1) cout
else cout
double *arr2 = new double[size];
for(int i = 0; i
{
arr2[i] = arr[i];
}
print(arr2,size);
bool equal = equals(arr,size,arr2,size);
if(equal) cout
else cout
delete [] arr2;
delete [] arr;
return 0;
}
main1.cpp
#include
#include
#include "Array.h"
using namespace std;
int main (int argc, char **argv)
{
int size(0);
cout
cin >> size;
double *arr;
arr = new double[size];
init(arr,size);
cout
print(arr,size);
cout
insert(10, 1, arr, size);
insert(1, 9.3, arr, size);
cout
print(arr,size);
cout
remove(10, arr, size);
remove(5, arr, size);
cout
print(arr,size);
cout
cout
cout
int ans = find(9.3,arr,size);
if(ans != -1) cout
else cout
int ans2 = find(999,arr,size);
if(ans2 != -1) cout
else cout
double *arr2 = new double[size];
for(int i = 0; i
{
arr2[i] = 0;
}
print(arr2,size);
bool equal = equals(arr,size,arr2,size);
if(equal) cout
else cout
delete [] arr2;
delete [] arr;
return 0;
}
casey@vanderwaal:-/Dropbox/Teaching/CSCE240Fal12017/Projects/Assignment3s ./a.out Please enter the size of your array: 5 Please enter 5 elements to populate the array. 1 2 3 45 The original array: 1 2 3 45 Size: 5 Index is not within the bounds of n+1. The new array: 1 9.3 2 3 45 Size: 6 Index is not within the bounds of n. The new array: 1 9.3 2 3 4 Size: 5 The element at position 2 is: 2 The element at position 10 is: -1 9.3 was found at position: 1 999 was found at position: 1 The arr and arr2 are not equal. casey@vanderwaal:-/Dropbox/Teaching/CSCE240Fall2017/Projects/Assignment3sStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started