Inheration and Iteration

I need to create 2 programs that have the same function, the first one use inheration, and the second use iteration. can anyone help me?

and also can anyone explain is this iteration or inheration?
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
#include <iostream>
#include <string>

using namespace std;

int getDiff(int X, int Y) { // Tail Implementation
  X++; // Inc X
  if (X == Y)
    return 1;

  return (1+getDiff(X, Y));
}


void getDiff2(int X, int Y, int Diff = 0) {
  X++;
  Diff++;

  if (X != Y)
    getDiff2(X, Y, Diff);
  else
    cout << "Diff Was: " << Diff << endl;

  return;
}

void main(void) {

  cout << "Difference Between 1 & 7 Is: " << getDiff(1, 7) << endl;
  getDiff2(2, 8);
  getchar();
}
iamamateur wrote:
is this iteration or inheration?
neither. Just two different versions of recursion.

You can inherit from a class or struct. http://www.cplusplus.com/doc/tutorial/inheritance/
You can iterate over a container. http://www.cplusplus.com/reference/std/iterator/
neither. Just two different versions of recursion.


thanks.. but..
Oops.. My Bad.. It isn't Inheration and iteration, But Recursion and iteration..

So if my task is making two programs with Recursion and iteration each..
then, The Code above is Right about Recursion? thanks..
Last edited on
well, as I said. Just two recusions. I'd say that iteration means using loops (and maybe itarators) but both don't. So no iteration
can you give me sample codes? I am still beginner programmer.. so it is kinda hard for me to understand.. :)
Recursion is when a function contains calling itself under some circumstances.

Iteration is when a single piece of code (like functions) contain a loop to do calculation a given amount of times or when a condition is met.
Topic archived. No new replies allowed.