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

In this lab you will gain experience writing and using functions with pointers.

ID: 3546683 • Letter: I

Question

In this lab you will gain experience writing and using functions with pointers. To that end,

you'll modify three functions from the chapter on Arrays, converting them to use pointers

instead of arrays.

STATISTICAL FUNCTIONS WITH POINTERS

Create a source file called pointerVSarrays.cpp. In it, put three functions from chapter 7:

mean(), variance(), and std_dev(). Then create versions of those functions that use only

pointers and match the following function prototypes:

double meanPtr(const double *start, const double *end);

double std_devPtr(const double *start, const double *end);

double variancePtr(const double *start, const double *end)

where, in each case, the data to be processed is a list that begins at the item pointed to by

start and continues to (and includes) the item pointed to by end. The pointer versions of

these statistical functions should only call other pointer versions (e.g. std_devPtr() should

call variancePtr().)

For this assignment, the only operations you may use on the pointers are:

dereferencing

increment operator

subtracting pointers

assignment

comparison of pointers


Specifically, you may not use the form *(ptr + index), nor may you use square brackets. Also,

use no counting variables in your pointer-based functions. Hint: Taking the difference

between the start and end pointers is SOME measure of the number of items in the list.


Your source file, pointerVSarrays.cpp, shall contain both versions of the three statistical

functions. (Don't forget to document all six functions.) Use testPointerVSarrays.cpp, which

you can download from the assignment page, to test your implementation.

Explanation / Answer

I'm not sure what kind of formulas you are using but here is what I have, also wasn't sure if you wanted to use sample or population formulas, I used sample formulas and used the cmath library to get the square root of the sample variance for standard deviation. The pointer usage should be correct, best of luck adjusting it if needed.



double meanPtr(const double *start, const double *end)
{
       double total = 0;
       //find number of values
       int numOfVal = end - start + 1;
       //from start to end without adding end
       do{
                   total += *start;
                   start++;
       }while(start != end);
       //add last value(end) to total
       total += *end;
       //divide by number of values
       return total / numOfVal;
}

double variancePtr(const double *start, const double *end)
{
       double total = 0;
       double avg = meanPtr(start,end);
       int numOfVal = end - start + 1;
       //find sums of the squares of the value minus the average(mean)
       do{
              total += (*start - avg) * (*start - avg);
              start++;
       }while(start != end);
       //last value
       total += (*end - avg) * (*end - avg);
       //divide by number of values
       return total/(numOfVal-1);
}

double std_devPtr(const double *start, const double *end)
{
       return pow(variancePtr(start,end), 0.5);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote