Recursively print all sentences that can be formed from list of word lists | C ++ program to print all possible sentences from a list of word list ; A recursive function to print all possible sentences that can be formed from a list of word list ; Add current word to output array ; If this is last word of current output sentence , then print the output sentence ; Recur for next row ; A wrapper over printUtil ( ) ; Create an array to store sentence ; Consider all words for first row as starting points and print all sentences ; Driver program to test above functions
#include <iostream> NEW_LINE #include <string> NEW_LINE #define R  3 NEW_LINE #define C  3 NEW_LINE using namespace std ; void printUtil ( string arr [ R ] [ C ] , int m , int n , string output [ R ] ) { output [ m ] = arr [ m ] [ n ] ; if ( m == R - 1 ) { for ( int i = 0 ; i < R ; i ++ ) cout << output [ i ] << " ▁ " ; cout << endl ; return ; } for ( int i = 0 ; i < C ; i ++ ) if ( arr [ m + 1 ] [ i ] != " " ) printUtil ( arr , m + 1 , i , output ) ; } void print ( string arr [ R ] [ C ] ) { string output [ R ] ; for ( int i = 0 ; i < C ; i ++ ) if ( arr [ 0 ] [ i ] != " " ) printUtil ( arr , 0 , i , output ) ; } int main ( ) { string arr [ R ] [ C ] = { { " you " , " we " } , { " have " , " are " } , { " sleep " , " eat " , " drink " } } ; print ( arr ) ; return 0 ; }