calculate a function

Hello I'm a beginner
Can you help me to write a program to calculate a function while lead in "x" and "n"

In advance thank you.
What is the function supposed to evaluate?
Give me your email and I send a file to see the function.
You can post it here:

[code]
your function
[/code]
1
2
3
            n  x^2
f(x)=       ∑  ─
           k=1 k
Last edited on
just translate in C++

1
2
3
4
5
6
7
int f( int x, int n )
{
   int result=0;//Initialize the number which will hold the sum
   for ( int k=1; k<=n; k++ )// ∑
     result += x*x / k; // add x2/k to the sum
   return result;//return the result
}
I work with "Dev-C++ 4.9.9.2" and I can't start this code.May be help me little. I have a similar program but it is different
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
    const int N = 6;
    double A[N];
    double sum =0;
    
    for(int i=0;i<N;i++)
    {
            A[i] =1.0 / (2*(i+1));
            cout << A[i] << endl;
            sum += A[i];
            }
            
            cout << "sum = " << sum << endl;
            system("pause");
            return 0;
            }


This program that I want must be a something similar.
Last edited on
Note how Bazzy's example mirrors the form of f(x). It is much more clear and concise. Although, you may want result and the return value to be a double.
What can I do to start this a program in "Dev-C++ 4.9.9.2"May be I must write somethink.

1
2
3
4
5
6
7
int f( int x, int n )
{
   int result=0;//Initialize the number which will hold the sum
   for ( int k=1; k<=n; k++ )// ∑
     result += x*x / k; // add x2/k to the sum
   return result;//return the result
}
You should define the entry point ( int main() )

eg:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
int f( int x, int n )
{
   //...
}

int main()
{
    std::cout << "sum = "<< f ( /*a value for x*/, /*a value for n*/ );
    return 0;
}
Functions in C++ work exactly as functions in mathematics. f of x, takes one parameter x and returns a single value (the y value) for the given x. Learning about functions in C++ might help you build a better understanding of Calculus. That's pretty cool, right?
That won't do to work.Excuse but can you write exactly please, from biggining to end.White copy and paste to work.I'm sory for my ignorance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
int f( int x, int n )
{

   int result=0;//Initialize the number which will hold the sum
   for ( int k=1; k<=n; k++ )// ?
     result += x*x / k; // add x2/k to the sum
   return result;//return the result
 //...
}

int main()
{
    std::cout << "sum = "<< f ( /*a value for x*/, /*a value for n*/ ); // Here  write out mistake
    return 0;
}
This is getting a little bit ridiculous. Compile that source and it will tell you what is wrong. If you still have problems, post the compiler error and source again. Your problem is so trivial, it's hard to believe that you're putting any effort into this. Just give it a try; we have confidence in you.
std::cout << "sum = "<< f ( /*a value for x*/, /*a value for n*/ ); // Here write out mistake

HINT: function parameters, you have to enter numbers the compiler won't make them up for you.
Thank you for help I will try
My Discrete Math course had us evaluate a bunch of derivatives and integrals using different approximation techniques. It would have been awesome if our assignments were in C++ but, because there was no C++ prerequisite (some of the students were Math Ed.), we used Excel. We had to use a few cells for our values and copy them down about a thousand lines until the answers stabilized to x decimal places...it was lame--well, I guess it was alright; but it was no C++!
Last edited on
Thank you man.I hope be right.Because I nothing understand of C++ , and I just cope and paste your formula and it just not start, but I'm sure with a little more luck I well go to the end. I'm from Bulgaria and I,m a student in university
Topic archived. No new replies allowed.