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++ 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...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
The shellsort algorithm is described in detail in the Topic 4 lecture slides, as well as...
The shellsort algorithm is described in detail in the Topic 4 lecture slides, as well as Wikipedia (https://en.wikipedia.org/wiki/Shellsort). Write a function in C which implements the shell sort algorithm. The recommended approach is to create an array of sub arrays using dynamic memory allocation, sort those sub-arrays, and replace them in the array. This is achievable through the use of double pointers. The following may be helpful: https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/ Inputs: array -> a pointer to the integer array to be sorted...
Lottery The lottery game matches three different integer numbers between 1 and 10. Winning depends on...
Lottery The lottery game matches three different integer numbers between 1 and 10. Winning depends on how many matching numbers are provided by a player. The player provides three different integers between 1 and 10. If there is a match of all 3 numbers, the winning $ 1000. If there is a match with 2 numbers, the winning $ 10. If there is a match with 1 number, the winning $ 1. With no match, the winning is $0. Write...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
C++ ONLY -- PRACTICE ASSIGNMENT For our practice assignment we have to turn in 2 files...
C++ ONLY -- PRACTICE ASSIGNMENT For our practice assignment we have to turn in 2 files - Driver.cpp and StringQueue.h Driver.cpp is provided for us, but if there are any changes needed to be made to that file please let me know. Based on the instructions below, what should the code look like for StringQueue.h ? Create a class named StringQueue in a file named StringQueue.h. Create a QueueNode structure as a private member of the class. The node should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT