how to solve this linear funciton ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int w=0, x=0, k=0

int fn(int w)
{
  x+=3
  if (w > 2)
    k += gn(w-1)
  return w + x
}

int gn(int x)
{
  w+=2
  if (x > 0)
    k += fn(x-1)
  return w + x
}

fn(4);
k = ?

my try
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
#include <iostream>

int main()
{
	int w = 0, x = 0, k = 0;
	int gn(int x);
	int fn(int w);
	{
		x += 3;
		if (w > 2)
			k += gn(w - 1);
			return w + x;
	}

	int gn(int x);
	{
		w += 2;
		if (x > 0)
			k += fn(x - 1);
			return w + x;
	}

	fn(4);

	std::cout << "k is " << k << std::endl;
	return 0;
}



k = ?
What do you want to get out of this?
What problems are you having?
What should be the output?
Have you compile it?
What errors are you getting?

Please, be specific. Contrary to popular belief, humans are the ones monitoring this forum.
want to get out of this value of k

getting error Severity Code Description Project File Line
Warning C4627 '#include <iostream>': skipped when looking for precompiled header use

Severity Code Description Project File Line
Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
closed account (48T7M4Gy)
The reason why you get no output is because both your functions are returning from main back to the system. Here your program ends at line 12. It never gets to line 25. In short your functions should not be defined within main().

I wish you luck because all your variables are a mess.

PS: no manner of #includes will help you. :)
Last edited on
Just to elaborate on what kemort said, your lines 7-13 appear to be attempting to define function fn, in reality, line 7 declares that the function exists. Then lines 8-13 are a block of code, still within the main() function. When it hits line 12, the main function exits.
something like this but i have somewhere bug it print out wrong number
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
29
30
31
32
33
34
35
36
37
// ConsoleApplication19.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <stdio.h>
#include <iostream>

int h = 0, e = 0, k = 0;
int gn(int e);
int fn(int h);
int fn(int h)
{
	e +=2;
	if (h > 1)
		k += gn(h - 1);
	return(h + e);
}

int gn(int e)
{
	h += 2;
	if (e > 2)
		k += fn(e - 1);
	return h + e;
}

int main()
{
	int h = 0, e = 0, k = 0;



	std::cout << fn(4);

	return 0;
}
closed account (48T7M4Gy)
Ok, that's a start, but tell me, what number is the right number. 8 is not nice? :(
when i change value of int gn final number dont change i dont know what to do
@ OP, I placed some cout statements in your code to give you a basic idea on how to break your code apart to figure out what is the output of the functions.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <iostream>

int h = 0, e = 0, k = 0;
int gn(int e);
int fn(int h);
int fn(int h)
{
	std::cout << "Function FN Called\n";
	e += 2;
	if (h > 1)
		k += gn(h - 1);

	std::cout << "Function FN Value: " << h + e << std::endl;
	std::cout << "Function FN End\n";
	return(h + e);
}

int gn(int e)
{
	std::cout << "Function GN Called\n";
	h += 2;
	if (e > 2)
		k += fn(e - 1);

	std::cout << "Function GN Value: " << h + e << std::endl;
	std::cout << "Function GN End\n";
	return h + e;
}

int main()
{
	//int h = 0, e = 0, k = 0;


	std::cout << "Function MAIN Called\n";
	//std::cout << fn(4);
	std::cout << "Function MAIN Value: " << fn(4) << std::endl;
	std::cout << "Function MAIN End\n";
	return 0;
}
Thanks a lot man but similiar function result should be 22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int a=0, m=0, k=0

int fn(int a)
{
  m+=2
  if (a > 0)
    k += gn(a-1)
  return a + m
}

int gn(int m)
{
  a+=3
  if (m > 1)
    k += fn(m-1)
  return a + m
}

fn(4);
k = ?


http://snap.ashampoo.com/uploads/2015-10-18/aw8cyGIY.png
I was trying to see the values on each function breakdown. Hopefully, someone more experienced will jump in to help you.
closed account (48T7M4Gy)
If you aren't getting the result you reckon is the right answer I would print out the program listing and against each line write down the variable values alongside each line as you follow step by step.

If you don't get 8 I will be very surprised.
Last edited on
closed account (48T7M4Gy)
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
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using std::cout;
using std::endl;

int h = 0, e = 0, k = 0;

int gn(int e);

int fn(int h)
{
    cout << "B " << h << ' ' << e << ' ' << k << endl;
	e +=2;
	cout << "C " << h << ' ' << e << ' ' << k << endl;
	if (h > 1)
		k += gn(h - 1);
	cout << "F " << h << ' ' << e << ' ' << k << endl;
	return(h + e);
}

int gn(int e)
{
	cout << "D " << h << ' ' << e << ' ' << k << endl;
	h += 2;
	cout << "E " << h << ' ' << e << ' ' << k << endl;
	if (e > 2)
		k += fn(e - 1);
    cout << "G " << h << ' ' << e << ' ' << k << endl;
	return h + e;
}

int main()
{
	int h = 0, e = 0, k = 0;
	
	cout << "A " << h << ' ' << e << ' ' << k << endl;
	std::cout << fn(4) << " THE END" << endl;
	cout << "H " << h << ' ' << e << ' ' << k << endl;

	return 0;
}
Last edited on
I suspect you're confusing your global and local variables which have the same name. What exactly is the mathematical function you're trying to implement?
Thanks a lot man but similiar function result should be 22

Similar, but not identical. Check your code very carefully for errors in the function.
Thanks a lot kemort you are a beast.
Topic archived. No new replies allowed.