Question

10. Number Array Class Design a class that has an array of floating-point numbers. The constructor...

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

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In C++ PART 1: Write a constructor for a class data type named Test that has...
In C++ PART 1: Write a constructor for a class data type named Test that has the following two member variables: double arr*; int size; The constructor should take one argument for the size and then dynamically allocate an array of that size. PART 2: Write a destructor for the Test class.
Write a function that passes an array argument, getRandomNumbers, to get a pointer to an array...
Write a function that passes an array argument, getRandomNumbers, to get a pointer to an array of random numbers in the array. The function dynamically allocates an array, uses the system clock to seed the random number generator, populates the array with random values, and then returns a pointer to the array. Function getRandomNumbers to generate a random array and return a pointer. int* getRandomNumbers(int num); // The parameter indicates the number of numbers requested. The algorithm can be described...
Java... Write a class named TestScores. The class constructor should accept an array of test scores...
Java... Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to...
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
Phone number lookup Design a program that has two parallel arrays: a string array named people...
Phone number lookup Design a program that has two parallel arrays: a string array named people that is initialized with the names of seven of your friends, and a string array named phoneNumbers that is initialized with your friends phone numbers. The program should allow the user to enter a persons name (or part of a persons name). it should then search for that person in the people array. If the person is found, it should get that persons phjone...
You need to write a permute class that will take first and second strings to rearrange...
You need to write a permute class that will take first and second strings to rearrange letters in first, followed by second. For example, if the first is “CAT” string and second is “MAN” string, then the program would print the strings TACMAN, ATCMAN, CTAMAN, TCAMAN, ACTMAN, and CATMAN. The first and second strings can be any length of string or a null. The permute class uses a Node class as link list node to link all letters arrangement. The...