简体   繁体   English

如何将过程/功能值输入最终程序?

[英]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.

相关问题 gdb如何读取正在调试的程序/进程的寄存器值? 寄存器与流程如何关联? - How does gdb read the register values of a program / process it's debugging? How are registers associated with a process? 如何将函数断言为 CRTP 的最终函数? - How to assert a function as final for CRTP? MFC-如何获取所有与进程ID相关的信息(它们是一个程序的所有进程) - MFC - How to get all process id related (they are all process of one program) 如何从同一个进程中挂钩一个函数并获取调用者函数的地址? - How to hook a function from the same process and get the address of the caller function? 如何在递归中仅使用最终值? - How to use only the final values in recursion? 程序如何成为一个过程。 OS如何使程序成为一个过程 - How Program Becomes a Process. How OS makes Program a process 关于如何检测 function 是否存在,是否有最终的简单答案? - Is there a final, simple answer of how to detect if a function exists? 在程序中获取AIX中进程的虚拟内存大小 - get virtual memory size of process in AIX in program 如何使用Boost程序选项获取默认参数值? - How do I get default argument values with boost program options? C++ - 使用特定函数计算 x 和 y 的最终值 - C++ - Calculating final values of x and y with a specific function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM