C++/Ploting points
Expert: Zlatko - 11/11/2010
QuestionI'm new to C++ and I have an assignment where I have to plot the point for f(mx+y)where m=1 thru 25 given x=10 and y=10 and I have to plot the point for f(mx+y^n) where n=1 thru 25 given m=1, x=10 and y=10 and they have to be in the same program using a header file, a .cpp file and a test.cpp file Please help I don't even know where to begin
AnswerHello
You can create a C function to calculate your expression like this:
/* f() = mx + y */
float function1(float m, float x, float y)
{
return m*x+y;
}
You can do the same with the other expression, but be sure to use the pow function to calculate y^n. In C, the ^ operator is not used for exponentiation. To use the pow function, you will need to add
#include <math.h>
to the top of your C++ file.
Read about the pow function and see some examples here:
http://www.cplusplus.com/reference/clibrary/cmath/pow/
You will also need
#include <iostream>
using namespace std;
so that you can easily get output using cout.
In main you can have a "for" loop to call function1, like this
int main()
{
int m;
for (m = 1; m <= 25; m++)
{
float result = function1(m, 10, 10);
cout << "function1 m=" << m << " result =" << result << endl;
}
}
That should be enough to get you started. Try to get the whole thing running using one test.cpp file. Once you have everything running, you can separate them into individual header and source files. I will show you how, but first you show me an attempt to put these ideas together into a program with one cpp file that compiles.
If you are confused about anything I've written here, please ask. I don't know how much time you have spent in class, so I might assume you know certain things, but I might be wrong.
Best regards
Zlatko