Need help with function

Trying to find the output off the statements
cout << secret (3, 6) << endl;
cout << secret (5, -4) << endl;

The function given is

1
2
3
4
5
6
7
8
9
10
11
int secret(int m, int n)

{
	int temp = 0;

	for (int i = 1; i < abs(n); i++)
		temp = temp + i * m;

		return temp;
		
}

Last edited on
What do you mean by "help"? You do have a clear syntax error on line 15. Logic errors are apparent, but without knowing your actual goal unverified.
closed account (o3hC5Di1)
Hi there,

Welcome to the forums. Please state your question clearly and specifically, just copying your code to us does not tell us much. Are you getting compilation errors? Does the program compile but are you getting runtime errors? If so, which errors are you getting?

Also, please wrap your code within [code][/code]-tags in order to make it more readable.

All the best,
NwN
well just a quick look at it and I notice on the line for (int i = 1; i < abs(n); there should be a ")" after abs(n) to make it look like (int i = 1; i < abs(n));as to close your first parenthesis
Trying to find the output off the statements
cout << secret (3, 6) << endl;
cout << secret (5, -4) << endl;

The function given is

1
2
3
4
5
6
7
8
9
10
11
int secret(int m, int n)

{
	int temp = 0;

	for (int i = 1; i < abs(n); i++)
		temp = temp + i * m;

		return temp;
		
}

The function looks syntactically ok and so do the function calls. What is the problem?
Have to find the output of the statements below after add them into the function
cout << secret (3, 6) << endl;
cout << secret (5, -4) << endl;


What would the output be after I print.
Last edited on
Rephrased: I have this piece of code. I have these input. I cannot think through what the code will do with the input. I cannot compile and run the program to see what the code does spit out as output.

Proceed in small steps. Take the first input (3,6). How many times the body of the loop will execute, i.e. which values the i will have?

Math hint:
a*b + a*c = a * (b+c)
Topic archived. No new replies allowed.