Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I understand that in the initial step till the base case it assigns 0 to all members of the array, but I don't understand what happens after that. It must then go from the base case to its top calling functions, but how it does and I don't understand also in what case the loop is traversed more than once. Thanks in advance.
C++
void k_string(char* A, int n, int k){
  if(n<1){
    printf("%s", A);
  }

  else{
    for(int j=0; j<k; j++){
      A[n-1]=j;
      k_string(A, n-1, k);
    }
  }
}


What I have tried:

I tried to go step-by-step writing on the paper, but as I don't understand the core logic, it makes no difference.
Posted
Updated 14-Jul-20 20:12pm

1 solution

Quote:
I understand that in the initial step till the base case it assigns 0 to all members of the array, but I don't understand what happens after that.

Did you missed that it is a recursive function ?
Launch the debugger and watch the code perform, it certainly will help to understand.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Member 13277493 14-Jun-19 2:24am    
thanks, I tried to understand using debugger but there were some problems I encountered, I will try again
Patrice T 14-Jun-19 2:38am    
look for a tutorial on your debugger.
Stefan_Lang 14-Jun-19 4:01am    
The most important parts about debugging are: setting breakpoints, examining the state of your program, and stepping through your code. If you're using VisualStudio (no matter which version), follow the links Patrice posted to learn about this. Otherwise try F1 or google to find help on the specific debugger you have.
[no name] 18-Jun-19 8:15am    
thanks to your solution I am able so solve my issues, thnx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900