how to pass multiple variables from main function down to other functions C++












-3















I am stuck on this program as follows:



Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are x1,x2,x3,x4,x5.



To solve mean : mean = (x1 + x2 + x3 + x4 + x5) / 5;



To solve deviation:
deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);



Your program must contain at least the following functions: a function that calculates and returns the mean and a function that calculates the standard deviation.



I am trying to use variable x1,x2,x3,x4,x5 from main but I am getting unitialized local variable errors and can not compile it.



#include <iostream>
#include <cmath>

using namespace std;

// Named constant definitions (and function declarations):


//Prototype
double meanfn();
double deviationfn();
// Main program:

int main()
{
// Variable declarations:
double x1, x2, x3, x4, x5;


// Function body:

cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
cin >> x1 >> x2 >> x3 >> x4 >> x5;


return 0;
} // end function main

double meanfn()
{

double x1, x2, x3, x4, x5;
double mean;



mean = (x1 + x2 + x3 + x4 + x5) / 5;

return mean;
}// end function meanfn

double deviationfn(double mean)
{
double x,deviation;
double x1, x2, x3, x4, x5;
x = mean;

deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

return deviation;
}









share|improve this question


















  • 3





    Seems like the problem is with your understanding in basic C++ function calls. Since it seems like you are trying to do an assignment, Instead of posting an answer, I would recommend you to learn some C++ basics first.

    – Teshan Shanuka J
    Nov 20 '18 at 4:00











  • You have asked multiple school homwork questions here. Why don't you discuss them first with your collegues before asking "professionals" who will give you immediate solutions. Will be much better for your programming future!

    – A.R.C.
    Nov 20 '18 at 9:54
















-3















I am stuck on this program as follows:



Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are x1,x2,x3,x4,x5.



To solve mean : mean = (x1 + x2 + x3 + x4 + x5) / 5;



To solve deviation:
deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);



Your program must contain at least the following functions: a function that calculates and returns the mean and a function that calculates the standard deviation.



I am trying to use variable x1,x2,x3,x4,x5 from main but I am getting unitialized local variable errors and can not compile it.



#include <iostream>
#include <cmath>

using namespace std;

// Named constant definitions (and function declarations):


//Prototype
double meanfn();
double deviationfn();
// Main program:

int main()
{
// Variable declarations:
double x1, x2, x3, x4, x5;


// Function body:

cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
cin >> x1 >> x2 >> x3 >> x4 >> x5;


return 0;
} // end function main

double meanfn()
{

double x1, x2, x3, x4, x5;
double mean;



mean = (x1 + x2 + x3 + x4 + x5) / 5;

return mean;
}// end function meanfn

double deviationfn(double mean)
{
double x,deviation;
double x1, x2, x3, x4, x5;
x = mean;

deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

return deviation;
}









share|improve this question


















  • 3





    Seems like the problem is with your understanding in basic C++ function calls. Since it seems like you are trying to do an assignment, Instead of posting an answer, I would recommend you to learn some C++ basics first.

    – Teshan Shanuka J
    Nov 20 '18 at 4:00











  • You have asked multiple school homwork questions here. Why don't you discuss them first with your collegues before asking "professionals" who will give you immediate solutions. Will be much better for your programming future!

    – A.R.C.
    Nov 20 '18 at 9:54














-3












-3








-3








I am stuck on this program as follows:



Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are x1,x2,x3,x4,x5.



To solve mean : mean = (x1 + x2 + x3 + x4 + x5) / 5;



To solve deviation:
deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);



Your program must contain at least the following functions: a function that calculates and returns the mean and a function that calculates the standard deviation.



I am trying to use variable x1,x2,x3,x4,x5 from main but I am getting unitialized local variable errors and can not compile it.



#include <iostream>
#include <cmath>

using namespace std;

// Named constant definitions (and function declarations):


//Prototype
double meanfn();
double deviationfn();
// Main program:

int main()
{
// Variable declarations:
double x1, x2, x3, x4, x5;


// Function body:

cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
cin >> x1 >> x2 >> x3 >> x4 >> x5;


return 0;
} // end function main

double meanfn()
{

double x1, x2, x3, x4, x5;
double mean;



mean = (x1 + x2 + x3 + x4 + x5) / 5;

return mean;
}// end function meanfn

double deviationfn(double mean)
{
double x,deviation;
double x1, x2, x3, x4, x5;
x = mean;

deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

return deviation;
}









share|improve this question














I am stuck on this program as follows:



Write a program that takes as input five numbers and outputs the mean (average) and standard deviation of the numbers. If the numbers are x1,x2,x3,x4,x5.



To solve mean : mean = (x1 + x2 + x3 + x4 + x5) / 5;



To solve deviation:
deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);



Your program must contain at least the following functions: a function that calculates and returns the mean and a function that calculates the standard deviation.



I am trying to use variable x1,x2,x3,x4,x5 from main but I am getting unitialized local variable errors and can not compile it.



#include <iostream>
#include <cmath>

using namespace std;

// Named constant definitions (and function declarations):


//Prototype
double meanfn();
double deviationfn();
// Main program:

int main()
{
// Variable declarations:
double x1, x2, x3, x4, x5;


// Function body:

cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
cin >> x1 >> x2 >> x3 >> x4 >> x5;


return 0;
} // end function main

double meanfn()
{

double x1, x2, x3, x4, x5;
double mean;



mean = (x1 + x2 + x3 + x4 + x5) / 5;

return mean;
}// end function meanfn

double deviationfn(double mean)
{
double x,deviation;
double x1, x2, x3, x4, x5;
x = mean;

deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

return deviation;
}






c++ function






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 3:50









Wessley Ray ConeWessley Ray Cone

12




12








  • 3





    Seems like the problem is with your understanding in basic C++ function calls. Since it seems like you are trying to do an assignment, Instead of posting an answer, I would recommend you to learn some C++ basics first.

    – Teshan Shanuka J
    Nov 20 '18 at 4:00











  • You have asked multiple school homwork questions here. Why don't you discuss them first with your collegues before asking "professionals" who will give you immediate solutions. Will be much better for your programming future!

    – A.R.C.
    Nov 20 '18 at 9:54














  • 3





    Seems like the problem is with your understanding in basic C++ function calls. Since it seems like you are trying to do an assignment, Instead of posting an answer, I would recommend you to learn some C++ basics first.

    – Teshan Shanuka J
    Nov 20 '18 at 4:00











  • You have asked multiple school homwork questions here. Why don't you discuss them first with your collegues before asking "professionals" who will give you immediate solutions. Will be much better for your programming future!

    – A.R.C.
    Nov 20 '18 at 9:54








3




3





Seems like the problem is with your understanding in basic C++ function calls. Since it seems like you are trying to do an assignment, Instead of posting an answer, I would recommend you to learn some C++ basics first.

– Teshan Shanuka J
Nov 20 '18 at 4:00





Seems like the problem is with your understanding in basic C++ function calls. Since it seems like you are trying to do an assignment, Instead of posting an answer, I would recommend you to learn some C++ basics first.

– Teshan Shanuka J
Nov 20 '18 at 4:00













You have asked multiple school homwork questions here. Why don't you discuss them first with your collegues before asking "professionals" who will give you immediate solutions. Will be much better for your programming future!

– A.R.C.
Nov 20 '18 at 9:54





You have asked multiple school homwork questions here. Why don't you discuss them first with your collegues before asking "professionals" who will give you immediate solutions. Will be much better for your programming future!

– A.R.C.
Nov 20 '18 at 9:54












2 Answers
2






active

oldest

votes


















3














You have two problems of understanding basics in C++:




  1. Variable scope.

  2. Function parameters.


To briefly explain:



Variables in C++ live only in the scope they were declared in. In your case, you declared: x1, x2, x3, x4, x5 inside of the function main which means they do not exist outside that scope. The variables x1, x2, x3, x4, x5 in meanfn and deviationfn are completely different variables. They only share the same names and that's about it. Therefore, when you declared them inside the functions, they had no previously assigned values which means you cannot use them.



As for function parameters, you have to declare what parameters a function accepts in its signature. Your meanfn signature is double meanfn() which accepts no parameters. If you want it to accept 5 and only 5 double variables, you should change it to accommodate.



There are two solutions based on the explanations above:



#include <iostream>
#include <cmath>

using namespace std;

// Named constant definitions (and function declarations):


//Prototype
double meanfn();
double deviationfn();
// Main program:

// Variable declarations:
double x1, x2, x3, x4, x5; // outside of 'main' making them global and everything has access to these variables.

