0

I was just watching a video on youtube about an akm function and i tried to implement it.
I actually wrote a code and forgot to make spaces between the variables(for an easy reading), but the program didn't print anything but it kept on calculating.

I thought that a similar syntax would work just fine. Is there something I am doing wrong ?
Here is the code :

#include <bits/stdc++.h>
using namespace std;

int akm(int m,int n) {
    if(m==0) return n+1;
    else if(n==0) return akm(m-1,1);
    else return akm(m-1 , akm(m,n-1));
}

int main() {
  for(int i=0;i<6;i++)
      for(int j=0;j<6;j++) {
          cout<<i<<" "<<j ;
          cout<<akm(i,j);
      }
 }
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 3
    Unrelated: `#include using namespace std;` leads me to suggest you watch different videos. That combination of bad ideas can lead to astoundingly strange errors. Read [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) for more information on why you don't want to use either statement alone. – user4581301 Mar 20 '19 at 20:35
  • It looks okay. You say it's not printing anything at all? –  Mar 20 '19 at 20:35
  • Are you sure your program isn't just taking very long? You're not flushing on every write, so it will only flush when the program exits – KABoissonneault Mar 20 '19 at 20:37
  • YUP I did try it also on many platforms but as always no output – Salim Hertelli Mar 20 '19 at 20:37
  • Sorry to link my own answer as a duplicate (there are many more to find), that was just by chance findinf it via google. – πάντα ῥεῖ Mar 20 '19 at 20:38
  • I suspect it never gets to the `cout` statement. Good odds the recursion runs off the end of the stack. – user4581301 Mar 20 '19 at 20:38
  • I know that the program should take so long to exectue( and i mean sooo long) – Salim Hertelli Mar 20 '19 at 20:38
  • 2
    ack(4,2) exceeds the limits of an int. – stark Mar 20 '19 at 20:39
  • If the program is taking a while to complete, you should probably flush the stream on every print. `std::cout << i << " " << j << "\n" << std::flush;` or `std::cout << i << " " << j << std::endl;` – KABoissonneault Mar 20 '19 at 20:40
  • here is the video for those intrested : https://www.youtube.com/watch?v=i7sm9dzFtEI – Salim Hertelli Mar 20 '19 at 20:40
  • OK. Looks like the video wasn't the source of the bad advice about `bits` and `using namespace std;`. That was all C code, but wherever you got that opener from, be careful with it. If you find yourself with a multi-page crawl of inscrutable error messages, it should be the first thing to go. – user4581301 Mar 20 '19 at 20:51
  • And the program's crashing, on my PC at least, but I do get some data printed out first. That's Undefined Behaviour for you. On Alice's PC it works. On Bob's it doesn't. Except on Tuesday. – user4581301 Mar 20 '19 at 20:55

1 Answers1

0

You may need to explicitly flush the output stream or print a newline which may flush the buffer on some streams:

std::cout << std::flush;

std::cout << std::endl;
jspcal
  • 50,847
  • 7
  • 72
  • 76