我被卡在这个程序上,如下所示:
编写一个程序,将五个数字作为输入,并输出数字的均值(平均值)和标准差。如果数字是x1,x2,x3,x4,x5。
求解均值:均值=(x1 + x2 + x3 + x4 + x5)/ 5;
要解决偏差: 偏差= sqrt((pow(x1-x,2)+ pow(x2-x,2)+ pow(x3-x,2)+ pow(x4-x,2)+ pow(x5-x,2)) / 5);
您的程序必须至少包含以下函数:一个计算并返回均值的函数以及一个计算标准差的函数。
我正在尝试使用来自main的变量x1,x2,x3,x4,x5,但是我遇到了局部化的局部变量错误,无法对其进行编译。
#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;
}
答案 0 :(得分:3)
您在理解C ++基础方面有两个问题:
简要说明:
C ++中的变量仅在声明它们的范围内存在。对于您而言,您在函数implementation 'com.beardedhen:androidbootstrap:2.3.2
内声明了x1, x2, x3, x4, x5
,这意味着它们不在该范围之外。 main
和x1, x2, x3, x4, x5
中的变量meanfn
是完全不同的变量。他们只共享相同的名称,仅此而已。因此,当您在函数中声明它们时,它们之前没有分配值,这意味着您无法使用它们。
对于函数参数,您必须声明函数在其签名中接受的参数。您的deviationfn
签名是meanfn
,它不接受任何参数。如果您希望它只接受5个double meanfn()
变量,则应对其进行更改以适应。
基于上述说明,有两种解决方案:
double
在上述解决方案中,变量#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;
}
被移为全局变量,从而使它们成为全局访问权限,因此所有函数都可以使用它们。
第二个解决方案将考虑函数的参数传递,例如:
x1, x2, x3, x4, x5
答案 1 :(得分:2)
您可以使用一个包含所有5个变量的一个变量,而不是使用带有编号名称的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;
}
但是请注意,这些函数中的每个函数对每个值都执行相同的操作,然后根据数量对它们进行一些最终计算。使用<algorithm>
中的函数可以更好地表达这一点。然后,我们不需要知道有多少个元素。
#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());
}