C++/display number structure??
Expert: vijayan - 5/16/2009
QuestionQUESTION: In my array
arr={10,20,30,40}
i want to display output like
10
20
30
40
10+20
10+30
10+40
20+30
20+40
30+40
10+20+30
10+20+40
20+30+40
10+20+30+40
[code]
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[] = {10,20,30,40};
//when i was use these two array
/*int arr[] = {10,20,30,40,50,60,70,80,90,100};
or
int arr[] = {10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100};
*/
int count=sizeof(arr)/sizeof(int);
//here i got 4 or 10 or 20
int i,j;
for (i=0;i<count;i++) // this loop is ok
{
printf("%d",arr[i]);
printf("\n");
//problem here
for(j=i+1;j<count;j++)
{
printf("%d + %d",arr[i],arr[j]); printf("\n");
}
/* i was asking about this..problem is how to make this dynamic??
or display all numbers*/
}
getch();
}
how to make it dynamic for any no of array i.e. any length of array??
ANSWER: In a C++ program, prefer using C++ functions for io.
Avoid <conio.h>, it has never been a standard C or C++ header ;
You need to write multiple (nested) loops, for each combination 1, 2, 3, 4
#include<iostream>
int main()
{
int arr[] = { 10, 20, 30, 40, 50, 60 } ;
const int N = sizeof(arr) / sizeof( arr[0] ) ;
for( int i = 0 ; i < N ; ++i ) std::cout << arr[i] << '\n' ;
for( int i = 0 ; i < N-1 ; ++i )
for( int j = i+1 ; j < N ; ++j )
std::cout << arr[i] << " + " << arr[j] << '\n' ;
for( int i = 0 ; i < N-2 ; ++i )
for( int j = i+1 ; j < N-1 ; ++j )
for( int k = j+1 ; k < N ; ++k )
std::cout << arr[i] << " + " << arr[j] << " + " << arr[k] << '\n' ;
for( int i = 0 ; i < N-3 ; ++i )
for( int j = i+1 ; j < N-2 ; ++j )
for( int k = j+1 ; k < N-1 ; ++k )
for( int m = k+1 ; m < N ; ++m )
std::cout << arr[i] << " + " << arr[j] << " + " << arr[k] << " + " << arr[m] << '\n' ;
std::cin.get() ;
}
---------- FOLLOW-UP ----------
QUESTION: Hello Sir,
for 6 numbers i.e. int arr[] = { 10, 20, 30, 40, 50, 60 } ;
Out put is like..
10
20
30
40
50
60
10+20
10+30
10+40
10+50
10+60
20+30
20+40
20+50
20+60
30+40
30+50
30+60
40+50
40+60
50+60
10+20+30
10+20+40
10+20+50
10+20+60
10+30+40
10+30+50
10+30+60
10+40+50
10+40+60
10+50+60
20+30+40
20+30+50
20+30+60
20+40+50
20+40+60
20+50+60
30+40+50
30+40+60
30+50+60
40+50+60
10+20+30+40
10+20+30+50
10+20+30+60
10+20+40+50
10+20+40+60
10+20+50+60
10+30+40+50
10+30+40+60
10+30+50+60
10+40+50+60
20+30+40+50
20+30+40+60
20+30+50+60
20+40+50+60
30+40+50+60
10+20+30+40+50
10+20+30+40+60
10+20+30+50+60
10+20+40+50+60
10+30+40+50+60
20+30+40+50+60
10+20+30+40+50+60
so i was asking for looping structure and dynamic solution.. i.e. for different length of array it becomes as i given format..
So how to do this in dynamic way ??
AnswerThe simplest way is to write a recursive function. for example:
#include <iostream>
#include <string>
#include <sstream>
#include <set>
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[] = { 10, 20, 30, 40, 50, 60 } ;
enum { N = sizeof(arr) / sizeof( arr[0] ) } ;
for( int num_comb = 1 ; num_comb <= N ; ++num_comb )
print_combinations( "", arr, N, 0, num_comb ) ;
}