Basically, we made a neural network that tests if a digit from a digital clock is odd or even.
The C code works on my windows machine, however, it gives me a lot of errors on my mac.
Like: function definition is not allowed here.
I think it's just a compilation issue. Any help would be much appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define ligne 10
#define colonne 8
int main (){
//--------------------------creation A, W, O, d, X, epsilon--------------------------
int i,j;
float epsilon = 0.2;
float cpt_A;
int O[ligne];
float A[ligne];
float W[colonne];
srand(time(NULL));
for( i = 0 ; i < colonne ; i++ ) {
W[i]=rand() % 6;
}
int d[ligne] = {0,1,0,1,0,1,0,1,0,1};
int X[ligne][colonne]= {{1,1,1,1,1,1,1,0},{1,0,1,1,0,0,0,0},{1,1,1,0,1,1,0,1},{1,1,1,1,1,0,0,1},
{1,0,1,1,0,0,1,1},{1,1,0,1,1,0,1,1},{1,1,0,1,1,1,1,1},{1,1,1,1,0,0,0,0},
{1,1,1,1,1,1,1,1},{1,1,1,1,1,0,1,1}};
//--------------------------Check for O--------------------------
int check(float a){
if(a>=0) return 1;
else return 0;
}
//--------------------------Remplissage de A[]--------------------------
float* Remplissage_A(float A[],float nW[],int index ){
for(i=index ; i<ligne ; i++){
for(j=0 ; j<colonne ; j++){
cpt_A += nW[j]*X[i][j];
}
A[i]=cpt_A;
}
return A;
}
//--------------------------Fonction check pour remplir O--------------------------
int* Remplissage_O(float A[]){
for ( i = 0; i < ligne; i++){
O[i]=check(A[i]);
}
return O;
}
//--------------------------Affichage X--------------------------
for(i=0; i<colonne; i++){
printf("X%d\t",i);
}
printf("Valeur\n");
for(i=0; i<ligne; i++){
for(j=0; j<colonne; j++){
printf("%d",X[i][j]);
printf(j<colonne-1?"\t":" => %d ",i);
}
printf("\n");
//printf("%f\n",A[i]);
}
//--------------------------Print A et O et d--------------------------
printf("\nA\t");printf("O\t");printf("D\n");
float *Ax= Remplissage_A(A,W,0);
int *Ox= Remplissage_O(A);
for ( i = 0; i < ligne; i++){
printf("%.1f\t",*(Ax + i));printf("%d\t",*(Ox + i));printf("%d\n",d[i]);
}
//--------------------------Fonction Correction W --------------------------
void Correction_W(int index){
for(j=0;j<colonne;j++){
W[j]=W[j]+(epsilon*((d[index]-O[index])*X[index][j]));
}
}
//--------------------------Fonction Correction--------------------------
printf("-----------------------------------------------\n");
void Correction(float *A,float *W,int *O){
int stop=0;
int allCorrect= false;
int i,index;
A=Remplissage_A(A,W,0);
O=Remplissage_O(A);
while (!allCorrect){
for ( i = 0; i < ligne; i++){
if(d[i]!=O[i]){
index=i;
Correction_W(index);
A = Remplissage_A(A,W,index);
O = Remplissage_O(A);
stop++;
}
}
if(stop==0){
allCorrect=true;
}
stop=0;
}
}
//--------------------------Appel Fonction--------------------------
Correction(A,W,O);
//--------------------------Apres correction--------------------------
printf("Apres correction : \n");
printf("-----------------------------------------------\n");
printf("\nA\t");printf("O\t");printf("D\n");
for ( i = 0; i < ligne; i++){
printf("%.1f\t",*(A + i));
printf("%d\t",*(O + i));
printf("%d\n",d[i]);
}
}
(main issue) I changed your nested functions to, well, non-nested functions. You may not like the order of arguments that I chose.
Remplissage_A(): I initialized cpt_A = 0. Please check that this is what you want.
main(): Fixed the conditional print to silence warning about one of the strings not having a matching format for variable being passed in.
check(): refactored then inlined.
Minimized scope of variables (i, j, size, cpt_A, etc).
Combined consecutive printf() calls.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ligne 10
#define colonne 8
//--------------------------Remplissage de A[]--------------------------
float* Remplissage_A(float A[],float nW[], int X[ligne][colonne], int index) {
float cpt_A = 0;
for(int i=index ; i<ligne ; i++){
for(int j=0 ; j<colonne ; j++){
cpt_A += nW[j]*X[i][j];
}
A[i]=cpt_A;
}
return A;
}
//--------------------------Fonction check pour remplir O--------------------------
int* Remplissage_O(float A[], int *O) {
for (int i = 0; i < ligne; i++){
O[i]=A[i] >= 0;
}
return O;
}
//--------------------------Fonction Correction W --------------------------
void Correction_W(int *O, float *W, int X[ligne][colonne], int d[ligne], float epsilon, int index){
for(int j=0;j<colonne;j++){
W[j]=W[j]+(epsilon*((d[index]-O[index])*X[index][j]));
}
}
//--------------------------Fonction Correction--------------------------
void Correction(float *A,float *W, int X[ligne][colonne], int *O, int d[ligne], float epsilon){
int stop=0;
int allCorrect= false;
A=Remplissage_A(A,W,X,0);
O=Remplissage_O(A,O);
while (!allCorrect){
for (int i = 0; i < ligne; i++){
if(d[i]!=O[i]){
Correction_W(O,W,X,d,epsilon, i);
A = Remplissage_A(A,W,X,i);
O = Remplissage_O(A,O);
stop++;
}
}
if(stop==0){
allCorrect=true;
}
stop=0;
}
}
int main (){
//--------------------------creation A, W, O, d, X, epsilon--------------------------
float epsilon = 0.2;
int O[ligne];
float A[ligne];
float W[colonne];
srand(time(NULL));
for(int i = 0 ; i < colonne ; i++ ) {
W[i]=rand() % 6;
}
int d[ligne] = {0,1,0,1,0,1,0,1,0,1};
int X[ligne][colonne]= {{1,1,1,1,1,1,1,0},{1,0,1,1,0,0,0,0},{1,1,1,0,1,1,0,1},{1,1,1,1,1,0,0,1},
{1,0,1,1,0,0,1,1},{1,1,0,1,1,0,1,1},{1,1,0,1,1,1,1,1},{1,1,1,1,0,0,0,0},
{1,1,1,1,1,1,1,1},{1,1,1,1,1,0,1,1}};
//--------------------------Affichage X--------------------------
for(int i=0; i<colonne; i++){
printf("X%d\t",i);
}
printf("Valeur\n");
for(int i=0; i<ligne; i++){
for(int j=0; j<colonne; j++){
printf("%d",X[i][j]);
if(j<colonne-1)
printf("\t");
else
printf(" => %d ",i);
}
printf("\n");
}
//--------------------------Print A et O et d--------------------------
printf("\nA\t");printf("O\t");printf("D\n");
float *Ax= Remplissage_A(A,W,X,0);
int *Ox= Remplissage_O(A,O);
for (int i = 0; i < ligne; i++){
printf(
"%.1f\t"
"%d\t"
"%d\n",
*(Ax + i),
*(Ox + i),
d[i]
);
}
printf("-----------------------------------------------\n");
//--------------------------Appel Fonction--------------------------
Correction(A,W,X,O,d,epsilon);
//--------------------------Apres correction--------------------------
printf(
"Apres correction : \n"
"-----------------------------------------------\n"
"\nA\t"
"O\t"
"D\n"
);
for (int i = 0; i < ligne; i++){
printf(
"%.1f\t"
"%d\t"
"%d\n",
*(A + i),
*(O + i),
d[i]
);
}
}
Related
I am writing a program to calculate matrix multiplication but it does not work. When I debug and check each value of the array a and b in function printMatrixMultiplication (which are entered by user), GDB prints out "cannot perform pointer math on incomplete type try casting". (I have searched for it but I still don't get it.) The function only works when the input is predefined in main.
This is my code
#include <stdio.h>
void input(int m, int n, double a[m][n]);
void output(int m, int n, double a[m][n]);
void printMatrixMultiplication(int row_a, int col_a, double a[row_a][col_a], int row_b, int col_b, double b[row_b][col_b]);
int main()
{
int row_a, col_a, row_b, col_b;
// get value of matrix a
printf("row_a = ");
scanf("%d", &row_a);
printf("col_a = ");
scanf("%d", &col_a);
double a[row_a][col_a];
input(row_a, col_a, a);
// output(row_a, col_a, a);
// get value of matrix b
printf("row_b = ");
scanf("%d", &row_b);
printf("col_b = ");
scanf("%d", &col_b);
double b[row_b][col_b];
input(row_b, col_b, a);
// output(row_b, col_b, a);
printMatrixMultiplication(row_a, col_a, a, row_b, col_b, b);
//test
// double a[2][2]={1,2,3,4};
// double b[2][3]={1,2,3,4,5,6};
// printMatrixMultiplication(2,2,a,2,3,b);
return 0;
}
void input(int m, int n, double a[m][n])
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%lf", &a[i][j]);
}
}
}
void output(int m, int n, double a[m][n])
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%.2f ", a[i][j]);
}
printf("\n");
}
}
void printMatrixMultiplication(int row_a, int col_a, double a[row_a][col_a], int row_b, int col_b, double b[row_b][col_b])
{
if (col_a != row_b)
{
return;
}
double res[row_a][col_b]; //this matrix store results
for (int i = 0; i < row_a; i++) //the values be stored line by line, this
{ //operation is controled by i and j loops.
for (int j = 0; j < col_b; j++) //the k loop helps calculate dot_product.
{
double dot_product = 0;
for (int k = 0; k < col_a; k++)
{
dot_product += a[i][k] * b[k][j]; //ERROR HERE
}
res[i][j] = dot_product;
}
}
output(row_a, col_b, res);
}
So, where does the error come from and how to fix it?
Irrelevant, but the function is not well implemented so if possible, I would really appreciate if anyone gives me a hint to improve it.
I am using GCC version 6.3.0.
It's typo in your code when reading matrix b.
Just replace:
input(row_b, col_b, a);
with
input(row_b, col_b, b);
Edit3: Added Example.
Edit2: So, because I am new to coding in general and because of the current comments,I need to ask, would it be better if I posted the entire code as a comment? (around 90 lines)
So, I have been playing around with dynamic memory allocation and I have a 2D board, witch fills with '.'. Then I import the board in a function, witch checks for available cells (cell with '.'==available). It compiles ok, but when I run it I get
segmentation fault
Here's the code for the board malloc
**board = (char**) malloc(x_input*sizeof(char*));
for(i = 0; i <x_input; i++){
board[i] = (char*) malloc(y_input*sizeof(char));
}
for (i = 0; i<x_input; i++){
for(j = 0; j<y_input; j++){
board[i][j]='.';
}
}
Here's the Function
int checker(int x_axis, int y_axis, char **board){
if (board[x_axis][y_axis] == '.'){
return 1;
} else {
return 2;
}
}
And here's the only time(so far) that I call the function
Edit: x_replacement and y_replacement are assigned random values through a rand function
do{
board[x_replacement][y_replacement] = '$';
} while(checker(x_replacement, y_replacement, board) == 2);
EX:
const int MAX_X = 40;
const int MAX_Y = 40;
const int MIN_X = 20;
const int MIN_Y = 20;
int x_input, y_input;
int main(void){
char **board;
int i, j, k, obstacles, enemies, choice;
do{
printf("Enter board size. (Must be between (%d, %d) and (%d, %d))\n:", MIN_X, MIN_Y, MAX_X, MAX_Y);
scanf("%d%d", &x_input, &y_input);
}while ((x_input <= MIN_X && x_input >= MAX_X) && (y_input <= MIN_Y && y_input >= MAX_Y));
*board = malloc(sizeof(char[x_input][y_input]));
assert(*board != NULL);
for (i = 0; i<x_input; i++){
for(j = 0; j<y_input; j++){
board[i][j]='.';
}
}
return 0;
}
I approached this from a different perspective, thanks to Ludin's and Some Programmer Dude's links so the result, witch works is the following:
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
void arr_alloc (size_t x, size_t y, char(**board)[x][y])
{
*board= malloc( sizeof(char[x][y]) );
assert(*board!= NULL);
}
void arr_fill (size_t x, size_t y, char board[x][y])
{
for(size_t i=0; i<x; i++)
{
for(size_t j=0; j<y; j++)
{
array[i][j] = '.';
}
}
}
void arr_print (size_t x, size_t y, char board[x][y])
{
for(size_t i=0; i<x; i++)
{
for(size_t j=0; j<y; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
int main (void)
{
int x,y;
char (*board)[x][y];
printf("enter dimentions: \n");
scanf("%d%d", &x, &y);
arr_alloc(x, y, &board);
arr_fill(x, y, *board);
arr_print(x, y, *board);
free(board);
return 0;
}
I'm trying to write a function that changes one value of the elements in an array of struct, but it isn't working, the function does nothing. What am I doing wrong?
Input:
300
9
1999
1050
301
5
2000
1200
20
Expected output:
300 1260
Actual output: nothing
#include <stdio.h>
typedef struct
{int codice;
int mese;
int anno;
int stipendio;}
dipendente;
void aumento (dipendente a[], int dim, int n){
int i;
for (i=0; i<dim; i++)
{if (a[i].anno<2000) a[i].stipendio=a[i].stipendio+(a[i].stipendio*n)/100;;
if (a[i].anno==2000)
{if (a[i].mese<5)
a[i].stipendio=a[i].stipendio+(a[i].stipendio*n)/100;}}
}
int main () {
int i;
int p;
dipendente a[2];
for (i=0; i<2; i++){
scanf("%d",&a[i].codice);
scanf("%d",&a[i].mese);
scanf("%d",&a[i].anno);
scanf("%d",&a[i].stipendio);
}
scanf("%d", &p);
aumento (a, 2, p);
for (i=0; i<2; i++)
{if(a[i].stipendio>1200)
printf("%d %d", a[i].codice, a[i].stipendio);}
return 0; }
There two problems.
As #n.m. pointed out in comments: if (a[i].anno=2000) is doing an assignment and is always true (because 2000 is true). You want to compare. Use double == for it if (a[i].anno == 2000)
As #SamiHult pointed out in comments: n/100 will always be 0 for any 0 <= n && n < 100, because n is an int. Use double or float to have floating point math. Or as #alk pointed out, you can first multiply then divide, so that you can stay in integer math (a[i].stipendio * n) / 100
This is good code, but indentation just hurts.
After fixing those errors:
#include <stdio.h>
typedef struct {
int codice;
int mese;
int anno;
int stipendio;
} dipendente;
void aumento(dipendente a[], int dim, int n) {
int i;
for (i = 0; i < dim; i++) {
if (a[i].anno < 2000) {
a[i].stipendio = a[i].stipendio + a[i].stipendio * ((double)n / 100);
}
if (a[i].anno == 2000) {
if (a[i].mese < 5) {
a[i].stipendio = a[i].stipendio + a[i].stipendio * ((double)n / 100);
}
}
}
}
int main() {
int i;
int p;
dipendente a[2];
for (i = 0; i < 2; i++){
scanf("%d", &a[i].codice);
scanf("%d", &a[i].mese);
scanf("%d", &a[i].anno);
scanf("%d", &a[i].stipendio);
}
scanf("%d", &p);
aumento(a, 2, p);
for (i = 0; i < 2; i++) {
if (a[i].stipendio > 1200) {
printf("%d %d", a[i].codice, a[i].stipendio);
}
}
return 0;
}
your code prints the expected output.
This is a sample implementation of KD-tree.
Where I first take number of dimensions, number of points, number of clusters to be formed. Bi-partition function calculates centroid, dimension which has max variance. Now based on the max dimensions mean I start splitting the points. This program works fine when input is (dimensions-2,points-20,clusters-4). But does not work for (dimensions-2,points-20,clusters-8). When I debug the program it gives proper output.But when I run the program it stops working.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
int *gendata(int num);
void bipartition_fn(int dimensions,int nodes,int i0, int im, int *data,int *cluster_size,int *cluster_start, int *cluster_bdry, int *cluster_centroid);
void kdtree_fn(int dimensions, int nodes, int k, int *data,int *k_cluster_size,int **k_cluster_centroid,int *k_cluster_start,int **k_cluster_bdry );
int main()
{
int dimensions,nodes,i,k,j;
printf("enter the number of dimensions");
scanf("%d",&dimensions);
printf("enter the total number of elements in multiples of dimensions");
scanf("%d", &nodes);
printf("enter number of clusters");
scanf("%d",&k);
int *data;
int *k_cluster_size;
int **k_cluster_centroid;
int *k_cluster_start;
int **k_cluster_bdry;
data = gendata(nodes); /*dynamic array generation for data points*/
k_cluster_bdry=(int **)malloc(k*sizeof(int *));
for(i=0;i<(2*k-2);i++)
*(k_cluster_bdry+i)=(int *)malloc(2*dimensions*sizeof(int));
k_cluster_centroid=(int **)malloc(k*sizeof(int *));
for(i=0;i<(2*k-2);i++)
*(k_cluster_centroid+i)=(int *)malloc(dimensions*sizeof(int));
k_cluster_size=malloc((2*k-2)*sizeof(int));
k_cluster_start = malloc((2*k-2)*sizeof(int));
/*calling the kdtree function*/
kdtree_fn(dimensions, nodes, k, data, k_cluster_size, k_cluster_centroid, k_cluster_start, k_cluster_bdry);
/*printing the cluster size */
printf("cluster size \n");
for(i=k-2; i<(2*k - 2); i++){
printf("%d ", k_cluster_size[i]);
}
free(data);
free(k_cluster_bdry);
free(k_cluster_centroid);
free(k_cluster_size);
free(k_cluster_start);
return 0;
}
void kdtree_fn(int dimensions, int nodes, int k, int *data,int *k_cluster_size,int **k_cluster_centroid,int *k_cluster_start,int **k_cluster_bdry){
int i,j,d0,dm,x,m=0,l,n=0,check=1,s,temp=0;
d0 = 0, dm =nodes ;
int *cluster_size, *cluster_start;
int *cluster_bdry;
int *cluster_centroid;
int *query;
int *res;
query = (int *)malloc(sizeof(int)*dimensions);
res = (int *)malloc(sizeof(int)*dimensions);
cluster_centroid = (int*)malloc(dimensions*sizeof(int));
cluster_bdry = (int*)malloc(4*dimensions*sizeof(int));
cluster_size=(int*)malloc(2*sizeof(int));
cluster_start = (int*)malloc(2*sizeof(int));
/* iterating k-1 times to form k clusters */
for(x=0 ; x<k-1; x++){
bipartition_fn(dimensions, nodes, d0, dm, data, cluster_size, cluster_start, cluster_bdry, cluster_centroid);
for( i=0;i<dimensions; i++){
k_cluster_centroid[x][i] = cluster_centroid[i];
}
for( i=0;i<2; i++){
k_cluster_size[m] = cluster_size[i];
k_cluster_start[m] = cluster_start[i];
m++;
}
int p=0,r=0;
while(p<2){
l=0;
i=0;
while(i<2*dimensions){
k_cluster_bdry[temp][l] = cluster_bdry[r];
l++;
i++;
r++;
}
temp++;
p++;
}
s = pow(2,check);
if(x == 0 ||(x%(s-2)) == 0){
d0 =0;
nodes = k_cluster_size[n];
check++;
n++;
}
else{
d0 = d0+k_cluster_size[n-1];
nodes = k_cluster_size[n];
n++;
}
}
free(cluster_bdry);
free(cluster_centroid);
free(cluster_size);
free(cluster_start);
}
/*Each bipartition function gives 2 clusters*/
void bipartition_fn(int dimensions,int nodes,int d0, int dm, int *data,int *cluster_size,int *cluster_start, int *cluster_bdry, int *cluster_centroid){
int i,j,x,k;
int node = nodes/dimensions;
int sum,min,max;
int *cluster_assign;
cluster_assign = malloc(nodes*sizeof(int));
int *assign;
assign= (int *)malloc(node*sizeof(int));
// printf("nodes: %d \n", nodes);
/*calculate centroid and boundaries*/
i=0;
j=d0;
while(i<dimensions){
sum=0;
while(j<(d0+nodes)){
sum = sum+data[j];
j = j+dimensions;
}
cluster_centroid[i] = sum/node;
i = i+1;
j=d0+i;
}
/* Calculate variance of each dimension and find dimension with maximum variance*/
int var[dimensions],g,h;
h=d0;
g=0;
while(g<dimensions){
sum = 0;
while(h<(d0+nodes)){
sum = sum +((cluster_centroid[g] - data[h])*(cluster_centroid[g] - data[h]));
h=h+dimensions;
}
var[g] = sum/node;
g=g+1;
h=(d0+g);
}
int large = var[0];
int max_dimension =0;
int p;
for(p=0; p<dimensions; p++){
if(var[p]>large){
large = var[p];
max_dimension = p;
}
}
/* find mean of maximum variance*/
int mean = cluster_centroid[max_dimension];
//printf("mean %d \n",mean);
i=d0+max_dimension;
x=0;
while(i<(d0+nodes)){
if(data[i] < mean){
assign[x]=0;
}
else{
assign[x]=1;
}
x++;
i= i+dimensions;
}
/* Rearranging the points based on mean points lesser than mean goes to left and greater than mean goes to right*/
x=0;
int count=0;
int y=0;
for(i=0; i<node; i++){
if(assign[y] == 0){
count++;
for(j=dimensions*i; j<dimensions*(i+1); j++){
cluster_assign[x] = data[d0+j];
x++;
}
}
y++;
}
cluster_size[0] = count*dimensions;
cluster_start[0]= d0;
count=0;
y=0;
for(i=0; i<node; i++){
if(assign[y]!=0){
count++;
for(j=dimensions*i; j<dimensions*(i+1); j++){
cluster_assign[x] = data[d0+j];
x++;
}
}
y++;
}
cluster_size[1] = count*dimensions;
cluster_start[1]= d0+cluster_size[0];
int temp1,temp2;
x=0;
p=0;
while(p<2){
j=cluster_start[p];
i=0;
while(i<dimensions){
min=data[j];
max=data[j];
temp1=cluster_start[p];
temp2=cluster_size[p];
while(j < temp1+temp2){
if(data[j]<min)
min = data[j];
if(data[j]>max)
max= data[j];
j = j+dimensions;
}
cluster_bdry[x]=min;
x=x+1;
cluster_bdry[x]=max;
x=x+1;
i = i+1;
j=temp1+i;
}
p++;
}
/*printf("bou");
for(i=0; i<4*dimensions; i++){
printf("%d ",cluster_bdry[i]);
} */
free(cluster_assign);
free(assign);
}
/*Initialize data array*/
int *gendata(int num)
{
int *ptr = (int *)malloc(sizeof(int)*num);
int j = 0;
if(ptr != NULL)
{
for(j = 0; j < num; j++)
{
ptr[j] = -50 + rand()%101;
}
}
return ptr;
}
I coded as below to print all the permutations of three number :1,2,3.
But the output is:
1,1,1
1,1,2
1,1,3
1,2,1
1,2,2
1,2,3
The code is as follows:
#include<stdio.h>
#include<conio.h>
void perm(int);
int a[10],l=2;
int main()
{
int k;
k=0;
perm(k);
getch();
return 0;
}
void perm(int k)
{
int i;
for(a[k]=1;a[k]<=3;a[k]++)
{
if(k==2)
{
for(i=0;i<3;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
else
{
k++;
perm(k);
}
}
}
Please give the correct code.
Why do you increment k? k should not change for a given call to perm().
Also it's a bit too bad to be stuck with 3 permutations, you can easily generalize this way:
#include<stdio.h>
#include<conio.h>
static void perm(int, int);
static void all_perm(int);
int a[10];
int main()
{
all_perm(3);
getch();
return 0;
}
void all_perm(int n)
{
perm(0, n);
}
void perm(int k, int n)
{
if (k == n)
{
for(int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
else
{
for(a[k]=1; a[k] <= n; a[k]++)
{
perm(k + 1, n);
}
}
}
Edit: Well, what you name permutations are not permutations.
The logic that I have used is to some extent similar to yours.
I have included the entire code to make it clear.
#include <stdio.h>
void recn(int*,int,int);
void print_arr(int*,int);
void main()
{
int arr[3] = {1,2,3};
recn(arr,3,0);
}
void print_arr(int *arr, int n){
int i;
for(i = 0,printf("\n"); i < n; printf("%d",arr[i++]));
}
void recn(int *arr, int n, int l) {
int i, j, f, k, xx = 0;
static int tst[15], a[14]={0};
if (l == n) {
for (i = 0; i < n; i++) {
tst[i] = arr[a[i]];
}
print_arr(tst,n);
return;
}
for (i = 0; i < n; i++) {
f = 0;
for (j = 0; j < l; j++)
if (a[j] == i)
f = 1;
if (!f) {
a[l] = i;
recn(arr, n, l + 1);
}
}
}