如何获得过程/函数值进入最终程序
How to get process/function values into the final program?
所以,我得到了一个代码,我需要用x,y,a在程序中表达我的答案,通过编译,我只得到计算答案-整个循环中有y值的1列[5]。我怎么把它变成一个有x, y, a列和它们的值的表?我需要另一辆自行车吗?欢迎提出任何建议。
#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 solutionn";
return y;
}
我猜你想要的是这样的:
x y a
-1 2.59457 -8
-0.5 1.69604 -6
0 -nan -4
0.5 -nan -2
1 -nan 0
对吧?
下面的代码可以完成这项工作:
#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;
}
代码中的问题是您只打印函数的结果。所以既不输出x也不输出a[i]的值。检查替换的cout行,您将看到如何打印其他值。
相关文章:
- 如何优化C++键值程序以获得更快的运行时间
- C++程序未获得所需的输出
- 如何在多线程应用程序中获得花费的时间
- 如何在Cocos2d Android应用程序中获得呼叫/挂起事件
- 继续在质数C 程序中获得浮点异常
- 为什么我会在数组队列程序中获得不希望的输出
- 运行外部程序并获得返回的整数
- 无法在我的程序中获得所需的输出
- 由t运行的程序无法获得正确的数字
- 如何在Qt程序内获得root权限
- 在简单的文件处理程序中获得异常输出
- 在成功运行10小时后,在发布模式下从windows应用程序中获得异常,如何检测出问题
- 如何从GLSL程序中获得uvec4输出
- 如何从c++ /MFC程序中获得控制台窗口
- 编译Qt应用程序以获得更好的调试信息(Linux)
- 如何在现有的Windows应用程序中获得ATL支持
- 如何从调用QProcess的程序中获得返回的标准输出
- 如何在应用程序中获得可信时间,可能是NTP
- 在android代码中创建一个套接字(而不是在android应用程序中)获得Permission Denied
- 如何审核我的Windows应用程序以获得正确的Unicode处理