int main()
{
// Function body:

cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
cin >> x1 >> x2 >> x3 >> x4 >> x5;

// CALL YOUR FUNCTIONS, THEY WONT BE CALLED BY THEIR OWN
double mean = meanfn();
double deviation = deviationfn(mean);

// print results
std::cout << "n Mean: " << mean << "n Deviation: " << deviation;

return 0;
} // end function main

double meanfn()
{
double mean = (x1 + x2 + x3 + x4 + x5) / 5;

return mean;
}// end function meanfn

double deviationfn(double mean)
{
double x,deviation;
x = mean;

deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

return deviation;
}


In the solution above, the variables x1, x2, x3, x4, x5 were moved to be global variables making their access global and hence all functions can use them.





The second solution would consider parameter passing for the functions, such as:



#include <iostream>
#include <cmath>

using namespace std;

// Named constant definitions (and function declarations):


//Prototype
double meanfn(double x1, double x2, double x3, double x4, double x5);
double deviationfn(double mean, double x1, double x2, double x3, double x4, double x5);

// Main program:
int main()
{
// Variable declarations:
double x1, x2, x3, x4, x5;


// Function body:

cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
cin >> x1 >> x2 >> x3 >> x4 >> x5;

// see the difference in the function calls?
double mean = meanfn(x1, x2, x3, x4, x5);
double deviation = deviationfn(mean, x1, x2, x3, x4, x5);

// print results
std::cout << "n Mean: " << mean << "n Deviation: " << deviation;

return 0;
} // end function main

double meanfn(double x1, double x2, double x3, double x4, double x5)
{
double mean;
mean = (x1 + x2 + x3 + x4 + x5) / 5;

return mean;
}// end function meanfn

double deviationfn(double mean, double x1, double x2, double x3, double x4, double x5)
{
double x,deviation;
x = mean;

deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

return deviation;
}





