[英]How to get process/function values into the final program?
so, I got a code and I need to express my answers into 3 columns with x,y,a in program, by compiling I only get the calculation answers - 1 column with y value throughout the cycle [5] . 因此,我得到了一个代码,我需要在程序中用x,y,a将我的答案表达为3列,通过编译,我在整个循环中只得到计算答案-y值为1列[5]。 How do I make it to a table with x, y, a columns and their values?
如何使用x,y,列及其值创建表格? Do I need another for cycle?
我需要另一个周期吗? Any suggestions are appreciated.
任何建议表示赞赏。
#include < iostream>
using namespace std;
float fy(float x, float a);
int main()
{
float a[5] = { -8, -6, -4, -2, 0 }
float y = 0;
int i = 0;
for (float x = -1; x <= 1; x += 0.5)
{
cout << fy(x, a[i]) << endl;
i++;
}
cin.get();
return 0;
}
float fy(float x, float a)
{
float y = 0;
if (sin(x)*a > 0)
y = sqrt(sin(x)*a);
else
cout << "no solution\n";
return y;
}
I guess what you want is something like this: 我想你想要的是这样的:
x y a
-1 2.59457 -8
-0.5 1.69604 -6
0 -nan -4
0.5 -nan -2
1 -nan 0
Right? 对?
Following code should do the job: 以下代码可以完成这项工作:
#include <iostream>
#include <math.h>
using namespace std;
float fy(float x, float a);
int main() {
float a[5] = {-8, -6, -4, -2, 0};
float y = 0;
int i = 0;
cout << "x"
<< "\t"
<< "y"
<< "\t"
<< "a" << endl;
for (float x = -1; x <= 1; x += 0.5)
{
cout << x << "\t" << fy(x, a[i]) << "\t" << a[i] << endl;
i++;
}
cin.get();
return 0;
}
float fy(float x, float a) {
float y = 0;
if (sin(x) * a > 0)
y = sqrt(sin(x) * a);
else
y = sqrt(-1);
return y;
}
The problem in your code is that you only print the result of the function. 代码中的问题在于,您仅打印函数的结果。 So you neither print x nor you print the a[i] value.
因此,您既不打印x也不打印a [i]值。 Check the replaced cout line and you'll see how to print the other values.
检查替换的提示行,您将看到如何打印其他值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.