Question

C++ please, thank you! Design a class named ArrayClass that has an array of floating-point numbers....

C++ please, thank you!

Design a class named ArrayClass that has an array of floating-point numbers. The constructor should accept an integer argument and allocate the array on the heap to hold that many numbers. The default constructor should allocate an array that can hold one number.

When the user attempts to store items, you must make a copy constructor that correctly produces a new copy of an ArrayClass object from an old one.

If the user attempts to store more numbers than the array can currently hold, you should reallocate into a new array that is twice the old array's size.

The destructor should free the memory held by the array. In addition, there should be member functions to perform the following operations, declared in the following way:

  • Store a number at the end of the array
    void store(float value)
  • Retrieve a number from any element of the array
    float fetch(int index)
  • Return the highest value stored in the array
    float maxValue()
  • Return the lowest value stored in the array
    float minValue()
  • Return the average of all the numbers stored in the array
    float averageValue()

Demonstrate your class in a program. Your program must not leak memory -- failing to delete allocated memory will be considered a major error. Your program must also never access memory that is not currently allocated.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question. Below is the code you will be needing. 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.

Thank You !
===========================================================================

#include<iostream>
using namespace std;
class ArrayClass{
   private:
       float *p;
       int size;
       int numCount;
   public:
       ArrayClass(int =1);
       ~ArrayClass();
       ArrayClass(const ArrayClass&);
       void store(float);
       float fetch(int);
       float maxValue();
       float minValue();
       float averageValue();
};

       ArrayClass::ArrayClass(int size):size(size){
           p = new float[size];
       }
       ArrayClass::~ArrayClass(){
           delete [] p;
       }
       ArrayClass::ArrayClass(const ArrayClass& a){

           int size = a.size;
           p = new float[size];
           for(int i=0; i<a.numCount;i++){
               *(p+i) = *(a.p+i);
           }
           numCount=a.numCount;
       }
       void ArrayClass::store(float val){
           if(numCount<size) {
               *(p+numCount)=val;
               numCount++;
           }
       }
       float ArrayClass::fetch(int index){
           if(0<=index && index<numCount) return *p+numCount;
           else return 0;
       }
       float ArrayClass::maxValue(){
           int maxIndex = 0;
           for(int i=0; i<numCount;i++){
               if(*(p+i)>*(p+maxIndex)) maxIndex =i;
           }
           return *(p+maxIndex);
       }
       float ArrayClass::minValue(){
           int minIndex = 0;
           for(int i=0; i<numCount;i++){
               if(*(p+i)<*(p+minIndex)) minIndex =i;
           }
           return *(p+minIndex);
       }
       float ArrayClass::averageValue(){
           float total=0;
           for(int i=0; i<numCount;i++) total+= *(p+i);
           return total/numCount;
       }
      
      
      
      
       int main(){
          
           ArrayClass arr(10);
           for(int i=1;i<=10;i++) arr.store(i);
          
           cout<<"Max Value: "<<arr.maxValue()<<endl;
           cout<<"Min Value: "<<arr.minValue()<<endl;
           cout<<"Avg Value: "<<arr.averageValue()<<endl;
          
           ArrayClass dup = arr;
          
           cout<<"Max Value in duplicate: "<<dup.maxValue()<<endl;
           cout<<"Min Value in duplicate: "<<dup.minValue()<<endl;
           cout<<"Avg Value in duplicate: "<<dup.averageValue()<<endl;
          
       }

==================================================================

Add a comment
Know the answer?
Add Answer to:
C++ please, thank you! Design a class named ArrayClass that has an array of floating-point numbers....
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in...

    Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in DynamicArray .h and DynamicArray.cpp files, according to the following UML class diagram: DynamicArray - int arrySize; - int currentSize; int* arrayPtr; + DynamicArray(int size) // Explicit constructor, which you define- allocate space in dynamic memory for an integer array of the given size. + DynamicArray) // Explicit destructor, which you define-de allocate dynamic memory. + additem(int item): bool // Set the value of the...

  • In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following...

    In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following members: A pointer to hold a dynamically allocated array, of type int. A member variable to hold the size of the array. A default constructor, which will allocate an array of size 10 A parameterized constructor, which takes a size and use the size to allocate array. A copy constructor, which performs deep copy. A copy assignment operator, which performs deep copy and supports...

  • Goals: Write your own class. Instantiate an object of your class. Interact with the object's public...

    Goals: Write your own class. Instantiate an object of your class. Interact with the object's public interface. Requirements: 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 - accepts an integer...

  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

  • Write a C++ console program that defines a class named Course that utilizes a dynamically allocated...

    Write a C++ console program that defines a class named Course that utilizes a dynamically allocated array. Do not use the vector class for this assignment. The Course class should define private data members for the name of the course, the number of students in the course, an array of student names (string*), and the capacity of the course (the array may not be full of students). Use pointer notation when dealing with the array. A 2-arg constructor should initialize...

  • This program has an array of floating point numbers as a private data member of a...

    This program has an array of floating point numbers as a private data member of a class. The data file contains floating point temperatures which are read by a member function of the class and stored in the array. Exercise 1: Why does the member function printList have a const after its name but getList does not? Exercise 2: Fill in the code so that the program reads in the data values from the temperature file and prints them to...

  • Create and implement a C++ class, RandomGenerator, that: Allocates and stores an array of random floating...

    Create and implement a C++ class, RandomGenerator, that: Allocates and stores an array of random floating point numbers between 0 and 1. The size of the array is passed into the constructor. The class contains a next() function which returns a new random number from the array every time the next() function is called (without calling rand()). Deletes any memory allocated on the heap when the class is destroyed. Note: This trick pre-calculates random numbers and saves execution time by...

  • 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...

  • C++ design a class named Technician that contains private data members to store the following: -technician's...

    C++ design a class named Technician that contains private data members to store the following: -technician's name (a string) - number of service calls - total time of all service calls - average service call time the technician class should also have the following public member functions: - a constructor function that initializes all data members by obtaining their values from the users. with the exception of the average service call time which will be calculated as: total_time/number_of_call - a...

  • How do I format this into C++? This the prompt: Design a class named Password that...

    How do I format this into C++? This the prompt: Design a class named Password that stores a password in a c-string and has member functions to test if the password complies with certain requirements as follows: The password should be between 6 and 20 characters long. The password should contain at least one uppercase and at least one lowercase letter. The password should have at least one digit. The password should have at least one punctuation character. Define a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT