function F(n,m)

Hi,

I'm a c++ Beginner.

I need someone to tell me how to write a function for F(n,m)= m*(m+1)*...*n,
if m<=n?

if you don't mind I need an explanation for this function.


that's my try. I don't know how to do it honestly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>

using namespace std;

int main ()
{
int m;
int n;

for (int i = 0 ; i <= n ; i++)
{
if (m <= n)
{
m = m+1;
}
}
return 0;
}


please help me with that.

thanks ,,
The function is supposed to multiply the values together. You don't have any multiplication at all. Also, you should write a separate function rather than just put the code inside main().

The initial value of i in the loop should not be zero.
The test if (m <= n) should be done before the start of the for loop. Think about what would happen to the loop if m was greater than n.

Perhaps you should consider some examples of what this means:
m*(m+1)*...*n

Let's say for example m = 4 and n = 7
then the function should calculate the result of 4 * 5 * 6 * 7 giving the result 840.

Tutorial on functions:
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Thanks for helping me.

Please check the code whether is correct or not :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
int i;
int n;
int m = 1;
cout <<" Enter Number : ";
cin >> n;
	for(i=1; i <= n; i = i+1){

     cout << i <<endl;
	m = m * (m + i);
}
	 
	 cout <<"the summiation is = "<<m<<endl;
	
	system("pause");
}
Well, I edited my original post more than once to give more detail, I'm not sure you took on board all of the information.

So far your code asks the user to enter the value of n. That's halfway there. You also need to get a value for m.
Thanks for helping.

I tried to do it, and I think I got the right way :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
int n;
int m = 1;
cout <<" Enter N Number : ";
cin >> n;
	for(int i=1; i <= n; i++){

     cout << i <<endl;
	m = m * i;
}
	 cout <<"The Total Multiplication = "<<m<<endl;
	
	system("pause");
}
I still don't see where you get the value of m?

I'm not sure whether we are discussing the same question. This is the problem I was trying to solve:
how to write a function for F(n,m)= m*(m+1)*...*n,
if m<=n?

That would go something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int f (int n, int m)
{
	// do some processing with m and n here
	return result;
}

using namespace std;

int main() 
{
	int n, m;
	cout << "Enter first number: ";
	cin >> n;
	
	cout << "Enter second number: ";
	cin >> m;
	
	
	cout << "Answer is: " << f(n, m) << endl;
	
	return 0;
}

Your task is to fill in the details of function f(n, m) (lines 3 through 7). What you have so far is leading in that direction but is still incomplete.
Last edited on
Keep in mind you're still missing the "m" part of the equation, as @Chervil mentioned. What if your teacher wanted m to be 5, and n to be 9?

Hint: you need a cin >> m somewhere.
Last edited on
Hi, guys.

How about now?

I'm a little bit confused with the logic; however, I'm trying to use me way to finish it up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;

int f(int m, int n)
{
	int x = 1;
	for (int i = 0; i <= n; i++)
	{
		x = x * (m + i);
	}
	return x;
}

void main()
{
	int m;
	int n;
	cout <<" M "<<endl;
	cin >> m;
	cout<<" N "<<endl;
	cin >> n;

	int x = f(m, n);

	cout << x << endl;

	system("Pause");
}
Last edited on
Well..I'm not sure if that's quite the logic you were going for, because when I run it in my end, with sample input m = 5 and n = 9, I get -662538496

What are the exact instructions your professor gave you? Because your first post and your last post (in terms of the logic in your code) are saying two very different things.
That was the question :
( write a function for F(n,m)= m*(m+1)...*n, if m<=n )
Ok so you'll want something like

1
2
for( int a = 0; a <= (n-m); ++a )
    m *= (m+a);


Now comes the interesting case. What happens when m > n? What do you do from there? Or does your professor not care about that?
He does not care if m>n. He just wants if m<=n
Realized I made a mistake in my earlier code. What you'll actually is this:

1
2
3
4
5
int x = 1;
for( int a = 0; a <= int(n-m); ++a )
{
	x *= (m+a);
}


I'll leave the rest up to you to figure out.
Last edited on
Topic archived. No new replies allowed.