Question
Lab 6 For this lab we are working on writing functions. This lab will have 2 parts. For full credit, you must turn in both
Lab 6
For this lab we are working on writing functions. This lab will have 2 parts. For full credit, you must turn in both parts.
Details
Part 1
For this part you will write 4 functions that adhere to a given signature. Normally I dont make you write more than the single function Ill connect to for testing but for this week, I need to make sure you can write functions so Im going to be testing them independently of the rest of the code in part 1.
The 4 functions are listed below and must have the same signatures:
double stdDev( int x1, int x2, int x3, int x4 ); double mean( int x1, int x2, int x3, int x4 ); double variance( int x1, int x2, int x3, int x4 ); double sumOfSquares( double d1, double d2, double d3, double d4 );
You must use the same spelling, return type, and parameter list. They must also be declared in the header file for this weeks lab. See the requirements below.
Then in the cpp file, you will implement each of these functions. You may notice that some of these functions can be used from the other functions. For example, to compute variance you need the mean you have to get a sum of squares. You should use this to your advantage so you arent reimplementing code that youve written functions for.
Heres what each of the functions should do.
The standard deviation is the square root of the variance.
The mean is the average of the 4 numbers that are given.
The variance is sum of the squares of the difference from the number and the mean and then divided by the count of the numbers, i.e. 4.
The sum of squares is the sum of the squares of the numbers.
So each of these functions will be given 4 numbers. You can use that fact in the places where you need to know how many numbers are given, e.g. mean and the variance.
So to think about this in terms of its components, the sum of squares is used by variance, one the difference from the input and the mean is determined. The variance also uses the mean to figure out the mean. Then the standard deviation uses the variance to return its answer. So each function is built using the simpler functions.
Testing Part 1 To test part 1, youll need a main function that will call the functions. I would simply cout the answer so you can quickly see if the answer is correct. You can prompt for the inputs or hard code them. Prompting would allow you to test more than once without having to recompile. Then something like this would work:
cout << mean( num1, num2, num3, num4) << endl;
would output the result from the mean function, assuming youve declared num1 - num4 and given them values somehow. You can do the rest with the other functions to make sure you are correct.
If you use the numbers, 1, 2, 3, and 4, these are the values for the functions:
mean: 2.5
sum of squares: 30
variance: 1.25
standard deviation: 1.11803
Once youve tested part 1 and have some confidence in your answers, submit your code to Web-CAT under lab 6 part 1. Once the tests pass there, go on to part 2.
Requirements
The following functions must all be declared in the header file lab6.h
double stdDev( int x1, int x2, int x3, int x4 );
double mean( int x1, int x2, int x3, int x4 );
double variance( int x1, int x2, int x3, int x4 );
double sumOfSquares( double d1, double d2, double d3, double d4 );
The functions must have those signatures.
You must implement the functions in a cpp file.
Submission of Part 1
Zip up your lab6.h and lab6.cpp and turn them in to Web-CAT.
Grading of Part 1
The grade from part 1 will be averaged with part 2 for the overall lab 6 score. So keep working to get those functions correct until you get a 100 on part 1.
Testing
Depending on how you coded your main, you might be able to just rerun your code and enter new values, or you may have to change the values in the code, less optimal, and recompile to see new results. In either case, you should test your code with various inputs, positive, negative, etc.
Part II
For part 2, well be using the functions you wrote in part 1, to get part 2 to do what we want. This will use the same command structure as we did in the last lab and weve been working on in the projects. So youll need a while loop and an if statement within to determine which command we are doing. Then youll call the function to do what is asked.
Commands
mean - this will compute the mean of the 4 given numbers
standard-deviation - this will compute the standard deviation of the 4 given numbers.
variance - this will compute the variance of the 4 given numbers.
sum-of-squares - this will compute the sum of the squares of the 4 given numbers.
Each of the commands and the 4 given numbers will be echoed back out.
Input
Here is what a sample input command file might look like:
mean 1 2 3 4 sum-of-squares 1 2 3 4 variance 1 2 3 4 standard-deviation 1 2 3 4
Output
For the given input file, this is what the output would look like:
Command: mean 1 2 3 4 Mean: 2.5 Command: sum-of-squares 1 2 3 4 Sum of Squares: 30 Command: variance 1 2 3 4 Variance: 1.25 Command: standard-deviation 1 2 3 4 Standard Deviation: 1.11803
Testing
Since this part uses an input file, you can change the input file to do different testing. If you remember the numbers you used when you tested part 1, you can use them as you are testing part II. The results should be the same since part II is just using the functions you wrote in part 1.
Requirements
You must continue to use the requirements for part 1.
You must also declare the function void standardDeviation( string input, string output ); in the lab6.h header file.
You must implement the functions in a cpp file.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started