Starting from:

$25

CSCI1730 - Breakout - Lab 03 - Solved

Arrays and Pointers

1           Array Practices
You should write a C++ program consisting of the following files:

•    arrayutil.h: This is a header file that includes the following function signatures:

int getMax (int arr[], int length); double getMean (int arr[], int length); double getVar (int arr[], int length); void sortArray (int arr[], int length);

It is also recommended that you have include guards in your header file.

•    arrayutil.cpp: This is where the actual implementations of the array utility functions reside.

–    getMax should return the maximum value of the given array parameter arr whose length is given by the parameter length.

–    getMean should return the mean/average of the given array. Note that the return type is double, so be careful not to do integer division.

–    getVar should return the population variance of the given array. Given a set of numbers x1,x2,...,xn, the population variance may be calculated by . Again, watch out that you don’t accidentally do integer division.

–    sortArray should sort the given array in ascending order. The sorting should be done in-place. You may choose to use any sorting algorithm without considering its time complexity.

•    main.cpp: This is source file containing the main function, in which you should test the array utility functions you wrote. You should provide at least 3 test cases for each of the array utility functions. For each test case, please print the contents of the array before invoking a function, and then print the output after invoking the function.

•    makefile: You need to compile and link your program separately. The first command in your makefile must perform the linking step which generates the executable. You should also have commands that compile the cpp files into object files (those with .o extension). Please also define a “run” command that executes your program and a “clean” command that removes all the object files and the executable.

As a simple example, suppose an array contains the values {2, 4, 1, 3}, then the max is 4, mean is 2.5, population variance is 1.25, and the sorted array, obviously, should be {1, 2, 3, 4}.

2           Pointer Practices
For this section, please record your answers to the following questions in the README file.

1.    What are the sizes of the following C++ types on odin? Please include your units. Hint: You can use the sizeof  operator and print out the sizes!

(a)                    char

(b)                    char *

(c)                     short

1

(d)                    short *

(e)                    int

(f)                      int *

(g)                    long

(h)                    long *

(i)                      int **

(j)                      int ***

2.    Run the program below three (3) times. For each run, what is the output of the marked lines below?

int i = 5; int * ip = &i; cout << ip     << endl; // (a) cout << (ip + 1) << endl; // (b) cout << (ip + 2) << endl; // (c) cout << (ip + 3) << endl; // (d) cout << (ip + 4) << endl; // (e)

(a)                    (b) (c) (d)

(e)

(f) Is the output for each run different? If so, explain why.

More products