-1

I am on the verge of finishing this project but I cannot figure it out. I asked my professor but when he said "for the 2 functions, some or all of the parameters need to be passed by reference, so that the functions can affect the arguments being passed in." But that just confused me more. IF anyone has any tips I would appreciate it. I would rather not have someone just solve it but more of point me in the right direction as that is how learning is accomplished.

Tried rewriting the code a couple of times.

#include <iostream>
#include <cmath>    //included for some pre-defined functions

using namespace std;

//function PROTOTYPES will go here

void getValues(double x1, double x2, double x3, double x4, double x5);
double calcMean(double x1, double x2, double x3, double x4, double x5);
double calcDev(double stDev, double mean, double x1, double x2, double x3, double x4, double x5);
void printResults(double mean, double stDev);

//DO NOT CHANGE ANYTHING IN THE MAIN FUNCTION!!!
int main()
{   
    //Defining variables to store the values, the mean and the standard deviation
    double x1, x2, x3, x4, x5;
    double mean, stDev;


    // calling all the functions
    getValues(x1, x2, x3, x4, x5);      // asks and reads in the 5 values.
    mean = calcMean(x1, x2, x3, x4, x5);    //calculates the mean
    calcDev(mean, stDev, x1, x2, x3, x4, x5);   //calculates the standard deviation
    printResults(mean, stDev);      //displays the results

    return 0;
}

//the function DEFINITIONS will go below

void getValues(double x1, double x2, double x3, double x4, double x5){
    cout<<"Please enter 5 values: ";
    cin>>x1;
    cin>>x2;
    cin>>x3;
    cin>>x4;
    cin>>x5;
}

double calcMean(double x1, double x2, double x3, double x4, double x5){
    return (x1+x2+x3+x4+x5)/5;

}

double calcDev(double stDev, double mean, double x1, double x2, double x3, double x4, double x5){
    return stDev=sqrt(((pow(x1-mean, 2)+pow(x2-mean, 2)+pow(x3-mean, 2)+pow(x4-mean, 2)+pow(x5-mean, 2))/5));
}

void printResults(double mean, double stDev){
    std::cout<<"The mean of the 5 values is: "<<mean<<std::endl;
    std::cout<<"The standard deviation of the 5 values is: "<<stDev<<std::endl;
}

If I type in 5 7 9 11 13 then the mean should be 9 with a standard deviation of about 2.8 if I am not mistaken.

user207421
  • 305,947
  • 44
  • 307
  • 483
aceaxer
  • 17
  • 3
  • In what functions are you changing the parameters? – GBlodgett Apr 17 '19 at 01:00
  • 1
    You need to understand pass by reference, as getValues() doesn't do anything other than get user input and have those values go out of scope. calcDev, doesn't do what you think. As a good note, your printResults() looks okay :) – Omid CompSCI Apr 17 '19 at 01:01
  • 2
    Your issue has nothing to do with getting the mean of deviation but actually has to do with not understanding how to get information back from a function. Try passing "C++ 'pass by reference'" into your favorite search engine. – David Schwartz Apr 17 '19 at 01:03
  • ON the side note, you definitely should read about vectors. –  Apr 17 '19 at 01:04

1 Answers1

1

The input parameters into getValues() needs to be passed by reference, otherwise what you currently have will have the user input the values and store into the variable only to go out of scope. This:

void getValues(double x1, double x2, double x3, double x4, double x5);

should be:

void getValues(double& x1, double& x2, double& x3, double& x4, double& x5);

Also when implementing it need to have same thing:

void getValues(double& x1, double& x2, double& x3, double& x4, double& x5) {
    cout<<"Please enter 5 values: ";
    cin>>x1;
    cin>>x2;
    cin>>x3;
    cin>>x4;
    cin>>x5;
}

The calcDev() should probably be changed to either return a double which would be the std. deviation, or pass it by reference. But going from the comment saying: //DO NOT CHANGE ANYTHING IN THE MAIN FUNCTION!!! and the call is as goes:

calcDev(mean, stDev, x1, x2, x3, x4, x5); 

Which means we need to return nothing and pass back by reference the stDev. So that means we need to change calcDev() as follows:

void calcDev(double mean, double& stDev, double x1, double x2, double x3, double x4, double x5);

And should be implemented as such:

void calcDev(double mean, double& stDev, double x1, double x2, double x3, double x4, double x5) {
  stDev = sqrt(((pow(x1-mean, 2)+pow(x2-mean, 2)+pow(x3-mean, 2)+pow(x4-mean, 
  2)+pow(x5-mean, 2))/5));
}
Omid CompSCI
  • 1,861
  • 3
  • 17
  • 29
  • Please understand why these changes are needed, and try to deduce certain things, like look at what main function is calling the functions like, and if they are or aren't returning things, and from there write the correct function declarations. Hopefully some of these comments help. – Omid CompSCI Apr 17 '19 at 01:17
  • As a side note, these are reasons why you should always initialize your variables, looks like in this case your professor doesn't let you change main() but those variables should all be initialized to 0.0. – Omid CompSCI Apr 17 '19 at 01:20
  • Thank you very much but my one question is What does the double& do compared to the double alone without the & symbol? and why is that making it work correctly? – aceaxer Apr 17 '19 at 01:23
  • You need to look up 'pass by reference' vs 'pass by value'. In easier terms, just know & will 'return' the value outside of the function or wherever you pass it. Otherwise it is just copied and will not return the value. Feel free to mark this as your answer if you think it addressed your question, so others can find it useful. – Omid CompSCI Apr 17 '19 at 01:24
  • Two links: https://www.cs.fsu.edu/~myers/c++/notes/references.html & https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value – Omid CompSCI Apr 17 '19 at 01:25