Count Sort in C - incomplete sorting - c

I'm trying to write an Count sort Program. My Problem is that it seems to sort the first 4 Numbers and after that he only prints 0. My input comes from an external file.
This is my sorted output:
0 1 5 8 0 0 0 0 0 0 0 0 0 0 0 0 0
The following is my code so far:
int MAX_LAENGE = 1000;
int MAX_VALUE = 100;
int i, k, j;
void count_sort_calculate_counts(int input_array[], int len, int count_array[]) {
for ( i = 0; i < len; i++)
{
count_array[i] = 0;
}
for (j = 0; j < len; j++)
{
count_array[input_array[j]] = count_array[input_array[j]] + 1;
}
}
void count_sort_write_output_array(int output_array[], int len, int count_array[]) {
k = 0;
for (j = 0; j < len; j++)
{
for (i = 0; i < count_array[j]; i++)
{
output_array[k] = j;
k = k + 1;
}
}

Two of the cycles - the one setting the counts to 0 and the one printing the numbers should not be until len, instead they should be until MAX_VALUE.
for ( i = 0; i < MAX_VALUE; i++)
{
count_array[i] = 0;
}
Similarly in the function that prints the numbers:
for (j = 0; j < MAX_VALUE; j++)
Without having more context around how you use this code, this is the only issue I spot.

Related

Sorting a matrix

Im trying to sort a matrix by the sum of its row's digits, from highest to lowest. I dont know if i explained that correctly so here's some photos explaining it.
This is what my code outputs. Basically, it asks you for m and n, which are the dimensions of the matrix. In this example it's a 3x4, 3 rows and 4 columns. Then, the matrix should be sorted by rows, by the sum of row's digits. Which means, instead of what's being outputted in the picture above, the correct result should be this:
I have no idea how to sort this from highest to lowest, i have been trying for hours to no avail.
Here's my code:
#include <stdio.h>
#define N 30
void main(){
double a[N][N], s[N], p;
int i, j, m, n, max;
while(1){
printf("\nm, n? ");
scanf("%d%d", &m, &n);
if(m <= 0 || m > N || n <=0 || n > N)
break;
for(i = 0; i < m; i++){
printf("%2d. row? ", i+1);
for(j = 0; j < n; scanf("%lf", &a[i][j++]));
}
for(i = 0; i < m; i++)
for(s[i] = j = 0; j < n; s[i] += a[i][j++]);
for(j = 0; j < n - 1; j++){
for(max = i, j = i+1; j < n; j++)
if(s[j] > s[max])
max = i;
if(max != j){
p = s[j];
s[j] = s[max];
s[max] = p;
for(j = 0; j < m; j++){
p = a[j][i];
a[j][i] = a[j][max];
a[j][max] = p;
}
}
}
printf("New matrix: \n");
for(i = 0; i < m; i++){
for(j = 0; j < n; printf("%8.2lf", a[i][j++]));
printf("\n");
}
for(j = 0; j < m; j++)
printf("-------------");
printf("\n");
for(j = 0; j < m; printf("%8.2f \n", s[j++]));
printf("\n");
}
}
You can sort the rows of the matrix from highest to lowest, using a simple bubble sort algorithm.Your code modified below:
int main() {
double a[N][N], s[N], p;
int i, j, m, n, max;
while (1) {
printf("\nm, n? ");
scanf("%d%d", & m, & n);
if (m <= 0 || m > N || n <= 0 || n > N)
break;
for (i = 0; i < m; i++) {
printf("%2d. row? ", i + 1);
for (j = 0; j < n; scanf("%lf", & a[i][j++]));
}
for (i = 0; i < m; i++)
for (s[i] = j = 0; j < n; s[i] += a[i][j++]);
for (i = 0; i < m - 1; i++) { // modified here
for (j = i + 1; j < m; j++) { // modified here
if (s[j] > s[i]) { // modified here
p = s[i];
s[i] = s[j];
s[j] = p;
for (int k = 0; k < n; k++) {
p = a[i][k];
a[i][k] = a[j][k];
a[j][k] = p;
}
}
}
}
printf("New matrix: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; printf("%8.2lf", a[i][j++]));
printf("\n");
}
for (j = 0; j < m; j++)
printf("-------------");
printf("\n");
for (j = 0; j < m; printf("%8.2f \n", s[j++]));
printf("\n");
}
return 0;
}
Here's how i modified your code to achieve that:
Initialize a loop variable i to 0.
In the outer loop, run the inner loop j from i+1 to m-1.
In the inner loop, compare the sum of the row i with the sum of row
j. If the sum of row j is greater than the sum of row i, swap the
rows using a temporary variable.
After the inner loop finishes, increment the value of i by 1. Repeat
the outer loop until i becomes equal to m-1.
Output:
You can just use qsort to let it handle the sorting and item swapping. Then you only need to write the code for comparing two rows with each other.
Given something like this:
int matrix[3][4] =
{
{1,2,3,4},
{5,6,7,8},
{9,1,2,3},
};
You'd call qsort as:
qsort(matrix, 3, sizeof(int[4]), compare);
The only complexity is implementing the comparison callback function. There's two things to consider there:
We've told qsort that we have an array of 3 items, each of type int[4]. So the void pointers it passes along to us will actually be pointers to type int[4]. That is: int(*)[4].
qsort sorts in ascending order by default, where the item considered "less" ends up first. So we need to tweak that to get the largest item first.
Example:
int compare (const void* obj1, const void* obj2)
{
const int (*ptr1)[4] = obj1;
const int (*ptr2)[4] = obj2;
size_t sum1=0;
size_t sum2=0;
for(size_t i=0; i<4; i++)
{
sum1 += (*ptr1)[i];
sum2 += (*ptr2)[i];
}
if(sum1 > sum2) // largest sum considered "less" for qsort
return -1;
else
return 1;
return 0;
}
sum1 < sum2 would have placed the smallest row first.
Full example:
#include <stdio.h>
#include <stdlib.h>
int compare (const void* obj1, const void* obj2)
{
const int (*ptr1)[4] = obj1;
const int (*ptr2)[4] = obj2;
size_t sum1=0;
size_t sum2=0;
for(size_t i=0; i<4; i++)
{
sum1 += (*ptr1)[i];
sum2 += (*ptr2)[i];
}
if(sum1 > sum2) // largest sum considered "less" for qsort
return -1;
else
return 1;
return 0;
}
void print_matrix(size_t col, size_t row, int matrix[col][row])
{
for(size_t i=0; i<col; i++)
{
for(size_t j=0; j<row; j++)
{
printf("%d,", matrix[i][j]);
}
puts("");
}
}
int main (void)
{
int matrix[3][4] =
{
{1,2,3,4},
{5,6,7,8},
{9,1,2,3},
};
print_matrix(3,4,matrix);
puts("");
qsort(matrix, 3, sizeof(int[4]), compare);
print_matrix(3,4,matrix);
}

selection sort isn't sorting correctly

My selection sort algorithm isn't working correctly. I've been stuck on this for quite a long time. I've solved it using pointers but for some reason it's sorting incorrectly.
void sortArray(int arr[], size_t n)
{
int i;
int j;
int min_idx;
int min;
for (i = 0; i < n - 2; i++)
{
min = arr[i];
min_idx = i;
for (j = (i+1); j < n - 1; j++)
{
if (arr[j] < arr[min])
{
min = arr[j];
min_idx = j;
}
}
//swap(arr[min_idx], arr[i]);
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
Before
10 9 8 7 6 5 4 3 0 0
After
0 9 7 3 6 4 5 8 10 0
But help would be appreciated.
There are at least two bugs in the function.
The first one is that the last element of the array with the index n-1 does not take part in sorting of the array due to conditions in the loops
for (i = 0; i < n - 2; i++)
{
min = arr[i];
min_idx = i;
for (j = (i+1); j < n - 1; j++)
//..
The second bug is that in this if statement
if (arr[j] < arr[min])
there is used min as an index instead of min_index in the operand arr[min].
Pay attention to that when n is equal to 0 or 1 then in the condition of the loop
for (i = 0; i < n - 2; i++)
the expression n - 2 will yield a big unsigned value that results in undefined behavior of the function.
And these variables
int i;
int j;
int min_idx;
shall have the type size_t instead of the type int.
Also declare variables in the minimum scope where they are used.
The function can be defined the following way.
void sortArray( int arr[], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
size_t min_idx = i;
for ( size_t j = i + 1; j < n; j++ )
{
if ( arr[j] < arr[min_idx] )
{
min_idx = j;
}
}
if ( i != min_idx )
{
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
}
Here is a demonstrative program.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sortArray( int arr[], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
size_t min_idx = i;
for ( size_t j = i + 1; j < n; j++ )
{
if ( arr[j] < arr[min_idx] )
{
min_idx = j;
}
}
if ( i != min_idx )
{
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
}
int main(void)
{
srand( ( unsigned int )time( NULL ) );
while ( 1 )
{
printf( "Enter the size of an integer array: " );
size_t n = 0;
if ( scanf( "%zu", &n ) != 1 || n == 0 ) break;
putchar( '\n' );
int a[n];
for ( size_t i = 0; i < n; i++ )
{
a[i] = rand() % n;
}
printf( "Source array: " );
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
sortArray( a, n );
printf( "Sorted array: " );
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
putchar( '\n' );
}
return 0;
}
Its output might look like
Enter the size of an integer array: 10
Source array: 4 1 1 3 1 8 7 4 0 9
Sorted array: 0 1 1 1 3 4 4 7 8 9
Enter the size of an integer array: 0
Your inner if is using the wrong index. It should be
if (arr[j] < arr[min_idx])
or, since arr[min_idx] is already stored in min
if (arr[j] < min)
You're also one index short in your sorting. You never look at arr[n - 1].
There are three small errors.
Two off by one errors on your loop conditions, and one variable name error (min instead of min_idx).
Fixing these three errors gives us...
void sortArray(int arr[], size_t n)
{
int i;
int j;
int min_idx;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = (i+1); j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;
}
}
//swap(arr[min_idx], arr[i]);
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
Also removed the unused min variable.

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;
}

use to matrix and double pointer

The size of the square matrix is entered as standard input (the range of input values is 2 to 10). Create a program that prints the unit matrix of the input size.
The unit matrix is a matrix whose diagonal from the top left to the bottom right (main diagonal) is 1 and all others are zero.
ex) input: 5
result :
1 0 0 0 0\n
0 1 0 0 0\n
0 0 1 0 0\n
0 0 0 1 0\n
0 0 0 0 1\n
my code:
int main(){
int n;
scanf("%d", &n);
int **matrix = malloc(sizeof(int *) * n);
for (int i = 0; i < n; i++)
{
matrix[i] = malloc(sizeof(int) * n);
memset(matrix[i], 0, sizeof(int) * n);
}
for (int i = 0; i < n; i++)
{
matrix[i][i] = 1;
printf("%d ", matrix[i][i]);
}
printf("\n");
for (int i = 0; i < n; i++)
{
free(matrix[i]);
}
free(matrix);
return 0;
}
.. what 's the error?
You need a double loop to print the output:
for (int i = 0; i < n; i++)
{
matrix[i][i] = 1; // set identity
}
for (int j=0; j<n; j++)
{
for (int i = 0; i < n; i++)
{
printf("%d ", matrix[j][i]);
}
printf("\n");
}
One error is to think yo need a data strucure to print the output requested.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
if (scanf("%d", &n) != 1 || n < 2 || 10 < n) {
perror("Input error!\n\n");
return EXIT_FAILURE;
}
for (int i = 0; i < n; ++i) {
for (int k = 0; k < n; ++k)
printf(i == k ? "1 " : "0 ");
putchar('\n');
}
}

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