Number of paths in matrix with obstacles - c

so the question is about printing all the paths from top left to bottom right of a matrix. the movement allowed are down, right and diagonal. this has to be applied with obstacles, i.e if an obstacle is present then that path should not be printed.
So I wrote this code and if run the output is
1 4 7 8 9
1 4 5 8 9
1 4 5 8 9
1 4 5 9
1 4 8 9
1 5 8 9
1 5 8 9
1 5 9
line 3 and 7 the last second value needs to be 6. but instead, it's 8.
Any ideas?
TIA
#include<stdio.h>
#include<stdlib.h>
void paths(int *mat, int i, int j, int n, int *path, int val)
{
int k,l;
if (i == n - 1)
{
for(k = j; k < n; k++)
{path[val + k - j] = *((mat + i*n) + k);}
for(l = 0; l < val + n - j; l++)
{if(path[l] == 0){return;}}
for(l = 0; l < val + n - j; l++)
{
printf("%d ", path[l]);
}printf("\n");
return;
}
if (j == n - 1)
{
for(k = j; k < n; k++)
{path[val + k - i] = *((mat + k*n) + j);}
for(l = 0; l < val + n - j; l++)
{if(path[l] == 0){return;}}
for(l = 0; l < val + n - i; l++)
{
printf("%d ", path[l]);
}printf("\n");
return;
}
path[val] = *((mat + i*n) + j);
paths(mat, i+1, j, n, path, val+1);
paths(mat, i, j+1, n, path, val+1);
paths(mat, i+1, j+1, n, path, val+1);
}
void printAll(int *mat, int n)
{
int *path = malloc((2*n) * sizeof(int));
paths(mat, 0, 0, n, path, 0);
}
int main()
{
int n,i,j;
printf("Enter the size of the grid\n");
scanf("%d", &n);
int mat[n][n];
int count = 1;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
mat[i][j] = count; count = count + 1;
}
}
printf("Enter the grid points that are offsets\n");
int obs[n*n][2];i=0;
while(1 == 1)
{
scanf("%d%d", &obs[i][0], &obs[i][1]);
mat[obs[i][0]][obs[i][1]] = 0;
if(obs[i][0] == -1 || obs[i][1] == -1){break;}
i = i + 1;
}
printf("The paths for the robot are\n");
printAll(*mat, n);
}

To get the correct output, change the for loop of the paths() function to:
for(k = i; k < n; k++)
{path[val + k - j] = *((mat + i*n) + k);}

Related

Calculating and printing the sums of the diagonals of a matrix

This is a program that is supposed to calculate the sum of all the diagonals in the matrix and then print them out.
ex. if the matrix is
1 2 3 4 5
2 3 4 5 6
0 1 1 2 5
5 5 5 5 5
7 8 9 7 7
the output should be
17 13 13 10 5
15 17 13 13 10
14 15 17 13 13
13 14 15 17 13
7 13 14 15 17
#include <stdio.h>
int main()
{
int n, sum=0, i, j, sub_i, sub_j, sub1_i, sub1_j;
scanf("%d ", &n);
int array1[n][n];
for(i=0;i<n;i++){
for(j=0; j<n; j++){
scanf("%d", &array1[i][j]);
}
}
for(i=0; i<n; i++){
for(j=0; j<n; j++){
sub_i=i;
sub_j=j;
sub1_i=i;
sub1_j=j;
sum=0;
if(j>i){
while(sub_j<n){
sum+=array1[sub_i][sub_j];
sub_i++;
sub_j++;
}
while(sub_j<n){
array1[sub_i][sub_j]=sum;
sub1_i++;
sub1_j++;
}
}
if(i>j){
while(sub_i<n){
sum+=array1[sub1_i][sub1_j];
sub_i++;
sub_j++;
}
while(sub1_i<n){
array1[sub1_i][sub1_j]=sum;
sub1_i++;
sub1_j++;
}
}
}
}
for(i=0; i<n; i++){
for(j=0; j<n; j++){
printf("%d ", array1[i][j]);
}
printf("\n");
}
return 0;
}
When i run the program it prints the array as if no value was assigned to the matrix. Can someone point out what is happening?
Quoting the comment by Weather Vane:
The program alters the array it is examining — see array1[sub_i][sub_j]=sum; — and then prints incorrect values, since you can't correctly sum the diagonals of an array that is changing.
The OP already realize that
... what you are telling me is to assign the values to another array and print that.
Yes, that is way easier:
#include <stdio.h>
int main(void)
{
int n;
// Checking the input is always a good idea, but you
// may prefer something less brutal, in case of error
if (scanf("%d", &n) != 1 || n < 1)
return 1;
int mat[n][n];
for (int i = 0; i < n; ++i) {
for (int j= 0; j < n; ++j) {
if (scanf("%d", &mat[i][j]) != 1)
return 1;
}
}
// Calculate and store the sum of the diagonals. Note that
// it could be done in the previous loop, but it may be better
// to refactor those snippets into separate functions.
int diagonals[2 * n + 1];
for (int i = 0; i < 2 * n + 1; ++i)
diagonals[i] = 0; // consider 'memset' instead of this loop
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
diagonals[n + i - j] += mat[i][j];
}
}
// Now print the correct values in their position
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
printf("%4d", diagonals[n + i - j]);
}
printf("\n");
}
return 0;
}
Testable HERE.
You can do like the follow:
#include <stdio.h>
#define N 5
int main()
{
int array[N][N] = {
1, 2, 3, 4, 5,
2, 3, 4, 5, 6,
0, 1, 1, 2, 5,
5, 5, 5, 5, 5,
7, 8, 9, 7, 7};
for(int i = 1; i < N; ++i)
for(int j = 1; j < N; ++j)
array[i][j] += array[i - 1][j - 1];
for (int i = 0; i + 1 < N; ++i)
for (int j = 0; j + 1 < N; ++j)
if (i == j)
array[i][j] = array[N - 1][N - 1];
else if (i > j)
array[i][j] = array[N - 1][N - 1 - i + j];
else
array[i][j] = array[N - 1 - j + i][N - 1];
for (int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j)
printf("%d ", array[i][j]);
printf("\n");
}
return 0;
}

Sort main diagonal of array by Shaker sort

So that's my task. I don't know how to realize it in 2D.
All variables (i, j, L, R, etc. are integer)
while (L < R)
{
for(i, j = L; i < R; i++, j++)
{
if (a[i][j] > a[i + 1][j + 1])
{
temp = a[i][j];
a[i][j] = a[i + 1][j + 1];
a[i + 1][j + 1] = temp;
k = i;
}
}
R = k;
for(i, j = R - 1; i >= L; i--, j--)
{
if(a[i][j] > a[i + 1][j + 1])
{
temp = a[i][j];
a[i][j] = a[i + 1][j + 1];
a[i + 1][j + 1] = temp;
k = i;
}
}
L = k + 1;
}
I tried this code but I think that something is wrong with that.
Input:
1 10 5
4 0 8
8 18 3
Output: (That should be)
0 10 5
4 1 8
8 18 3
But current Output is
1 10 5
4 0 8
8 18 3
A function to swap values using call by reference.
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
A function implementing shaker sort
void ShakerSort(int a[], int n)
{
int i, j, k;
for(i = 0; i < n;)
{
// First phase for ascending highest value to the highest unsorted index.
for(j = i+1; j < n; j++)
{
if(a[j] < a[j-1])
swap(&a[j], &a[j-1]);
}
// Decrementing highest index.
n--;
// Second phase for descending lowest value to the lowest unsorted index.
for(k = n-1; k > i; k--)
{
if(a[k] < a[k-1])
swap(&a[k], &a[k-1]);
}
// Incrementing lowest index.
i++;
}
}
The main function looks like this
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
ShakerSort(arr, n);
// Printing the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}
The above code is written in c++ refer it and make the changes according to your requirment

what is the better way to loop this problem?

#include <stdio.h>
int main()
{
int arr[9][9];
int i = 0, x = 10;
for (int i = 0, j = 0; j <= 8; j++) {
x++;
arr[i][j] = x;
}
for (int j = 8, i = 1; i <= 8; i++) {
x++;
arr[i][j] = x;
}
for (int i = 8, j = 7; j >= 0; j--) {
x++;
arr[i][j] = x;
}
for (int j = 0, i = 7; i >= 1; i--) {
x++;
arr[i][j] = x;
}
for (int i = 1, j = 1; j <= 7; j++) {
x++;
arr[i][j] = x;
}
for (int j = 7, i = 2; i <= 7; i++) {
x++;
arr[i][j] = x;
}
for (int i = 7, j = 6; j >= 1; j--) {
x++;
arr[i][j] = x;
}
for (int j = 1, i = 6; i >= 2; i--) {
x++;
arr[i][j] = x;
}
...
arr[4][4] = x + 1;
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 8; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
getch();
}
so I have this program, and I know you can loop it but how ? been sitting for an hour thinking and nothing came to my mind. By the way, the task is to append a matrix like on picture. Does anyone know to do it ? Maybe use some complex for loop
Here's one way to do it:
int arr[9][9] = {0};
int x = 0, i = 0, j = 0, vi = 0, vj = 1;
do {
++x;
arr[i][j] = x;
{
int ii = i+vi;
int jj = j+vj;
if (ii < 0 || ii >= 9 || jj < 0 || jj >= 9 || arr[ii][jj] != 0) {
if (vi != 0) {
vj = -vi;
vi = 0;
} else {
vi = vj;
vj = 0;
}
}
}
i = i+vi;
j = j+vj;
} while (arr[i][j] == 0);
Live on Coliru
Here's another way:
int arr[9][9] = {0};
int x = 0, i = 0, j = 0, vi = 0, vj = 1, lk = 8;
while (lk > 0) {
for (int k = 0; k < lk; ++k) {
++x;
arr[i][j] = x;
i += vi;
j += vj;
}
vi = vj;
vj = 0;
for (int k = 0; k < lk; ++k) {
++x;
arr[i][j] = x;
i += vi;
j += vj;
}
vj = -vi;
vi = 0;
if (vj > 0) {
++i;
++j;
lk -= 2;
}
}
arr[9/2][9/2] = x+1; // Only if odd dimensions
Live on Coliru
And here is yet another:
int arr[9][9] = {0};
int i = 0, lk = 8, x = 1;
while (lk > 0) {
for (int k = 0; k < lk; ++k) {
arr[i][i+k] = x + k;
arr[i+k][lk+i] = x + lk + k;
arr[lk+i][lk+i-k] = x + 2*lk + k;
arr[lk+i-k][i] = x + 3*lk + k;
}
x += 4*lk;
lk -= 2;
++i;
}
arr[9/2][9/2] = x; // Only if odd dimensions
Live on Coliru
Here is the "straight forward" option with for loops:
#include <stdio.h>
#define N 5
int main(void) {
int i,j,dim;
int matrix[N][N];
// init and print the matrix
for (i=0; i < N; i++)
{
for (j=0; j< N; j++)
{
matrix[i][j] = i*N + j;
printf("%2d ", matrix[i][j]);
}
printf("\n");
}
printf("\n");
// perform spiral print
for (dim = 0; dim < (N+1)/2; dim++)
{
// set initial i and go till the "last column"
i = dim;
for (j = dim; j < N - dim; j++)
{
printf("%2d ", matrix[i][j]);
}
printf("\n");
// bring back i and j to the proper coordinate
// and move down to the "last row"
j--;i++;
for (; i < N - dim; i++)
{
printf("%2d ", matrix[i][j]);
}
printf("\n");
// bring back i and j to the proper coordinate
// and move back to the "first column"
i--;j--;
for (; j >= dim; j--)
{
printf("%2d ", matrix[i][j]);
}
printf("\n");
// bring back i and j to the proper coordinate
// and move up to the "first row"
j++;i--;
for (; i > dim; i--)
{
printf("%2d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
The output, as can be seen here is
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
0 1 2 3 4
9 14 19 24
23 22 21 20
15 10 5
6 7 8
13 18
17 16
11
12
==========================================================================
Looks like I misunderstood the question but the step from "printing" clockwise to "setting" clockwise is really small. Here is the setting flow:
#include <stdio.h>
#define N 5
int main(void) {
int i,j,dim, val = 1;
int matrix[N][N];
// perform spiral print
for (dim = 0; dim < (N+1)/2; dim++)
{
// set initial i and go till the "last column"
i = dim;
for (j = dim; j < N - dim; j++)
{
matrix[i][j] = val++;
}
// bring back i and j to the proper coordinate
// and move down to the "last row"
j--;i++;
for (; i < N - dim; i++)
{
matrix[i][j] = val++;
}
// bring back i and j to the proper coordinate
// and move back to the "first column"
i--;j--;
for (; j >= dim; j--)
{
matrix[i][j] = val++;
}
// bring back i and j to the proper coordinate
// and move up to the "first row"
j++;i--;
for (; i > dim; i--)
{
matrix[i][j] = val++;
}
}
// print the matrix
for (i=0; i < N; i++)
{
for (j=0; j< N; j++)
{
printf("%2d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
The output as shown here is
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
My solution. Using an "object" struct strangeite_s (from strange ite-rator). It allows ease reusing on different arrays and probably could be even rescaled to support n-dimensional arrays.
The strangeite is initialized using _init function with specified dimensions sizes of an 2d array. Each time _loop is evaluated, the x and y positions are updated and the condition for loop end is checked (and returned). The _inc function should be called on each increment of the iterator.
#include <stdio.h>
#include <limits.h>
#include <stddef.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
struct strangeite_s {
size_t steplen[2];
size_t idx[2];
size_t cursteplen;
int direction;
};
void strangeite_init(struct strangeite_s *t, size_t max_x, size_t max_y)
{
assert(t != NULL);
t->steplen[0] = max_y;
t->steplen[1] = max_x;
memset(t->idx, 0, sizeof(t->idx));
t->direction = 0;
t->cursteplen = t->steplen[0];
}
bool strangeite_loop(const struct strangeite_s *t, size_t *x, size_t *y)
{
if (x) *x = t->idx[0];
if (y) *y = t->idx[1];
for (size_t i = 0; i < sizeof(t->steplen)/sizeof(t->steplen[0]); ++i) {
if (t->steplen[i] == 0) {
return false;
}
}
return true;
}
void strangeite_inc(struct strangeite_s *t)
{
if (t->cursteplen != 1) {
--t->cursteplen;
} else {
t->direction = ++t->direction % (2 * 2);
t->cursteplen = --t->steplen[t->direction % 2];
}
const size_t idx_to_change = t->direction % 2;
t->idx[idx_to_change] = t->idx[idx_to_change] + ( t->direction < 2 ? +1 : -1 );
}
int main()
{
int var[5][5];
struct strangeite_s i;
strangeite_init(&i, 5, 5);
int idx = 0;
for (size_t x, y; strangeite_loop(&i, &x, &y); strangeite_inc(&i)) {
var[y][x] = ++idx;
}
for (size_t i = 0; i < 5; ++i) {
for (size_t j = 0; j < 5; ++j) {
printf("%d ", var[i][j]);
}
printf("\n");
}
return 0;
}
Produces the following output:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Live version at onlinegdb.
It's possible to deterministically compute any one entry in the array A_{i,j} as a function only given the array size N, and i, j, in O(1), on-line, without any state, noticing the radius is a geometric progression. The space requirement is O(1) since one doesn't actually need the array to store the values; we can scan the array sequentially. However, like ray-tracing, it probably is slower, (up to a constant, it's still O(N^2).)
#include <stdio.h>
/* N: The size of the (simulated) array. */
#define N (16)
/* i, j: The array indices, as if, a[i][j]. */
static int a(const int i, const int j) {
/* (x,y) translation of (i,-j) to the centre, scaled up 2x for int math. */
const int x = 2 * i + 1 - N, y = -2 * j - 1 + N;
/* Geometric series and an offset +fiddling to get the directionality. */
return N*N - ((x < -y) ?
(-x > -y) ? (x+1)*(x+1) - (y+x)/2 - 1: y*y + (x+y)/2 :
(x > y) ? x*x + (x+y)/2 : (y+1)*y + (-x+y)/2);
}
int main(void) {
int i, j;
for(j = 0; j < N; j++) {
for(i = 0; i < N; i++) {
printf("%3d ", a(i, j));
}
printf("\n");
}
return 0;
}

Can't change dynamically allocated array values inside a function call

I'm having some issues with C.
I was tasked with this assignment:
Create a dynamically allocated matrix (max 5x5), let the user fill in the values, then send it to a function along with an unallocated array (1D) which will be used to store all values of the matrix that are lower than the average of each row.
The 1D array should be defined in the main function and allocated inside the function that finds the desired elements.
This was going fine, I made my matrix, I sent it to the aforementioned function, but I could not change the values of the newly allocated 1D array.
#include<stdio.h>
#include<stdlib.h>
int lower_than_average(int **matrix, int **p, int m, int n) {
int i, j, sum;
float average;
int count = 0, counter = 0;
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) sum += matrix[i][j];
average = (float)sum / n;
for (j = 0; j < n; j++){
if (matrix[i][j] < average) count++;
}
}
*p = (int*)malloc(count * sizeof(int));
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) sum += matrix[i][j];
average = (float)sum / n;
for (j = 0; j < n; j++) {
if (matrix[i][j] < average) *p[counter++] = matrix[i][j];
}
}
printf("\n\n");
return(count);
}
void print_matrix(int **matrix, int m, int n) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n\n");
}
}
int main(void) {
int m, n, **matrix, *elements_a = NULL;
int i, j, elements_a_size;
do {
printf("Enter the number of rows and columns [max 5x5]: ");
scanf("%d %d", &m, &n);
} while (n > 5 || n < 0 || m > 5 || m < 0);
matrix = (int**)malloc(m * sizeof(int*)); // Alokacija redaka
/*
X | <empty>
X | <empty>
X | <empty>
X | <empty>
*/
for (i = 0; i < m; i++) matrix[i] = (int*)calloc(n, sizeof(int)); // Alokacija stupaca
/*
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
*/
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("Enter [%d][%d] element: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\n\n");
print_matrix(matrix, m, n);
elements_a_size = lower_than_average(matrix, &elements_a, m, n);
printf("\nElements of rows smaller than the row average value: ");
for (i = 0; i < elements_a_size; i++) printf("%d ", elements_a[i]);
for (i = 0; i < m; i++) free(matrix[i]);
free(matrix);
return 0;
}
For the input: 3 3 then 1 2 3 4 5 6 7 8 9
It outputs literally nothing.
**p and *p[0]
instead of
*p[counter++]
it outputs this: 7 -2467754 -2467754
What am I doing wrong? I thought the allocation was correct and that I could access *(p + n) but it doesn't do anything...
You have to change the line
*p[counter++] = matrix[i][j];
by
(*p)[counter++] = matrix[i][j];
because [] take priority over *
See : Operator priorities in c/c++
The easiest way may be to create a one dimensional array that is big enough to store all the elements in an array, and then populate the 1D array as you go through each row of the 2D array. Once you have gone through the 2D array you copy the values that you need to an array that is just long enough and return.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int lower_than_average(int **matrix, int **p, int m, int n) {
int i, j, sum;
float average;
int count = 0, counter = 0;
int *buff;
buff = (int *)malloc(m * n * sizeof(int));
if (NULL == buff)
{
return (-1); //return negative value to show an error occurred
}
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) sum += matrix[i][j];
average = (float)sum / n;
for (j = 0; j < n; j++){
if (matrix[i][j] < average)
{
buff[count] = matrix[i][j];
count++;
}
}
}
*p = (int*)malloc(count * sizeof(int));
if(NULL == *p)
{
free(buff);
return (-1); //return negative value to show an error occurred
}
memcpy(*p, buff, count*sizeof(int));
free(buff);
printf("\n\n");
return(count);
}
void print_matrix(int **matrix, int m, int n) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n\n");
}
}
int main(void) {
int m, n, **matrix, *elements_a = NULL;
int i, j, elements_a_size;
do {
printf("Enter the number of rows and columns [max 5x5]: ");
scanf("%d %d", &m, &n);
} while (n > 5 || n < 0 || m > 5 || m < 0);
matrix = (int**)malloc(m * sizeof(int*)); // Alokacija redaka
/*
X | <empty>
X | <empty>
X | <empty>
X | <empty>
*/
for (i = 0; i < m; i++) matrix[i] = (int*)calloc(n, sizeof(int)); // Alokacija stupaca
/*
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
*/
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("Enter [%d][%d] element: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\n\n");
print_matrix(matrix, m, n);
elements_a_size = lower_than_average(matrix, &elements_a, m, n);
printf("\nElements of rows smaller than the row average value: ");
for (i = 0; i < elements_a_size; i++) printf("%d ", elements_a[i]);
printf("\n"); //a new line so the prompt isn't on the same line as the output
for (i = 0; i < m; i++) free(matrix[i]);
free(matrix);
free(elements_a); //also free the memory dynamically allocated in the function!
return 0;
}
And the results:
Enter the number of rows and columns [max 5x5]: 3
3
Enter [0][0] element: 1
Enter [0][1] element: 2
Enter [0][2] element: 3
Enter [1][0] element: 4
Enter [1][1] element: 5
Enter [1][2] element: 80
Enter [2][0] element: 12
Enter [2][1] element: 2
Enter [2][2] element: 1
1 2 3
4 5 80
12 2 1
Elements of rows smaller than the row average value: 1 4 5 2 1

assigning all combinations of a variable number of variable objects

I'm having difficulty with this recursion problem. I thought I had an answer to it but it doesn't work, and I simply don't know why, so I thought I would ask the experts. Please go easy on me, I took C programming more than 15 years ago and even then I was maybe a B student. I don't know C++ or Java.
The purpose is to generate all of the possible combinations of integers from 0 to (n[j]-1), where j can be an arbitrary integer. Right now it is hard-coded as 2, but I would like it to be able to take any value eventually.
Anyway, here is my code. Thanks in advance for your help.
Edit:
For the code below, I define 2 sequences, with the 0th sequence having a length of 2 (0,1) and the 1st sequence having a length of 3 (0, 1, 2).
The desired output is as follows:
p[0][0] = 0
p[0][1] = 0
p[1][0] = 0
p[1][1] = 1
p[2][0] = 0
p[2][1] = 2
p[3][0] = 1
p[3][1] = 0
p[4][0] = 1
p[4][1] = 1
p[5][0] = 1
p[5][1] = 2
That is,
the 0th combination contributes 0 from sequence 0 and 0 from sequence 1
the 1st combination contributes 0 from sequence 0 and 1 from sequence 1
the 2nd combination contributes 0 from sequence 0 and 2 from sequence 1
the 3rd combination contributes 1 from sequence 0 and 0 from sequence 1
the 4th combination contributes 1 from sequence 0 and 1 from sequence 1
the 5th combination contributes 1 from sequence 0 and 2 from sequence 1
I hope this makes it clearer what I'm trying to do!
#include <stdio.h>
#include <stdlib.h>
int recurse (int **p, int *n, int nclass, int classcount, int combcount);
int recurse (int **p, int *n, int nclass, int classcount, int combcount)
{
int k, j, kmax;
kmax = n[classcount];
j = classcount;
if (j == nclass) {
return (combcount+1);
}
for (k = 0; k < kmax; k++) {
p[combcount][j] = k;
combcount = recurse (p, n, nclass, j+1, combcount);
}
}
int main (void)
{
int **p, n[2], i, j;
n[0] = 2;
n[1] = 3;
p = (int **) malloc ((n[0]*n[1]) * sizeof (int *));
for (i = 0; i < (n[0]*n[1]); i++) {
p[i] = (int *) malloc (2 * sizeof (int));
for (j = 0; j < 2; j++)
p[i][j] = -1;
}
/* p[i][j] = the value of the integer in the ith combination
arising from the sequence 0...n[j]-1 */
recurse (p, n, 2, 0, 0);
for (i = 0; i < (n[0]*n[1]); i++)
for (j = 0; j < 2; j++)
printf ("%d %d: %d\n", i, j, p[i][j]);
for (i = 0; i < (n[0]*n[1]); i++)
free (p[i]);
free (p);
return (0);
}
#include <stdio.h>
#include <stdlib.h>
void recurse(int *n, int *accum, int **p, int N, int k) {
static int comb;
int i, j;
if (k == 0)
comb = 0;
if (k == N) {
for (i = 0; i < N; ++i)
p[comb][i] = accum[i];
comb++;
}
else
for (i = 0; i < n[k]; ++i) {
accum[k] = i;
recurse(n, accum, p, N, k+1);
}
}
int main(void) {
const int N = 2;
int n[N];
int accum[N];
int **p;
int mult;
int i, j;
n[0] = 2;
n[1] = 3;
for (mult = 1, i = 0; i < N; mult *= n[i], ++i);
p = malloc(mult*sizeof(int*));
for (i = 0; i < mult; i++)
p[i] = malloc(N*sizeof(int));
recurse(n, accum, p, N, 0);
for (i = 0; i < mult; ++i)
for (j = 0; j < N; ++j)
printf("p[%d][%d] = %d\n", i, j, p[i][j]);
for (i = 0; i < mult; i++)
free(p[i]);
free(p);
}
#include <stdio.h>
#include <stdlib.h>
int recurse (int **p, int *n, int nclass, int classcount, int p_size){
int i, j, jmax, k, kmax;
if (classcount == nclass) return 1;
i = 0;
kmax = n[classcount];
while(i < p_size){
for (k = 0; k < kmax; ++k){
jmax = recurse (p, n, nclass, classcount+1, p_size);
for(j = 0;j < jmax; ++j)
p[i++][classcount] = k;
}
}
return kmax*jmax;
}
int main (void){
int **p, n[2], i, j;
int sizeAll, sizeN;
n[0] = 2;
n[1] = 3;
sizeAll = n[0]*n[1];
sizeN = sizeof(n)/sizeof(int);
p = (int **) malloc (sizeAll * sizeof (int *));
for (i = 0; i < sizeAll; ++i) {
p[i] = (int *) malloc (sizeN * sizeof (int));
for (j = 0; j < sizeN; ++j)
p[i][j] = -1;
}
recurse (p, n, sizeN, 0, sizeAll);
for (i = 0; i < sizeAll; ++i)
for (j = 0; j < sizeN; ++j)
printf ("%d %d: %d\n", i, j, p[i][j]);
for (i = 0; i < sizeAll; ++i)
free (p[i]);
free (p);
return (0);
}

Resources