Help with code

I need to change the count() so that it counts down till 0 but it should still remain a recursive function and should not be a for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
 using namespace std;
 void count(int counter)
 {

 if (counter == 0)
 return;
else
 {
 cout << "Calling counter(" <<counter << ")" << endl;
 count(--counter);
 cout << "counter(" << counter << ") processed "
<< endl;
 return;
 }
 }
 int main()
  {
 int i = 3;
 count(i);
 return 0;
}
What is the problem?
It should countdown as in

3
2
1
0
print the current number before calling the function recursively with decremented value.

Edit: You've already done this.
Calling counter(3)
Calling counter(2)
Calling counter(1)
counter(0) processed 
counter(1) processed 
counter(2) processed

Remove the "processed" print statements, and then add a print statement in the base case for 0.

PS: Even a simple program like yours is hard to read when you don't use proper indentation.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

void count(int counter)
{
 cout << "Calling counter(" <<counter << ")" << endl;
 if (counter > 0)
   count(--counter);
}

int main()
{
 int i = 3;
 count(i);
 return 0;
}
I don't know what I have to think about recursive functions. Even if it works as expected, I guess that it could be better to use a loop no? At first, it takes a lot of stack space compared to an iterative program. Second fact, it uses more processor time... In this example, I would have formulated the code in different way. After some tests, recursives functions seem to me unclever and irrelevant. Loops do/while and for++ have a better execution time :

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
#include <iostream>
#include <chrono>

void count(int counter)
{
    std::cout << "Calling counter(" << counter << ")" << std::endl;

    if (counter > 0)
        count(--counter);
}

int main()
{
    auto start = std::chrono::high_resolution_clock::now();

    int i = 1000;
    count(i);

    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);

    std::cout << "execution time : " << duration.count() << std::endl;

    return 0;
}


Calling counter(1000)
Calling counter(999)
Calling counter(998)
(...)
execution time : 108420


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <chrono>

void count(int i)
{
    std::cout << "Calling counter(" << i << ")" << std::endl;
}

int main()
{
    auto start = std::chrono::high_resolution_clock::now();

    for (int i = 1000; i >= 0; --i)
        count(i);

    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);

    std::cout << "execution time : " << duration.count() << std::endl;

    return 0;
}


Calling counter(1000)
Calling counter(999)
Calling counter(998)
(...)
execution time : 53940


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
#include <iostream>
#include <chrono>

void count(int i)
{
    std::cout << "Calling counter(" << i << ")" << std::endl;
}

int main()
{
    auto start = std::chrono::high_resolution_clock::now();

    int i = 1000;

    do {
        count(i);
        --i;
    } while (i >= 0);

    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);

    std::cout << "execution time : " << duration.count() << std::endl;

    return 0;
}


Calling counter(1000)
Calling counter(999)
Calling counter(998)
(...)
execution time : 57840
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

void count(int counter) {
	if (counter >= 0) {
		std::cout << counter << '\n';
		count(counter - 1);
	}
}

int main() {
	count(3);
}



3
2
1
0

Topic archived. No new replies allowed.