Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP ME! Encapsulate an array of doubles in a class FloatArray. Make a .h file t

ID: 3765455 • Letter: H

Question

HELP ME!

Encapsulate an array of doubles in a class FloatArray. Make a .h file that has all the code for FloatArray. The idea is that arrays are "evil" and need to be wrapped in some type of container that makes them behave better with respect to C++ ideals. So have a dynamically allocated representation that can be resized smaller or larger, has a [] operator for accessing elements, and a size() method so a floatarray knows it size. Also make the thing a concrete data type, that is having appropriate constructor, destructor, copy constructor and assignment operator. Also write a resize method for your FloatArray, that will resize the thing to a new given size. Have it be able to go up or down, preserving ail of the old array elements that it can. Obviously if the array is size 10 and you downsize it to 5, you can only preserve 5 of the original values. Then write a main program as follows (say, testarray.cpp). This should have a function meanstd() that takes two parameters, a FloatArray and a double by reference, and returns its double average and also returns the standard deviation in the 2nd parameter. Then in main, do the following:

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <Math.h>

using namespace std;
double meanstd(double *F,double &SDev)
{
   double mean=0.0;
   int i;
   for(i=0;i<sizeof(F);i++)
   {
       mean+=*(F+i);
   }
   mean/=sizeof(F);
   SDev=0.0;
   for(i=0;i<sizeof(F);i++)
   {
       SDev+=pow((*(F+i)-mean),2);
   }
   SDev/=sizeof(F);
   SDev=sqrt(SDev);
   return mean;
}

void print(double *p, int size)
{
   for(int i=0;i<size;i++)
   {
       cout<<" "<<*(p+i);
   }
}
int main()
{
   int n,i;
   cout<<" Enter the number of Elements:";
   cin>>n;
   double FloatArray[n];
   for(i=0;i<n;i++)
   {
       FloatArray[i] = double(rand())/RAND_MAX;
   }
   cout<<" Original Array";
   print(FloatArray,n);
   double stdDev,mean;
   mean=meanstd(FloatArray,stdDev);
   cout<<" Mean:"<<mean;
   cout<<" Standard deviation:"<<stdDev;

   cout<<" Upsized Array";
   //arr = (int*) realloc(arr,(sizeof(int)*(count + 1)));
   double *p= new double[n+10];
   for(i=0;i<n;i++)
   {
       *(p+i) = FloatArray[i];
   }
   for(i=n;i<n+10;i++)
   {
       *(p+i)= double(rand())/RAND_MAX;
   }
   print(p,n+10);
   n=n+10; // incremented size
   cout<<" Downsizing Array";
   double *p1= new double[n-5];
   for(i=0;i<n-5;i++)
   {
       *(p1+i) = FloatArray[i];
   }
   print(p1,n-5);
   return 0;
}