10. Number Array Class Design a class that has an array of floating-point number
ID: 3765323 • Letter: 1
Question
10. Number Array Class
Design a class that has an array of floating-point numbers. The constructor should accept an integer
argument and dynamically allocate the array to hold that many numbers. The private data members of
the class should include the integer argument in a variable to hold the size of the array and a
pointer to float type to hold the address of the first element in the array .
The destructor should free the memory held by the array .
In addition, there should be member functions to perform the following operations :
• Store a number in any element of the array
• Retrieve a number from any element of the array
• Return the highest value stored in the array
• Return the lowest value stored in the array
• Return the average of all the numbers stored in the array
Explanation / Answer
#include "NmClass.h" // including NmClass class
#include <iostream>
using namespace std;
int main()
{
NmClass * Array; //creating a pointer
int element;
int size;
cout << "Enter the size of array: ";
cin >> size;
cout << endl;
Array = new NmClass(size); //call the class constructor initializing a new array
for (int ind =0; ind < size; ind++)
{
cout << "which is the number that is in the " << ind << " place of the array? ";
cin >> Array->NArray[index]; //assign the values to the array indexes
}
// storing the array
cout << "select an element in the array. ";
cin >> element;
// Display the Array's data.
cout << " Array : ";
cout << " Number from element " << element << " :" << Array->NArray[element] << endl;
cout << " Highest : " << Array->Highest() << endl;
cout << " Lowest: " << Array->Lowest() << endl;
cout << " Average: " << Array->Avg() << endl;
cout << endl;
delete Array;
}
// Implementation of the NmClass class
# include "NmClass.h"
#include <cstdlib>
#include <iostream>
using namespace std;
// The default constructor
NmClass::NmClass(int size)
{
NArray = new int[size]; // dynamically allocated array
numElements = size;
for (int ind = 0; ind< size; ind++)
NArray[ind] = 0;
}
NmClass::~NmClass()
{
delete[]NArray; //destructor
NArray = 0}
// function for the highest number
double NmClass::Highest() const
{ int count;
double highest;
highest = NArray[0]; // set the array element at the highest
for (count =1; count < numElements; count++)
{
if (NArray[count] > highest)
{
highest = NArray[count];
}
}
return highest;
}
// Accessor function for the lowest
double NmClass::Lowest() const
{
int count;
double lowest;
lowest = NArray[0]; // set the array element at the lowest
for (count = 1; count < numElements; count++)
{
if (NArray[count] < lowest)
{
lowest = NArray[count];
}
}
return lowest;
}
// function for the average
double NmClass::Avg() const
{
double total = 0;
int count;
double avg;
for (count = 0; count < numElements; count++)
{
total += NArray[count];
}
avg = (total/count);
return avg;
}
// for the NmClass class
#ifndef NmClass_H // include guard to keep from being run twice
#define NmClass_H // defines class
class NmClass // class name
{
public:
int *NArray; // pointer
int numElements;
NmClass(int); // constructor
~NmClass(); // destructor
void Highest(double); // for the highest value
void Lowest(double); // for the lowest value
void Avg(double); // for the average value
double Highest() const;
double Lowest() const;
double Avg() const;
};
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.