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 Demonstrate the Class in a Program
Here is the completed code for this problem. Assuming the language is C++. Please don’t forget to mention the language while you post a question in future.
Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
#include<iostream>
using namespace std;
//NumberArray class
class NumberArray{
//instance variables
float *arr;
int size;
public:
//constructor taking capacity
NumberArray(int capacity){
//assigning capacity to size
size=capacity;
//initializing dynamic float array
arr=new float[size];
//initializing all elements to 0
for(int i=0;i<size;i++){
arr[i]=0;
}
}
//destructor to delete memory occupied by arr
~NumberArray(){
delete[] arr;
}
//method to set a value at a specific index
void set(int index, float value){
//assuming index is valid, assigning value at this index
arr[index]=value;
}
//method to fetch a value at a given index, assuming index is valid
float get(int index) const{
return arr[index];
}
//returns the lowest element in the array
float lowest() const{
float min=0;
//looping through elements
for(int i=0;i<size;i++){
//if this is first element or this number is less than min, updating min
if(i==0 || arr[i]<min){
min=arr[i];
}
}
return min;
}
//returns the highest element in the array
float highest() const{
float max=0;
for(int i=0;i<size;i++){
if(i==0 || arr[i]>max){
max=arr[i];
}
}
return max;
}
//returns the average of all numbers in the array
float average() const{
float sum=0;
//summing values
for(int i=0;i<size;i++){
sum+=arr[i];
}
//finding and returning average
float avg=(float) sum/size;
return avg;
}
};
//a simple main method for testing
int main(){
//creating a NumberArray of size 5
NumberArray array(5);
//assigning some values in each index
array.set(0,2);
array.set(1,2.5);
array.set(2,-8);
array.set(3,13.25);
array.set(4,10.2);
//displaying array elements using get method
cout<<"Array: ";
for(int i=0;i<5;i++){
cout<<array.get(i)<<" ";
}
cout<<endl;
//displaying highest, lowest and average values
cout<<"Highest: "<<array.highest()<<endl;
cout<<"Lowest: "<<array.lowest()<<endl;
cout<<"Average: "<<array.average()<<endl;
return 0;
}
/*OUTPUT*/
Array: 2 2.5 -8 13.25 10.2
Highest: 13.25
Lowest: -8
Average: 3.99
Get Answers For Free
Most questions answered within 1 hours.