Hi,

this is my CPP code which i want to convert to PHP

#include <iostream>
#include <string>
#include <sstream>
#include <set>
#include<stdio.h>
#include<conio.h>

void print_combinations( std::string prefix, const int* arr, std::size_t N,
                         std::size_t start, std::size_t num_comb, bool first_time = true )
{
   static std::set< std::string > prefixes ;
   if( first_time ) prefixes.clear() ;

   if( num_comb == 1 )
   {
       if( prefixes.insert(prefix).second )
       for( std::size_t i = start ; i < N ; ++i )
       {
           std::cout << prefix << arr[i] << '\n' ;
       }
   }
   else
   {

       std::string pref ;
       for( std::size_t i = start ; i < N - num_comb + 1 ; ++i )
           for( std::size_t j = i  ; j < N - num_comb + 2 ; ++j )
           {
               {
                   std::ostringstream stm( prefix ) ;
                   stm << prefix << arr[j] << " + "  ;
                   pref = stm.str() ;
               }
               print_combinations( pref, arr, N, j+1, num_comb-1, false ) ;
           }
   }
}

int main()
{
  int arr[] = {9, 6, 3, 2} ;
  enum { N = sizeof(arr) / sizeof( arr[0] ) } ;

  for( int num_comb = 1 ; num_comb <= N ; ++num_comb )
      print_combinations( "", arr, N, 0, num_comb ) ;
      getch();
}

Please help me in solving my problem ?
how to do this??

Recommended Answers

All 4 Replies

in which line(s) you are faceing problem??

Hi,
My problem is in converting function

void print_combinations( std::string prefix, const int* arr, std::size_t N,
                         std::size_t start, std::size_t num_comb, bool first_time = true )
{
   static std::set< std::string > prefixes ;
   if( first_time ) prefixes.clear() ;

   if( num_comb == 1 )
   {
       if( prefixes.insert(prefix).second )
       for( std::size_t i = start ; i < N ; ++i )
       {
           std::cout << prefix << arr[i] << '\n' ;
       }
   }
   else
   {

       std::string pref ;
       for( std::size_t i = start ; i < N - num_comb + 1 ; ++i )
           for( std::size_t j = i  ; j < N - num_comb + 2 ; ++j )
           {
               {
                   std::ostringstream stm( prefix ) ;
                   stm << prefix << arr[j] << " + "  ;
                   pref = stm.str() ;
               }
               print_combinations( pref, arr, N, j+1, num_comb-1, false ) ;
           }
   }
}

I am not a c++ guy but since the languages are similar I thought I'd give it a try. There are a few things I don't know yet, they are commented. If you can explain what they do, I can probably tell you the equivalent in php.

function print_combinations( $prefix,$arr,$n,$start,$num_comb,$first_time=true ) {
	static $prefixes;
	if ( $first_time ) {
		$prefixes = '';
	}
	if ( $num_comb == 1 ) {
		if ( /*whatever the insert and .second do*/ ) {
			for( $i = $start; $i < $n; ++$i ) {
				echo "{$arr[$i]}\n";
			}
		}
	}
	else {
		$pref = '';
		for( $i = $start; $i < ( $n - ( $num_comb + 1 ) ); ++$i ) {
			for( $j = $i; $j < ( $n - ( $num_comb + 2 ) ); ++$i ) {
				//whatever stm does to prefix variable
				print_combinations( $pref,$arr,$n,$j+1,$num_comb-1,false );
			}
		}
	}
}

i modified my code but not getting correct output

function print_combinations($prefix,$arr,$n,$start,$num_comb,
$first_time = true){
   if($first_time) $prefix='';
   if($num_comb=="0"){

       for($i = $start ; $i < $n ; $i++ )
       {
           echo  $arr[$i];echo "<BR>";
       }
   }
   else
   {
       $pref='';
      for($i = $start ; $i < $n - $num_comb + 1 ; ++$i )
          {
           for($j = $i  ; $j < $n - $num_comb + 2 ; ++$j )
           {
                echo $arr[$j]."+";
                                $pref.=$arr[$j];
                print_combinations($pref, $arr, $n, $j+1, $num_comb-1,
false ) ; echo "<BR>";
           }

      }

  }

}

$arrR = array(9, 6, 3, 2);

$n = sizeof($arrR) / sizeof($arrR[0]);

for($num_comb = 0 ; $num_comb<$n ; ++$num_comb ){
  print_combinations( "", $arrR, $n, 0, $num_comb,'') ;

please tell me what's wrong in this code..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.