#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;
}
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?
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.
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.
// 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;
}
@ 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.
#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;
}
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.
#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;
}
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?