C++ assignment

profileSam640
assg-01.pdf

Assg 01: Review of Functions and Arrays

COSC 2336 Data Structures

Objectives

• Practice using <cmath> predefined functions

• Review and practice writing user defined functions

• Review of arrays in C

• Practice passing arrays into functions

Description

In this programming assignment you will write a program that will take an array of integers, and calculate the mean (average) and standard deviation of the numbers. If the numbers in the array are

int n = 5; int x[] = {5, 3, 8, 2, 1};

Then to calculate the mean, we use the formula:

x̄ = 1

n

n−1∑ i=0

xi

where x̄ represents the calculated mean, and n is the total number of values in the array x. In other words, the mean is simply the sum of the values divided by the total number of values (n), or

x̄ = x0 + x1 + x2 + x3 + x4

n =

5 + 3 + 8 + 2 + 1

5 = 3.8

Likewise, the formula for calculating the standard deviation of a set of values is given by:

1

s =

√√√√ 1 n

n−1∑ i=0

(xi − x̄)2

Here you should notice that x̄ is the calculated mean of the values in the x array we just showed previously. In english, the standard deviation is shte sum of the square of the differences of each value from the mean. This sum of the squared differences is again divided by n the total number of values, then we take the square root of this whole calculation to get the final standard deviation.

So for our example x array, the standard deviation would be calculated as

s =

√ 1

n

[ (x0 − x̄)2 + (x1 − x̄)2 + (x2 − x̄)2 + (x3 − x̄)2 + (x4 − x̄)2

] =

√ 1

5

[ (5 − 3.8)2 + (3 − 3.8)2 + (8 − 3.8)2 + (2 − 3.8)2 + (1 − 3.8)2

] ≈ 2.4819

You will be given a starting template for this assignment. For this as- signment you are to perform the following tasks.

1. Implement a function named calculateMean(). This function takes two parameters, an integer indicating the size of the array of values (n), and an array of integers. This function will return a value of type double. The functions should calculate the mean for any array of any sized passed in to it, as described above.

2. Implement a second function named calculateStandardDeviation(). This function takes the same two parameters, the size of the array of values (n) and an array of integers. This function will also return a double result. The function will calculate and return the standard de- viation of any array of integers of any size passed to it. Some further requirements of this function. You must demonstrate the use of prede- fined functions from <cmath> such as the pow() and sqrt() functions. Also, you must call the calculateMean() function from this function, e.g. do not repeat the calculation of the mean value, but reuse your previous function to do this work to obtain x̄.

2

Here is the correct output you get from your program if you correctly implement the two functions and use the array given to you in the starting template for this assignment:

The calculated mean is: 5.6363636 The calculated standard deviation is: 2.6206428

The starting template you are given will compile. There are some lines of code commented out that call the functions you are supposed to write. You should uncomment those lines of code and write the functions so that you get exactly this given output from your program.

Assignment Submission

A MyLeoOnline submission folder has been created for this assignment. You should attach and upload your completed .cpp source file to the submission folder to complete this assignment.

Requirements and Grading Rubrics

Program Execution, Output and Functional Requirements

1. Your program must compile, run and produce some sort of output to be graded. 0 if not satisfied.

2. 33 pts. for correctly implementing the calculateMean() function.

3. 50 pts. for correctly implementing the calculateStandardDeviation() function.

4. 7 pts. for correctly demonstrating <cmath> functions as required.

5. 5 pts. for showing reuse of function to calculate mean.

6. 5 pts. for following class style guidelines.

Program Style

Your programs must conform ot the style and formatting guidelines given for this class. The following is a list of the guidelines that are required for the assignment to be submitted this week.

3

1. Most importantly, make sure you figure out how to set your indentation settings correctly. All programs must use 2 spaces for all indentation levels, and all indentation levels must be correctly indented. Also all tabs must be removed from files, and only 2 spaces used for indentation.

2. A function header must be present for all functions you define. You must give a short description of the function, and document all of the input parameters to the function, as well as the return value and data type of the function if it returns a value.

3. Do not include any statements (such as system("pause") or inputting a key from the user to continue) that are meant to keep the terminal from going away. Do not include any code that is specific to a single operating system, such as the system("pause") which is Microsoft Windows specific.

4