Question
#include using namespace std; class Array { private: float* numbers; int size; float maxValue, minValue, average; public: Array(int s); ~Array(); void store(int index, float value);
#include
class Array { private: float* numbers; int size; float maxValue, minValue, average;
public: Array(int s);
~Array();
void store(int index, float value);
float retrieve(int index); float getMax(); float getMin(); float getAverage();
friend istream& operator >> (istream& in, Array& a); friend ostream& operator << (ostream& out, Array& a); bool operator == (Array& a); bool operator != (Array& a); Array operator = (Array& a); float operator [] (int index); };
----
#include
using namespace std;
Array::Array(int s) { average = 0.0; minValue = 0.0; maxValue = 0.0; size = s; numbers = new float[size]; }
Array::~Array() { delete[] numbers; }
void Array::store(int index, float value) {
for (int i = 0; i < index; i++) { numbers[i] = value; } }
float Array::retrieve(int index) { return numbers[index]; }
float Array::getMax() { maxValue = numbers[0]; for (int i = 1; i < size; i++) { if (numbers[i] > maxValue) { maxValue = numbers[i]; } } return maxValue; }
float Array::getMin() { minValue = numbers[0]; for (int i = 1; i < size; i++) { if (numbers[i] < minValue) { minValue = numbers[i]; } } return minValue; }
float Array::getAverage() { float sum = 0; for (int i = 0; i < size; i++) { sum += numbers[i]; } return sum / size; }
istream& operator >> (istream& in, Array& a) { for (int i = 0; i < a.size; i++) { in >> a.numbers[i]; } return in; } ostream& operator << (ostream& out, Array& a) { for (int i = 0; i < a.size; i++) { out << a.numbers[i] << " "; } return out; } bool Array::operator == (Array& a) { if (this->size != a.size) return false; for (int i = 0; i < a.size; i++) { if (this->numbers[i] != a.numbers[i]) return false; } return true; } bool Array::operator != (Array& a) { if (this->size != a.size) return true; for (int i = 0; i < a.size; i++) { if (this->numbers[i] != a.numbers[i]) return true; } return false; } Array Array::operator = (Array& a) { if (this->size != a.size) { delete[] this->numbers; this->size = a.size; this->numbers = new float[this->size]; } for (int i = 0; i < a.size; i++) { this->numbers[i] = a.numbers[i]; } return *this; } float Array::operator [] (int index) { return this->numbers[index]; } ----
#include
#include "Number.h" int main() { Array a(5); cout << "Input numbers for the array\t"; cin >> a; cout << "Numbers in the Array: " << a << endl; a.store(2, 3.14); cout << "Retrieved number: " << a.retrieve(2) << endl; cout << "Maximum value: " << a.getMax() << endl; cout << "Minimum value: " << a.getMin() << endl; cout << "Average value: " << a.getAverage() << endl; Array b(5); cout << "Input numbers for the array\t"; cin >> b;
b.store(2, 3.14); cout << "Retrieved number: " << b.retrieve(2) << endl;
cout << "Maximum value: " << b.getMax() << endl;
cout << "Minimum value: " << b.getMin() << endl;
cout << "Average value: " << b.getAverage() << endl;
if (a == b) cout << "Arrays are equal" << endl; else cout << "Arrays are not equal" << endl; if (a != b) cout << "Arrays are not equal" << endl; else cout << "Arrays are equal" << endl; a = b; cout << "After assignment: " << a << endl; cout << "Value at index 2: " << a[2] << endl; return 0; }
Program crashes after inputting values for object b
C++
Step 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