Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <iostream>
#include<string>
using namespace std;
void func(char a[],string s,int st_ind,int length_of_string)
{
    if(s.length()==length_of_string)
    {
        cout<<s<<" ";
        return;
    }
    if(s.length()<length_of_string="" &&="" st_ind<length_of_string)
    {
        func(a,s+a[st_ind],st_ind,length_of_string);
        func(a,s+a[st_ind],st_ind+1,length_of_string);
        func(a,s,st_ind+1,length_of_string);
    }
}
int main() 
{
    int n;
    cin>>n;
    char a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++)
    {
        string s;
        func(a,s,0,i);
    }
    return 0;
}


What I have tried:

I feel like this is the correct solution but there is no output being displayed.
The answer has to be " a b aa ab ba bb " when there is an input of n = 2 and char array being [a,b].
Posted
Updated 18-Jun-19 10:19am
v5
Comments
Patrice T 18-Jun-19 12:27pm    
What are the ="" in your code ?
Richard MacCutchan 18-Jun-19 12:54pm    
I think the editor/formatter is misreading some control characters.
Raghurss 20-Jun-19 1:55am    
Yes! I'm sorry for that.
Stefan_Lang 19-Jun-19 11:32am    
When I fix the second if statement in func() (what PatriceT mentioned above), and replace the cin statements with static assignments (n=2; and a[i] = 'a'+i; respectively), I do get an output:
a a aa aa ab ab ab ab bb bb

If you don't get an output at all, you might want to check your input - this is the only difference in my version.

Of course, the output is not correct, but at least I get something.

P.S.: I changed the code back to use the original input statements and it works as well. Note that you need to confirm each character input with a separate [enter] key.
Raghurss 20-Jun-19 1:57am    
oh! yeah i'll try that. your output is at least better than my nothing. thanks

A give you a sample code which incidentally (?!) could be used as a building block for your program:
C++
#include <iostream>
#include <vector>
using namespace std;


void produce(const vector<char> & v, string s, size_t index)
{
  if ( index == v.size() )
  {
    cout << s << " ";
    return;
  }
  for ( auto x : v )
  {
    produce( v, s+x, index + 1);
  }
}


int main()
{

  string s{};
  vector<char> v{'a','b','c'};
  produce(v,s,0);
  cout << endl;
}
 
Share this answer
 
Comments
Raghurss 20-Jun-19 2:02am    
Thanks!!
CPallini 20-Jun-19 10:50am    
You are welcome.
Maciej Los 20-Jun-19 5:10am    
5ed!
CPallini 20-Jun-19 10:50am    
Thank you very much, Maciej.
Quote:
I feel like this is the correct solution but there is no output being displayed.

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
Raghurss 20-Jun-19 1:59am    
Thanks! I got to know there is something called debugger because of this! Thank you :)

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