Edminas - 1 year ago 67
C++ Question

# 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] . How do I make it to a table with x, y, a columns and their values? 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. Check the replaced cout line and you'll see how to print the other values.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download