Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
2.33/5 (5 votes)
See more:
Q: Array 1={1,2,3} Array 2={4,5,6}
2n-1=2(pow 3)-1=7
2n-1=2(pow 3)-1=7
7*7 =49

Output Need like that......... Any one can help me thanks
---------------------------------------------------------------------------------
C1=(1,4) C2=(1,5) C3=(1,6) C4=(1,4,5) C5=(1,4,6) C6=(1,5,6) C7=(1,4,5,6)

C8=(2,4) C9=(2,5) C10=(2,6) C11=(2,4,5) C12=(2,4,6) C13=(2,5,6) C14=(2,4,5,6)

C15=(3,4) C16=(3,5) C17=(3,6) C18=(3,4,5) C19=(3,4,6) C20=(3,5,6) C21=(3,4,5,6)

C22=(1,2,4) C23=(1,2,5) C24=(1,2,6) C25=(1,2,4,5) C26=(1,2,4,6) C27=(1,2,5,6) C28=(1,3,4)
C29=(1,3,5) C30=(1,3,6) C31=(1,3,4,5) C32=(1,3,4,6) C33=(1,3,5,6)

C34=(2,3,4) C35=(2,3,5) C36=(2,3,6) C37=(2,3,4,5) C38=(2,3,4,6) C39=(2,3,5,6) C40=(1,2,4,5,6) C41=(1,3,4,5,6) C42=(2,3,4,5,6)

C43=(1,2,3,4) C44=(1,2,3,5) C45=(1,2,3,6) C46=(1,2,3,4,5) C47=(1,2,3,4,6) C48=(1,2,3,5,6) C49=(1,2,3,4,5,6)

i try this code but it wrong....


C++
#include <iostream>
using namespace std;


int main() {
	int i,j;
		int arr1[]={1,2,3};
		int arr2[]={4,5,6};
	for (i=0; i<3; i++){
		for(j=0; j<3; j++)
			cout<<arr1[i]<<", "<<arr2[j]<<endl;
			cout<<arr1[i]<<", "<<arr2[i]<<", "<<arr2[i+1]<<"\n";
			cout<<arr1[i]<<", "<<arr2[i]<<", "<<arr2[i+2]<<"\n";
			cout<<arr1[i]<<", "<<arr2[i+1]<<", "<<arr2[i+2]<<"\n";
			cout<<arr1[i]<<", "<<arr2[i]<<", "<<arr2[i+1]<<", "<<arr2[i+2]<<"\n";

	}
	return 0;
}
Posted
Updated 2-Oct-13 3:41am
v5
Comments
joshrduncan2012 2-Oct-13 9:06am    
Why is it wrong?
Member 10310972 2-Oct-13 9:09am    
we need output like above...
joshrduncan2012 2-Oct-13 9:15am    
That doesn't tell me anything.

What output are you getting versus what you should be expecting?
Have you tried debugging it to see where the problem is? If so, what did you try?
Please "Improve Question" with that information.
Member 10310972 2-Oct-13 9:37am    
we need output like that
C1=(1,4) C2=(1,5) C3=(1,6) C4=(1,4,5) C5=(1,4,6) C6=(1,5,6) C7=(1,4,5,6)
C8=(2,4) C9=(2,5) C10=(2,6) C11=(2,4,5) C12=(2,4,6) C13=(2,5,6) C14=(2,4,5,6)
C15=(3,4) C16=(3,5) C17=(3,6) C18=(3,4,5) C19=(3,4,6) C20=(3,5,6) C21=(3,4,5,6)
C22=(1,2,4) C23=(1,2,5) C24=(1,2,6) C25=(1,2,4,5) C26=(1,2,4,6) C27=(1,2,5,6)
C28=(1,3,4) C29=(1,3,5) C30=(1,3,6) C31=(1,3,4,5) C32=(1,3,4,6) C33=(1,3,5,6)
C34=(2,3,4) C35=(2,3,5) C36=(2,3,6) C37=(2,3,4,5) C38=(2,3,4,6) C39=(2,3,5,6) C40=(1,2,4,5,6) C41=(1,3,4,5,6) C42=(2,3,4,5,6)

C43=(1,2,3,4) C44=(1,2,3,5) C45=(1,2,3,6) C46=(1,2,3,4,5) C47=(1,2,3,4,6) C48=(1,2,3,5,6) C49=(1,2,3,4,5,6)
Captain Price 2-Oct-13 9:14am    
looks interesting

This is an interesting question. The solution to this problem is much more complex than it sounds. It took me more than an hour to figure out an algorithm.

Here's the code :

C++
#include <iostream>
using namespace std;

//a helper function used by comb().
void print_arr(int *arr, size_t beg, size_t size)
{
	if(size < 1)return;

	for(int i=beg; i < size; i++)
	{
		cout << arr[i];
		cout << ",";
	}

}


void comb(int* a, int *b)
{
	int j=1;

	int al_pos = 0;
	int al_start = 1;

	int k=1;
	int bl_pos = 0;
	int bl_start = 1; 

	while(true)
	{

		if(j > 3)//j==4
		{
			
			al_pos++;
			j = al_pos + 1;

			if(al_pos > 1)//l_pos == 2
			{
				al_start++;
			}
			
		}


		k=1;
		bl_pos = 0;
		bl_start = 1; 

		while(true)
		{
			if(k > 3)//k==4
			{
			
				bl_pos++;
				k = bl_pos + 1;

				if(bl_pos > 1)//bl_pos == 2
				{
					bl_start++;
				}
			
			}

			if(al_start > 2)//l_start ==3
			{
				print_arr(a, 0, 2);
				cout << a[2] << ",";
				
			}
			else
			{
				print_arr(a, al_start-1, al_pos);
				cout << a[j-1] << ",";

			}

			if(bl_start > 2)
			{
				print_arr(b, 0, 2);
				cout << b[2] << endl;
				break;
			}

			print_arr(b, bl_start-1, bl_pos);
			cout << b[k-1] << endl;

			k++;
		}

		if(al_start > 2)
			break;

		j++;
		
	}
}


int main()
{
	int a[3] = {1, 2, 3};
	int b[3] = {4, 5, 6};


	comb(a, b);

        return 0;
}
 
Share this answer
 
v2
C++
int main()
{
	int i,j, k = 0;
	int arr1[]={1,2,3};
	int arr2[]={4,5,6};
	for (i=0; i<3; i++)
         {
		for(j=0; j<3; j++)
		{
             	    cout"C"<<k<<" = ("<<arr1[i]<<", "<<arr2[j]<<endl" )";  // you need to format your out put better.
		    cout<<arr1[i]<<", "<<arr2[i]<<", "<<arr2[i+1]<<"\n";
		    cout<<arr1[i]<<", "<<arr2[i]<<", "<<arr2[i+2]<<"\n";
		    cout<<arr1[i]<<", "<<arr2[i+1]<<", "<<arr2[i+2]<<"\n";
		    cout<<arr1[i]<<", "<<arr2[i]<<", "<<arr2[i+1]<<", "<<arr2[i+2]<<"\n";
 
	        }
	return 0;
         }//It appears that you missed a closing brace.
}
 
Share this answer
 

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