Note that there are some explanatory texts on larger screens.

plurals
  1. POCartesian Product of multiple arrays in C
    primarykey
    data
    text
    <p>I am able to achieve cartesian product of static number of arrays in C.But I would like to build a code dynamically taking the number of input arrays.Can someone shed some light how to do this "only using arrays".If it's not possible with arrays please suggest me other solution.Thank you.Here is my code below for cartesian product of 3 arrays.</p> <pre><code>#include&lt;stdio.h&gt; int main(void) { int array1[100]; int array2[100]; int array3[100]; int cartesian_array[100][100]; int m,n,o; int i,j; int p=0,q=0,r=0; int x,y,z; int cartesian_arr_row_len; int cartesian_arr_col_len; printf("Enter the size of first array:\n"); scanf("%d",&amp;m); printf("Enter the size of second array:\n"); scanf("%d",&amp;n); printf("Enter the size of third array:\n"); scanf("%d",&amp;o); printf("Enter the first array elements:\n"); for(i=0;i&lt;m;i++) scanf("%d",&amp;array1[i]); printf("Enter the second array elements:"); for(i=0;i&lt;n;i++) scanf("%d",&amp;array2[i]); printf("Enter the third array elements:"); for(i=0;i&lt;o;i++) scanf("%d",&amp;array3[i]); cartesian_arr_row_len=m*n*o; cartesian_arr_col_len=3; x=cartesian_arr_row_len/m; y=cartesian_arr_row_len/(m*n); z=o; for(i=0;i&lt;cartesian_arr_row_len;i++) { for(j=0;j&lt;cartesian_arr_col_len;j++) { if(j==0) { cartesian_array[i][j]=array1[p/x]; p++; } if(j==1) { cartesian_array[i][j]=array2[q/y]; q++; if(q&gt;=n*y) q=0; } if(j==2) { cartesian_array[i][j]=array3[r%z]; r++; } } } printf("The Cartesian Product of two arrays is:\n"); for(i=0;i&lt;cartesian_arr_row_len;i++) { for(j=0;j&lt;cartesian_arr_col_len;j++) { printf("%d\t",cartesian_array[i][j]); } printf("\n"); } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload