Question

In: Computer Science

c++ You will implement a template class with the following members: An array of our generic...

c++

You will implement a template class with the following members:

  1. An array of our generic type. The size of this array should be determined by an integer argument to the constructor.
  2. An integer for storing the array size.
  3. A constructor capable of accepting one integer argument. The constructor should initialize the array and set the array size.
  4. A method, find Max, that returns the maximum value in the array.
  5. A method, find Min, that returns the minimum value in the array.

Additionally you will need to create a short driver program that demonstrates your template class working with a few different numeric data types.

Solutions

Expert Solution

#include <iostream>

using namespace std;

template<class T>
class Array {
private:
    T *arr;
    int size;
public:
    Array(int size) : size(size) {
        arr = new int[size];
    }

    Array(T *arr, int size) : arr(arr), size(size) {

    }

    T max() {
        T result = arr[0];
        for (int i = 0; i < size; ++i) {
            if (arr[i] > result) {
                result = arr[i];
            }
        }
        return result;
    }

    T min() {
        T result = arr[0];
        for (int i = 0; i < size; ++i) {
            if (arr[i] < result) {
                result = arr[i];
            }
        }
        return result;
    }
};

int main() {
    double d[] = {4.5, 2.9, 9.0, 1.2, 6.5};
    Array<double> doubles(d, 5);
    cout << "Maximum is " << doubles.max() << endl;
    cout << "Minimum is " << doubles.min() << endl;

    int a[] = {2, 0, 1, 6, 9, 5};
    Array<int> integers(a, 6);
    cout << "Maximum is " << integers.max() << endl;
    cout << "Minimum is " << integers.min() << endl;
    return 0;
}


Related Solutions

Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
you are asked to implement a C++ class to model a sorted array of unsigned integers....
you are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Class will provide (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be used...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
You'll implement a completely generic version of an algorithm to find the maximum of an array....
You'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects that implement Comparable and return the maximum. If the array is...
1) Submit your completed dynamic array with template/generic data type (DArray.java) implementation. 2) Complete the template...
1) Submit your completed dynamic array with template/generic data type (DArray.java) implementation. 2) Complete the template DArray (if you have not done so) and this attached Iterator class. Make sure to document and test your code thoroughly. import java.util.Arrays; public class DArray { private final double GROW_FACTOR = 0.5;// array size growing rate //attributes private int size; private int buffer[]; //the actual array //constructors public DArray() { this.size=10; } public DArray(int size) throws Exception { if(size < 0) { throw...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT