Question

Your goal is to create an ‘Array’ class that is able to hold multiple integer values. The ‘Array’ class will be given functionality through the use of various overloaded operators
You will be given the main() function for your program and must add code in order to achieve the desired result. Do not change any code in main(). If something is not working, you must change your own code, not the code in main().

Assignment 5: overloading member functions. Overview: Your goal is to create an Array class that is able to hold multiple i

arri contains: @ 1234 arr2 contains: 0 1 2 3 4 5 6 7 8 9 arr3 contains: @ 1 2 3 4 Notice that you are able to compare two Arr

• Note that this list does not contain every operator and item you will need to add to your class; it only lists the ones tha

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the completed code for the question. Please do rate the answer if it helped. Let me know if any issues.

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

class Array
{
private:
static int numberOfElements; //class static variable
int size;
int* values;
  
public:
Array(int len)
{
size = len;
values = new int[size];
for(int i = 0; i < size; i++)
values[i] = 0;
numberOfElements += size;
srand(time(NULL));
}
  
Array(const Array& other) //copy constructor
{
size = other.size;
values = new int[size];
for(int i = 0; i < size; i++)
values[i] = other.values[i];
numberOfElements += size;
}
  
  
Array& operator =(const Array & other)
{
delete []values;
numberOfElements = numberOfElements - size + other.size;

size = other.size;
values = new int[size];
for(int i = 0; i < size; i++)
values[i] = other.values[i];
return *this;
}
  
int& operator[](int index)
{
return values[index];
}
  
friend ostream& operator <<(ostream & out, const Array &a)
{
for(int i = 0; i < a.size; i++)
out << a.values[i] << " ";
out << endl;
return out;
}
  
int getSize()
{
return size;
}
  
bool operator ==(const Array &other)
{
if(size == other.size)
{
  
for(int i = 0; i < size; i++)
{
if(values[i] != other.values[i])
return false;
}
return true;

}
else
return false;
}
  
bool operator <(const Array &other)
{
int index;
  
for(index = 0 ; index < size && index < other.size; index++)
{
if(values[index] >= other.values[index])
return false;
}
  
if(index == size) //all elements in this array are lesser than other
return true;
else
return false;
  
}
  
Array& operator +=(const Array& other) //concatenate
{
int newSize = size + other.size;
  
int* temp = new int[newSize];
int j = 0;
for(int i = 0; i < size; i++) //copy from this array
temp[j++] = values[i];
for(int i = 0; i < other.size; i++) //copy from other array
temp[j++] = other.values[i];
  
numberOfElements = numberOfElements - size + newSize; //update class variable
size = newSize;
delete[] values;
values = temp;
return *this;
}
  
int operator *() //indirection operator
{
int min = values[0];
for(int i = 1; i < size; i++)
{
if(values[i] < min)
min = values[i];
}
return min;
}
  
Array& operator !() //shuffle
{
for(int i = 0; i < size; i++)
{
int index = rand() % size;
//swap
int temp = values[i];
values[i] = values[index];
values[index] = temp;
}
return *this;
}
  
Array operator ++(int) //postfix
{
Array copy(*this);
for(int i = 0; i < size; i++)
values[i]++;
return copy;
}
  
Array& operator --() //prefix
{
for(int i = 0; i < size; i++)
--values[i];
return *this;
}
  
  
static int getNumberOfElements()
{
return numberOfElements;
}
  
~Array()
{
delete[] values;
numberOfElements -= size;
  
}
};

int Array::numberOfElements = 0; //initialize static variable


int main() {
  
Array arr1(5), arr2(10);
  
for (int i = 0; i < arr1.getSize(); i++) arr1[i] = i;
  
for (int i = 0; i < arr2.getSize(); i++) arr2[i] = i;
  
cout << "arr1 contains: " << arr1; cout << "arr2 contains: " << arr2;
  
Array arr3(arr1);
  
cout << "arr3 contains: " << arr3 << endl;
  
arr2 = arr2;
  
cout << "arr2 contains: " << arr2;
  
arr3 = arr2;
  
cout << "arr3 contains: " << arr3 << endl;
  
cout << boolalpha; //Display booleans as 'true' or 'false' instead of 1 or 0
  
cout << "arr2 == arr3: " << (arr2 == arr3) << endl;
  
cout << "arr1 == arr3: " << (arr1 == arr3) << endl;
  
cout << "arr1 < arr3: " << (arr1 < arr3) << endl << endl;
  
arr3[0] = 100;
  
cout << "New arr3: " << arr3; cout << "arr2 == arr3: " << (arr2 == arr3) << endl;
  
cout << "arr1 == arr3: " << (arr1 == arr3) << endl;
  
cout << "arr1 < arr3: " << (arr1 < arr3) << endl << endl;
  
arr1 += arr2;
  
cout << "arr1 += arr2: " << arr1 << endl;
  
cout << "!arr1: " << !arr1;
  
cout << "*arr1: " << *arr1 << endl << endl;
  
cout << "arr1++: " << arr1++;
  
cout << "arr1 is: " << arr1 << endl; cout << "--arr1: " << --arr1;
  
cout << "arr1 is: " << arr1 << endl;
  
cout << "Total number of elements in all arrays: " << Array::getNumberOfElements() << endl << endl;
  
return 0;
  
}


output
=------
arr1 contains: 0 1 2 3 4
arr2 contains: 0 1 2 3 4 5 6 7 8 9
arr3 contains: 0 1 2 3 4

arr2 contains: 0 1 2 3 4 5 6 7 8 9
arr3 contains: 0 1 2 3 4 5 6 7 8 9

arr2 == arr3: true
arr1 == arr3: false
arr1 < arr3: false

New arr3: 100 1 2 3 4 5 6 7 8 9
arr2 == arr3: false
arr1 == arr3: false
arr1 < arr3: false

arr1 += arr2: 0 1 2 3 4 0 1 2 3 4 5 6 7 8 9

!arr1: 2 4 8 1 7 4 0 3 1 6 0 5 2 9 3
*arr1: 0

arr1++: 2 4 8 1 7 4 0 3 1 6 0 5 2 9 3
arr1 is: 3 5 9 2 8 5 1 4 2 7 1 6 3 10 4

--arr1: 2 4 8 1 7 4 0 3 1 6 0 5 2 9 3
arr1 is: 2 4 8 1 7 4 0 3 1 6 0 5 2 9 3

Total number of elements in all arrays: 35

Add a comment
Know the answer?
Add Answer to:
Your goal is to create an ‘Array’ class that is able to hold multiple integer values....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Redesign your Array class from lab6 as a class template to work with the application below....

    Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below. #include "Array.h" main() {   Array<char> c(3);   c.setValue(0,'c');   c.setValue(1,'s');   c.setValue(2,'c');   cout << c;   Array<int> i(3);   i.setValue(0,1);   i.setValue(1,2);   i.setValue(2,5);   cout << i;   Array<int> j(3);   j.setValue(0,10);   j.setValue(1,20);   j.setValue(2,50);   cout << j;   Array<int> ij;   ij = i + j;   cout << ij;...

  • You are making a .h file. Implement a recursive binary search function. bSearch passes in an...

    You are making a .h file. Implement a recursive binary search function. bSearch passes in an array of integers, the size of the array, and the value the function is searching for. The function should return the index where found or -1 if not found. You will want to implement a recursive helper function that passes in the array, the value to be located, and the beginning and ending of the range of values to search within. Use the provided...

  • Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify...

    Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • Transform the find function of Question 4 into a function template. Here is the program used...

    Transform the find function of Question 4 into a function template. Here is the program used to test your template, followed by the output of that program: #include <iostream> #include <string> #include "find.h" using namespace std; #define NUM_ELEMENTS(a) (sizeof(a) / sizeof(a[0])) int main() {         cout << "int" << endl;         cout << "---" << endl;         int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};         cout << "3 is at location " << find(arr1, NUM_ELEMENTS(arr1),...

  • Must be written in C 89 Mode Array Insertion Your task is to complete the implementation...

    Must be written in C 89 Mode Array Insertion Your task is to complete the implementation of the insertion function, insert_into_array: int* insert_into_array(int arr[], size_t arr_len, int value, size_t pos); The insertion function inserts a value into an array at a specified position. All elements to the right of the inserted element are shifted over a position (upon inserting, all elements at the insertion position are shifted up an index, and the last element in the array is overwritten by...

  • In C++ Write a function that accepts two int arrays of the same size. The first...

    In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. The function should find all numbers in the array that are greater or equal to the average and fill out the second array with these numbers. You need to design the function. I tried this code but the errors returned were: expression must have a constant value #include<iostream> using...

  • Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft ...

    Variable Size Array with Classes, Testing. Study Code and Object Definition Windows of Microsoft Visual Studio described here. As you work on the below project, demonstrate to the instructor the usage of this feature. Create a project titled Lab11_VarArrayTest. Implement the dynamically expanding and contracting array of doubles described in the previous lab as a class. You should use this class definition. The class attributes are a pointer to the dynamically allocated array dAarray and the array size size This...

  • Problem: Zipper For this problem, you have some starter code. Save it and run it a...

    Problem: Zipper For this problem, you have some starter code. Save it and run it a few times -- you will see that it generates two arrays of equal lengths populated with random values in range [0,9]. Your task is to complete the zip() method, which takes two equal length arrays and "zips" them. See sample runs below and comments in the starter code for further description of what that means. Input Validation: • None - there is no user...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT