using C++, Design a class that has an array of floating-point numbers. The const
ID: 3800456 • Letter: U
Question
using C++,
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 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.
Create the header file named NumberArray.h.
Create the class implementation file named NumberArray.cpp.
Explanation / Answer
#include "NumClass.h" // For the NumClass class
#include <iostream>
using namespace std;
// Main to run program
int main()
{
//variables
int a_size = 10; // initialize
NumClass myArray(a_size); // name for instance of array
int element; // to pick an element from the array
int size;
size=a_size;
// Get the array size.
cout << "Enter the arrays's size: ";
cin >> size;
cout << endl;
myArray.theArray[size];
// ask for the numbers in the array
for (int index =0; index < size; index++)
{
cout << "What is the number that is in " << index << " place of the array? ";
cin >> myArray.theArray[index];
}
// store the in the instance of digits
myArray.theArray[size];
cout << "Pick an element in the array. ";
cin >> element;
// Display the Array's data.
cout << " Array Information ";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ";
cout << " Number from element " << element << " :" << myArray.theArray[element] << endl;
cout << " Highest : " << myArray.getHighest() << endl;
cout << " Lowest: " << myArray.getLowest() << endl;
cout << " Average: " << myArray.getAverage() << endl;
cout << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ";
cout << endl;
}
/*
NumClass.cpp
*/
// Implementation file for the NumClass class
# include "NumClass.h"
#include <cstdlib>
#include <iostream>
using namespace std;
// The default constructor
NumClass::NumClass(int size)
{
theArray = new int[size]; // points to a dynamically allocated array of integers
numElements = size; // number of elements in the array
for (int index = 0; index < size; index++)
theArray[index] = 0; // set everyting to zero to start
}
NumClass::~NumClass()
{
//destructor
delete[]theArray; //free memory
theArray = 0; //clear to prevent invalid memory address
}
// Accessor function for the highest number
// use const to tell complier not to change it
// only return value
double NumClass::getHighest() const
{
int count; // local to count in loop
double highest; // to hold highest
// set the first array element to highest
highest = theArray[0];
// step through array size to compare
for (count =1; count < numElements; count++)
{
if (theArray[count] > highest)
{
// stores the highest number
highest = theArray[count];
}
}
return highest;
}
// Accessor function for the lowest
// use const to tell complier not to change it
// only return value
double NumClass::getLowest() const
{
int count; // local to count in loop
double lowest; // to hold lowest
// set the first array element to lowest
lowest = theArray[0];
// step through array size to compare
for (count = 1; count < numElements; count++)
{
if (theArray[count] < lowest)
{
// stores the lowest number
lowest = theArray[count];
}
}
return lowest;
}
// Accessor function for the average
// use const to tell complier not to change it
// only return value
double NumClass::getAverage() const
{
double total = 0; // accumulator for function
int count; // local to count in loop
double average; // to hold average
// step through array size to add up numbers
for (count = 0; count < numElements; count++)
{
total =+ theArray[count];
}
average = (total/count);
return average;
}
/*
NumClass.h
*/
// Specification file for the NumClass class
#ifndef NumClass_H // include guard to keep from being run twice
#define NumClass_H // defines class
class NumClass // class name
{
public:
// private member variables
// Dynamically allocate size of array
int *theArray; // pointer to the array
int numElements; // number of elements
NumClass(int); // constructor
~NumClass(); // destructor
// Mutators used to alter variables
void setHighest(double); // set the highest value
void setLowest(double); // set the lowest value
void setAverage(double); // set the average value
// Accessors used to get the variables
//set as constant to avoid making changes to variable
//int getElement() const;
double getHighest() const;
double getLowest() const;
double getAverage() const;
};
#endif // closes guard
[/code]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.