179k views
2 votes
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

User SMPLYJR
by
5.2k points

1 Answer

1 vote

Answer:

Here is the Array class named Number:

#include <iostream>

#include <iomanip>

using namespace std;

class Number {

private:

int size;

float *ptr;

float num;

public:

Number(){

cout<<"Enter size of array: ";

cin>>size;

ptr = new float[size];

cout<<"Enter elements"<<endl;

for(int i=0;i<size;i++) {

cin>>ptr[i]; } }

void getNumbers(){

cout << "{ ";

for (int i = 0; i < size; i++) {

cout <<ptr[i] <<setprecision(2)<< " "; }

cout << "}"; }

Number(int s){

ptr = new float[s];

size = s;

for (int i = 0; i < size; i++) {

cout << "Enter elements : ";

cin >> num;

ptr[i] = num; } }

~Number(){

delete [] ptr; }

void storeNumber(int input, float num){

while (input < 0 || input > size-1) {

cout << "array size exceeded! Enter an element again " << endl;

cin >> input;

if (input >= 0 && input < size) {

ptr[input] = num;

break; } }

if (input >= 0 && input < size) {

ptr[input] = num; } }

void retrieveNumber(int position){

while (position < 0 || position > size-1) {

cout << "array size exceeded! Enter an element again " << endl;

cin >> position;

if (position >= 0 && position < size) {

cout << "The number at "<<position<<"is: " << ptr[position];

break; } }

if (position >= 0 && position < size) {

cout << "The number at "<<position<<" is: " << ptr[position]; } }

float HighestNumber(){

float highest = ptr[0];

for (int i = 1; i < size; i++) {

if (ptr[i] > highest) {

highest = ptr[i]; } }

return highest; }

float LowestNumber(){

float lowest = ptr[0];

for (int i = 1; i < size; i++) {

if (ptr[i] < lowest) {

lowest = ptr[i]; } }

return lowest; }

float AverageNumber(){

float avg = 0.0;

for (int i = 0; i < size; i++) {

avg += ptr[i]; }

return avg/size; }

};

int main() {

Number array;

array.getNumbers();

cout << endl;

int pos;

float no;

cout << "Choose an element to replace: ";

cin >> pos;

cout << "What number do you want to replace element with?" << endl;

cin >> no;

array.storeNumber(pos, no);

array.getNumbers();

cout << endl;

int pos1;

cout << "Enter an element to retrieve the number: ";

cin >> pos1;

cout << endl;

array.retrieveNumber(pos1);

cout << endl;

cout << endl;

cout << "The highest number is: " << array.HighestNumber() << endl;

cout << "The lowest number is : " << array.LowestNumber() << endl;

cout << "Average of all numbers is : " << array.AverageNumber() << endl;

cout << endl;

return 0; }

Step-by-step explanation:

The program is well explained in the comments mentioned with each line of code in the attached document.

The screenshot of the program along with its output is attached.

Number Array Class Design a class that has an array of floating-point numbers. The-example-1
User Sebastian Nette
by
4.8k points