driver: #include \"MyArray.h\" #include using std::cout; using std::cin; using s
ID: 3662319 • Letter: D
Question
driver:
#include "MyArray.h"
#include
using std::cout;
using std::cin;
using std::endl;
int main (int argc, char const* argv[])
{
MyArray m(10);
double num;
cout << m.set( 0, 3.14 ) << endl;
cout << m.set( 1, 5.5 ) << endl;
cout << m.set( 2, 4.2 ) << endl;
cout << m.set( 22, 8.11 ) << endl;
cout << m.get( 1,num ) << ",";
cout << num << endl;
cout << m.getMax() << endl;
cout << m.getMin() << endl;
cout << m.getAverage() << endl;
m.print();
return 0;
}
MyArray -d:double -capacity int +MyArray( c: int) +-MyArray() +set(i: int, v: double): int +get( i: int, v: double&): const int +print): const void +getMin(): const double +getMax(): const double +getAverage): const double Using the above UML for your specification, implement a class that contains a dynamically allocated array Include the following methods as part of it's public interface . constructor specifies the size of the array using the parameter. The initializes the object by creating the array, setting the capacity using the parameter, and initializing the array's elements to 0 . destructor responsible for deallocating the array . set() is responsible for assigning value v to element i in the array. It returns -1 if there is an error ( array index out of bounds), 0 otherwise . get() is responsible for retrieving a value at the ith element and returning it through reference parameter v. It returns -1 if there is an error (array index out of bounds), 0 otherwise . print) prints the current contents of the array . getMin() returns the smallest value in the array . getMax() returns the largest value in the array . getAverage() returns the average of the values in the array Submit your class in it's own header file, MyArray.h. Do not write a program, just submit the class in it's own header file. Include the class declaration and implementation of all methods. I will test your class with my own test program that will create a MyArray object and test each method Hints: Use the attached driver program to help test your class. Your public interface must match the public interface of the UML diagram. Remember the D is required when deleting dynamically allocated arrays! Review the CS2370 textbook on classes.Explanation / Answer
MyArray.h
#pragma once
class MyArray
{
public:
MyArray();
MyArray(int i);
~MyArray();
int set(int i, double v);
int get(int i, double& v) const;
void print();
double getMin() const;
double getMax() const;
double getAverage() const;
private:
double* d;
int capacity;
};
MyArray.cpp
#include "stdafx.h"
#include "MyArray.h"
#include<iostream>
using namespace std;
MyArray::MyArray()
{
}
MyArray::MyArray(int c)
{
capacity = c;
d = new double[capacity];
for (int i = 0; i < capacity; i++)
{
d[i] = 0;
}
}
MyArray::~MyArray()
{
delete[] d;
}
int MyArray::set(int i, double v)
{
if (i < 0 || i >= capacity)
{
return -1;
}
d[i] = v;
return 0;
}
int MyArray::get(int i, double& v) const
{
if (i < 0 || i >= capacity)
{
return -1;
}
v = d[i];
return 0;
}
void MyArray::print()
{
for (int i = 0; i < capacity; i++)
{
cout << d[i] << endl;
}
}
double MyArray::getMin() const
{
double min = DBL_MAX;
for (int i = 0; i < capacity; ++i)
{
if (d[i] < min)
{
min = d[i];
}
}
return min;
}
double MyArray::getMax() const
{
double max = -DBL_MAX;
for (int i = 0; i < capacity; ++i)
{
if (d[i] > max)
{
max = d[i];
}
}
return max;
}
double MyArray::getAverage() const
{
double avg = 0,sum=0;
for (int i = 0; i < capacity; ++i)
{
sum += d[i];
}
avg = sum / capacity;
return avg;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.