share|improve this answer

































    2














    Rather than having 5 variables with numbered names, you can instead have one variable that contains all 5.



    #include <iostream>
    #include <cmath>
    #include <array>

    using Values = std::array<double, 5>;

    double mean(Values xs)
    {
    return (xs[0] + xs[1] + xs[2] + xs[3] + xs[4]) / xs.size();
    }

    double distribution(Values xs, double x_bar)
    {
    return sqrt((pow(xs[0] - x_bar, 2) + pow(xs[1] - x_bar, 2) + pow(xs[2] - x_bar, 2) + pow(xs[3] - x_bar, 2) + pow(xs[4] - x_bar, 2)) / xs.size());
    }

    int main()
    {
    std::cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << std::endl;

    Values xs;
    std::cin >> xs[0] >> xs[1] >> xs[2] >> xs[3] >> xs[4];

    auto x_bar = mean(xs);
    auto sigma = distribution(xs, x_bar);

    std::cout << "n Mean: " << x_bar << "n Deviation: " << sigma;
    }


    But notice that each of those functions is doing the same operation to each of the values, then performing some final calculation based on how many. That can better be expressed using functions from <algorithm>. We then don't need to know how many elements there are.



    #include <algorithm>
    #include <vector>

    using Values = std::vector<double>; // Any number of values

    double mean(Values xs)
    {
    // The default combiner is +
    return std::accumulate(xs.begin(), xs.end(), 0) / xs.size();
    }

    double distribution(Values xs, double x_bar)
    {
    auto sum_squares = [x_bar](double cumulative, double x){ return cumulative + pow(x - x_bar, 2); }
    return sqrt(std::accumulate(xs.begin(), xs.end(), 0, sum_squares) / xs.size());
    }





    share|improve this answer


























    • This is a very good answer and I had that in mind before I typed mine. However, since the question showed lack of basic knowledge in the language, I thought this could be intimidating and would freak him out. Nonetheless, +1

      – Everyone
      Nov 20 '18 at 12:05











    • Anyway, for the sake of completeness, why not pass Values by const reference instead of copying it with the function call.

      – Everyone
      Nov 20 '18 at 12:10











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385956%2fhow-to-pass-multiple-variables-from-main-function-down-to-other-functions-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You have two problems of understanding basics in C++:




    1. Variable scope.

    2. Function parameters.


    To briefly explain:



    Variables in C++ live only in the scope they were declared in. In your case, you declared: x1, x2, x3, x4, x5 inside of the function main which means they do not exist outside that scope. The variables x1, x2, x3, x4, x5 in meanfn and deviationfn are completely different variables. They only share the same names and that's about it. Therefore, when you declared them inside the functions, they had no previously assigned values which means you cannot use them.



    As for function parameters, you have to declare what parameters a function accepts in its signature. Your meanfn signature is double meanfn() which accepts no parameters. If you want it to accept 5 and only 5 double variables, you should change it to accommodate.



    There are two solutions based on the explanations above:



    #include <iostream>
    #include <cmath>

    using namespace std;

    // Named constant definitions (and function declarations):


    //Prototype
    double meanfn();
    double deviationfn();
    // Main program:

    // Variable declarations:
    double x1, x2, x3, x4, x5; // outside of 'main' making them global and everything has access to these variables.

    int main()
    {
    // Function body:

    cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
    cin >> x1 >> x2 >> x3 >> x4 >> x5;

    // CALL YOUR FUNCTIONS, THEY WONT BE CALLED BY THEIR OWN
    double mean = meanfn();
    double deviation = deviationfn(mean);

    // print results
    std::cout << "n Mean: " << mean << "n Deviation: " << deviation;

    return 0;
    } // end function main

    double meanfn()
    {
    double mean = (x1 + x2 + x3 + x4 + x5) / 5;

    return mean;
    }// end function meanfn

    double deviationfn(double mean)
    {
    double x,deviation;
    x = mean;

    deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

    return deviation;
    }


    In the solution above, the variables x1, x2, x3, x4, x5 were moved to be global variables making their access global and hence all functions can use them.





    The second solution would consider parameter passing for the functions, such as:



    #include <iostream>
    #include <cmath>

    using namespace std;

    // Named constant definitions (and function declarations):


    //Prototype
    double meanfn(double x1, double x2, double x3, double x4, double x5);
    double deviationfn(double mean, double x1, double x2, double x3, double x4, double x5);

    // Main program:
    int main()
    {
    // Variable declarations:
    double x1, x2, x3, x4, x5;


    // Function body:

    cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
    cin >> x1 >> x2 >> x3 >> x4 >> x5;

    // see the difference in the function calls?
    double mean = meanfn(x1, x2, x3, x4, x5);
    double deviation = deviationfn(mean, x1, x2, x3, x4, x5);

    // print results
    std::cout << "n Mean: " << mean << "n Deviation: " << deviation;

    return 0;
    } // end function main

    double meanfn(double x1, double x2, double x3, double x4, double x5)
    {
    double mean;
    mean = (x1 + x2 + x3 + x4 + x5) / 5;

    return mean;
    }// end function meanfn

    double deviationfn(double mean, double x1, double x2, double x3, double x4, double x5)
    {
    double x,deviation;
    x = mean;

    deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

    return deviation;
    }





    share|improve this answer






























      3














      You have two problems of understanding basics in C++:




      1. Variable scope.

      2. Function parameters.


      To briefly explain:



      Variables in C++ live only in the scope they were declared in. In your case, you declared: x1, x2, x3, x4, x5 inside of the function main which means they do not exist outside that scope. The variables x1, x2, x3, x4, x5 in meanfn and deviationfn are completely different variables. They only share the same names and that's about it. Therefore, when you declared them inside the functions, they had no previously assigned values which means you cannot use them.



      As for function parameters, you have to declare what parameters a function accepts in its signature. Your meanfn signature is double meanfn() which accepts no parameters. If you want it to accept 5 and only 5 double variables, you should change it to accommodate.



      There are two solutions based on the explanations above:



      #include <iostream>
      #include <cmath>

      using namespace std;

      // Named constant definitions (and function declarations):


      //Prototype
      double meanfn();
      double deviationfn();
      // Main program:

      // Variable declarations:
      double x1, x2, x3, x4, x5; // outside of 'main' making them global and everything has access to these variables.

      int main()
      {
      // Function body:

      cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
      cin >> x1 >> x2 >> x3 >> x4 >> x5;

      // CALL YOUR FUNCTIONS, THEY WONT BE CALLED BY THEIR OWN
      double mean = meanfn();
      double deviation = deviationfn(mean);

      // print results
      std::cout << "n Mean: " << mean << "n Deviation: " << deviation;

      return 0;
      } // end function main

      double meanfn()
      {
      double mean = (x1 + x2 + x3 + x4 + x5) / 5;

      return mean;
      }// end function meanfn

      double deviationfn(double mean)
      {
      double x,deviation;
      x = mean;

      deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

      return deviation;
      }


      In the solution above, the variables x1, x2, x3, x4, x5 were moved to be global variables making their access global and hence all functions can use them.





      The second solution would consider parameter passing for the functions, such as:



      #include <iostream>
      #include <cmath>

      using namespace std;

      // Named constant definitions (and function declarations):


      //Prototype
      double meanfn(double x1, double x2, double x3, double x4, double x5);
      double deviationfn(double mean, double x1, double x2, double x3, double x4, double x5);

      // Main program:
      int main()
      {
      // Variable declarations:
      double x1, x2, x3, x4, x5;


      // Function body:

      cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
      cin >> x1 >> x2 >> x3 >> x4 >> x5;

      // see the difference in the function calls?
      double mean = meanfn(x1, x2, x3, x4, x5);
      double deviation = deviationfn(mean, x1, x2, x3, x4, x5);

      // print results
      std::cout << "n Mean: " << mean << "n Deviation: " << deviation;

      return 0;
      } // end function main

      double meanfn(double x1, double x2, double x3, double x4, double x5)
      {
      double mean;
      mean = (x1 + x2 + x3 + x4 + x5) / 5;

      return mean;
      }// end function meanfn

      double deviationfn(double mean, double x1, double x2, double x3, double x4, double x5)
      {
      double x,deviation;
      x = mean;

      deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

      return deviation;
      }





      share|improve this answer




























        3












        3








        3







        You have two problems of understanding basics in C++:




        1. Variable scope.

        2. Function parameters.


        To briefly explain:



        Variables in C++ live only in the scope they were declared in. In your case, you declared: x1, x2, x3, x4, x5 inside of the function main which means they do not exist outside that scope. The variables x1, x2, x3, x4, x5 in meanfn and deviationfn are completely different variables. They only share the same names and that's about it. Therefore, when you declared them inside the functions, they had no previously assigned values which means you cannot use them.



        As for function parameters, you have to declare what parameters a function accepts in its signature. Your meanfn signature is double meanfn() which accepts no parameters. If you want it to accept 5 and only 5 double variables, you should change it to accommodate.



        There are two solutions based on the explanations above:



        #include <iostream>
        #include <cmath>

        using namespace std;

        // Named constant definitions (and function declarations):


        //Prototype
        double meanfn();
        double deviationfn();
        // Main program:

        // Variable declarations:
        double x1, x2, x3, x4, x5; // outside of 'main' making them global and everything has access to these variables.

        int main()
        {
        // Function body:

        cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
        cin >> x1 >> x2 >> x3 >> x4 >> x5;

        // CALL YOUR FUNCTIONS, THEY WONT BE CALLED BY THEIR OWN
        double mean = meanfn();
        double deviation = deviationfn(mean);

        // print results
        std::cout << "n Mean: " << mean << "n Deviation: " << deviation;

        return 0;
        } // end function main

        double meanfn()
        {
        double mean = (x1 + x2 + x3 + x4 + x5) / 5;

        return mean;
        }// end function meanfn

        double deviationfn(double mean)
        {
        double x,deviation;
        x = mean;

        deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

        return deviation;
        }


        In the solution above, the variables x1, x2, x3, x4, x5 were moved to be global variables making their access global and hence all functions can use them.





        The second solution would consider parameter passing for the functions, such as:



        #include <iostream>
        #include <cmath>

        using namespace std;

        // Named constant definitions (and function declarations):


        //Prototype
        double meanfn(double x1, double x2, double x3, double x4, double x5);
        double deviationfn(double mean, double x1, double x2, double x3, double x4, double x5);

        // Main program:
        int main()
        {
        // Variable declarations:
        double x1, x2, x3, x4, x5;


        // Function body:

        cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
        cin >> x1 >> x2 >> x3 >> x4 >> x5;

        // see the difference in the function calls?
        double mean = meanfn(x1, x2, x3, x4, x5);
        double deviation = deviationfn(mean, x1, x2, x3, x4, x5);

        // print results
        std::cout << "n Mean: " << mean << "n Deviation: " << deviation;

        return 0;
        } // end function main

        double meanfn(double x1, double x2, double x3, double x4, double x5)
        {
        double mean;
        mean = (x1 + x2 + x3 + x4 + x5) / 5;

        return mean;
        }// end function meanfn

        double deviationfn(double mean, double x1, double x2, double x3, double x4, double x5)
        {
        double x,deviation;
        x = mean;

        deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

        return deviation;
        }





        share|improve this answer















        You have two problems of understanding basics in C++:




        1. Variable scope.

        2. Function parameters.


        To briefly explain:



        Variables in C++ live only in the scope they were declared in. In your case, you declared: x1, x2, x3, x4, x5 inside of the function main which means they do not exist outside that scope. The variables x1, x2, x3, x4, x5 in meanfn and deviationfn are completely different variables. They only share the same names and that's about it. Therefore, when you declared them inside the functions, they had no previously assigned values which means you cannot use them.



        As for function parameters, you have to declare what parameters a function accepts in its signature. Your meanfn signature is double meanfn() which accepts no parameters. If you want it to accept 5 and only 5 double variables, you should change it to accommodate.



        There are two solutions based on the explanations above:



        #include <iostream>
        #include <cmath>

        using namespace std;

        // Named constant definitions (and function declarations):


        //Prototype
        double meanfn();
        double deviationfn();
        // Main program:

        // Variable declarations:
        double x1, x2, x3, x4, x5; // outside of 'main' making them global and everything has access to these variables.

        int main()
        {
        // Function body:

        cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
        cin >> x1 >> x2 >> x3 >> x4 >> x5;

        // CALL YOUR FUNCTIONS, THEY WONT BE CALLED BY THEIR OWN
        double mean = meanfn();
        double deviation = deviationfn(mean);

        // print results
        std::cout << "n Mean: " << mean << "n Deviation: " << deviation;

        return 0;
        } // end function main

        double meanfn()
        {
        double mean = (x1 + x2 + x3 + x4 + x5) / 5;

        return mean;
        }// end function meanfn

        double deviationfn(double mean)
        {
        double x,deviation;
        x = mean;

        deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

        return deviation;
        }


        In the solution above, the variables x1, x2, x3, x4, x5 were moved to be global variables making their access global and hence all functions can use them.





        The second solution would consider parameter passing for the functions, such as:



        #include <iostream>
        #include <cmath>

        using namespace std;

        // Named constant definitions (and function declarations):


        //Prototype
        double meanfn(double x1, double x2, double x3, double x4, double x5);
        double deviationfn(double mean, double x1, double x2, double x3, double x4, double x5);

        // Main program:
        int main()
        {
        // Variable declarations:
        double x1, x2, x3, x4, x5;


        // Function body:

        cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << endl;
        cin >> x1 >> x2 >> x3 >> x4 >> x5;

        // see the difference in the function calls?
        double mean = meanfn(x1, x2, x3, x4, x5);
        double deviation = deviationfn(mean, x1, x2, x3, x4, x5);

        // print results
        std::cout << "n Mean: " << mean << "n Deviation: " << deviation;

        return 0;
        } // end function main

        double meanfn(double x1, double x2, double x3, double x4, double x5)
        {
        double mean;
        mean = (x1 + x2 + x3 + x4 + x5) / 5;

        return mean;
        }// end function meanfn

        double deviationfn(double mean, double x1, double x2, double x3, double x4, double x5)
        {
        double x,deviation;
        x = mean;

        deviation = sqrt((pow(x1 - x, 2) + pow(x2 - x, 2) + pow(x3 - x, 2) + pow(x4 - x, 2) + pow(x5 - x, 2)) / 5);

        return deviation;
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 9:06

























        answered Nov 20 '18 at 4:49









        EveryoneEveryone

        819318




        819318

























            2














            Rather than having 5 variables with numbered names, you can instead have one variable that contains all 5.



            #include <iostream>
            #include <cmath>
            #include <array>

            using Values = std::array<double, 5>;

            double mean(Values xs)
            {
            return (xs[0] + xs[1] + xs[2] + xs[3] + xs[4]) / xs.size();
            }

            double distribution(Values xs, double x_bar)
            {
            return sqrt((pow(xs[0] - x_bar, 2) + pow(xs[1] - x_bar, 2) + pow(xs[2] - x_bar, 2) + pow(xs[3] - x_bar, 2) + pow(xs[4] - x_bar, 2)) / xs.size());
            }

            int main()
            {
            std::cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << std::endl;

            Values xs;
            std::cin >> xs[0] >> xs[1] >> xs[2] >> xs[3] >> xs[4];

            auto x_bar = mean(xs);
            auto sigma = distribution(xs, x_bar);

            std::cout << "n Mean: " << x_bar << "n Deviation: " << sigma;
            }


            But notice that each of those functions is doing the same operation to each of the values, then performing some final calculation based on how many. That can better be expressed using functions from <algorithm>. We then don't need to know how many elements there are.



            #include <algorithm>
            #include <vector>

            using Values = std::vector<double>; // Any number of values

            double mean(Values xs)
            {
            // The default combiner is +
            return std::accumulate(xs.begin(), xs.end(), 0) / xs.size();
            }

            double distribution(Values xs, double x_bar)
            {
            auto sum_squares = [x_bar](double cumulative, double x){ return cumulative + pow(x - x_bar, 2); }
            return sqrt(std::accumulate(xs.begin(), xs.end(), 0, sum_squares) / xs.size());
            }





            share|improve this answer


























            • This is a very good answer and I had that in mind before I typed mine. However, since the question showed lack of basic knowledge in the language, I thought this could be intimidating and would freak him out. Nonetheless, +1

              – Everyone
              Nov 20 '18 at 12:05











            • Anyway, for the sake of completeness, why not pass Values by const reference instead of copying it with the function call.

              – Everyone
              Nov 20 '18 at 12:10
















            2














            Rather than having 5 variables with numbered names, you can instead have one variable that contains all 5.



            #include <iostream>
            #include <cmath>
            #include <array>

            using Values = std::array<double, 5>;

            double mean(Values xs)
            {
            return (xs[0] + xs[1] + xs[2] + xs[3] + xs[4]) / xs.size();
            }

            double distribution(Values xs, double x_bar)
            {
            return sqrt((pow(xs[0] - x_bar, 2) + pow(xs[1] - x_bar, 2) + pow(xs[2] - x_bar, 2) + pow(xs[3] - x_bar, 2) + pow(xs[4] - x_bar, 2)) / xs.size());
            }

            int main()
            {
            std::cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << std::endl;

            Values xs;
            std::cin >> xs[0] >> xs[1] >> xs[2] >> xs[3] >> xs[4];

            auto x_bar = mean(xs);
            auto sigma = distribution(xs, x_bar);

            std::cout << "n Mean: " << x_bar << "n Deviation: " << sigma;
            }


            But notice that each of those functions is doing the same operation to each of the values, then performing some final calculation based on how many. That can better be expressed using functions from <algorithm>. We then don't need to know how many elements there are.



            #include <algorithm>
            #include <vector>

            using Values = std::vector<double>; // Any number of values

            double mean(Values xs)
            {
            // The default combiner is +
            return std::accumulate(xs.begin(), xs.end(), 0) / xs.size();
            }

            double distribution(Values xs, double x_bar)
            {
            auto sum_squares = [x_bar](double cumulative, double x){ return cumulative + pow(x - x_bar, 2); }
            return sqrt(std::accumulate(xs.begin(), xs.end(), 0, sum_squares) / xs.size());
            }





            share|improve this answer


























            • This is a very good answer and I had that in mind before I typed mine. However, since the question showed lack of basic knowledge in the language, I thought this could be intimidating and would freak him out. Nonetheless, +1

              – Everyone
              Nov 20 '18 at 12:05











            • Anyway, for the sake of completeness, why not pass Values by const reference instead of copying it with the function call.

              – Everyone
              Nov 20 '18 at 12:10














            2












            2








            2







            Rather than having 5 variables with numbered names, you can instead have one variable that contains all 5.



            #include <iostream>
            #include <cmath>
            #include <array>

            using Values = std::array<double, 5>;

            double mean(Values xs)
            {
            return (xs[0] + xs[1] + xs[2] + xs[3] + xs[4]) / xs.size();
            }

            double distribution(Values xs, double x_bar)
            {
            return sqrt((pow(xs[0] - x_bar, 2) + pow(xs[1] - x_bar, 2) + pow(xs[2] - x_bar, 2) + pow(xs[3] - x_bar, 2) + pow(xs[4] - x_bar, 2)) / xs.size());
            }

            int main()
            {
            std::cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << std::endl;

            Values xs;
            std::cin >> xs[0] >> xs[1] >> xs[2] >> xs[3] >> xs[4];

            auto x_bar = mean(xs);
            auto sigma = distribution(xs, x_bar);

            std::cout << "n Mean: " << x_bar << "n Deviation: " << sigma;
            }


            But notice that each of those functions is doing the same operation to each of the values, then performing some final calculation based on how many. That can better be expressed using functions from <algorithm>. We then don't need to know how many elements there are.



            #include <algorithm>
            #include <vector>

            using Values = std::vector<double>; // Any number of values

            double mean(Values xs)
            {
            // The default combiner is +
            return std::accumulate(xs.begin(), xs.end(), 0) / xs.size();
            }

            double distribution(Values xs, double x_bar)
            {
            auto sum_squares = [x_bar](double cumulative, double x){ return cumulative + pow(x - x_bar, 2); }
            return sqrt(std::accumulate(xs.begin(), xs.end(), 0, sum_squares) / xs.size());
            }





            share|improve this answer















            Rather than having 5 variables with numbered names, you can instead have one variable that contains all 5.



            #include <iostream>
            #include <cmath>
            #include <array>

            using Values = std::array<double, 5>;

            double mean(Values xs)
            {
            return (xs[0] + xs[1] + xs[2] + xs[3] + xs[4]) / xs.size();
            }

            double distribution(Values xs, double x_bar)
            {
            return sqrt((pow(xs[0] - x_bar, 2) + pow(xs[1] - x_bar, 2) + pow(xs[2] - x_bar, 2) + pow(xs[3] - x_bar, 2) + pow(xs[4] - x_bar, 2)) / xs.size());
            }

            int main()
            {
            std::cout << "Enter a number followed by space 5 times to calculate the mean of five numbers. " << std::endl;

            Values xs;
            std::cin >> xs[0] >> xs[1] >> xs[2] >> xs[3] >> xs[4];

            auto x_bar = mean(xs);
            auto sigma = distribution(xs, x_bar);

            std::cout << "n Mean: " << x_bar << "n Deviation: " << sigma;
            }


            But notice that each of those functions is doing the same operation to each of the values, then performing some final calculation based on how many. That can better be expressed using functions from <algorithm>. We then don't need to know how many elements there are.



            #include <algorithm>
            #include <vector>

            using Values = std::vector<double>; // Any number of values

            double mean(Values xs)
            {
            // The default combiner is +
            return std::accumulate(xs.begin(), xs.end(), 0) / xs.size();
            }

            double distribution(Values xs, double x_bar)
            {
            auto sum_squares = [x_bar](double cumulative, double x){ return cumulative + pow(x - x_bar, 2); }
            return sqrt(std::accumulate(xs.begin(), xs.end(), 0, sum_squares) / xs.size());
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '18 at 9:57

























            answered Nov 20 '18 at 9:45









            CalethCaleth

            17.1k22139




            17.1k22139













            • This is a very good answer and I had that in mind before I typed mine. However, since the question showed lack of basic knowledge in the language, I thought this could be intimidating and would freak him out. Nonetheless, +1

              – Everyone
              Nov 20 '18 at 12:05











            • Anyway, for the sake of completeness, why not pass Values by const reference instead of copying it with the function call.

              – Everyone
              Nov 20 '18 at 12:10



















            • This is a very good answer and I had that in mind before I typed mine. However, since the question showed lack of basic knowledge in the language, I thought this could be intimidating and would freak him out. Nonetheless, +1

              – Everyone
              Nov 20 '18 at 12:05











            • Anyway, for the sake of completeness, why not pass Values by const reference instead of copying it with the function call.

              – Everyone
              Nov 20 '18 at 12:10

















            This is a very good answer and I had that in mind before I typed mine. However, since the question showed lack of basic knowledge in the language, I thought this could be intimidating and would freak him out. Nonetheless, +1

            – Everyone
            Nov 20 '18 at 12:05





            This is a very good answer and I had that in mind before I typed mine. However, since the question showed lack of basic knowledge in the language, I thought this could be intimidating and would freak him out. Nonetheless, +1

            – Everyone
            Nov 20 '18 at 12:05













            Anyway, for the sake of completeness, why not pass Values by const reference instead of copying it with the function call.

            – Everyone
            Nov 20 '18 at 12:10





            Anyway, for the sake of completeness, why not pass Values by const reference instead of copying it with the function call.

            – Everyone
            Nov 20 '18 at 12:10


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385956%2fhow-to-pass-multiple-variables-from-main-function-down-to-other-functions-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$