Print all the permutations using recursion - c

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

Related

How fix the bubble-sort ? It doesn't line up in numerical order

#define stdMax 5
#define SWAP(a,b) {int t; t = a; a=b; b=t;}
typedef struct student {
char name[100];
int stdNum;
} Student;
void bubbleSort(int std[], int n );
int main() {
char text[stdMax][100];
Student std[stdMax];
char* temp = NULL;
for (int i = 0; i < stdMax; i++) {
printf("%d Enter student name / student number: ", i + 1);
gets(&text[i][0]);
}
for (int i = 0; i < stdMax; i++ ) {
strcpy_s(std[i].name, 100, strtok_s(&text[i][0], "/",&temp));
std[i].stdNum = atoi(strtok_s(NULL,"/",&temp));
}
bubbleSort(std, 5);
for (int i = 0; i < stdMax; i++) {
printf("%d %s\n", std[i].stdNum, std[i].name);
}
}
void bubbleSort(int *std, int n)
{
int i, j;
for (i = n; i > 1; i--)
{
for (j = 1; j < i; j++)
{
if (std[j - 1] > std[j])
{
SWAP(std[j - 1], std[j]);
}
}
}
}
ex) student/1234,
I'd like to put 5 in order of number size. However, when this code is executed, it only outputs as it is entered. I don't know which part is wrong. I think there's something wrong with the bubble sort, so I'd appreciate it if you could help me.
You are dealing with an array of Student, but the bubbleSort function is for sorting int. Mismatch here.
#define stdMax 5
#define SWAP(a,b) {Student t; t = a; a=b; b=t;}
typedef struct student {
char name[100];
int stdNum;
} Student;
void bubbleSort(Student std[], int n );
/* omit: same as original */
void bubbleSort(Student *std, int n)
{
int i, j;
for (i = n; i > 1; i--)
{
for (j = 1; j < i; j++)
{
if (std[j - 1].stdNum > std[j].stdNum)
{
SWAP(std[j - 1], std[j]);
}
}
}
}

Passing pointer to an array as a parameter to a function

I tried to build a heap and finally print the elements in the form of an array.
Here it is the code (I know this doesn't really make sense but I just wanted to test my knowlwdge of heap and dynamic arrays):
#include <stdio.h>
#include <stdlib.h>
void heapiify(int *arr,int n, int i)
{
int largest=i;
int l=2*i+1; // left node
int r= 2*i+2; // right node
if(l<=n && *arr[l]>=*arr[i])
largest=l;
if (r <=n && *arr[r]<=*arr[i])
largest= r;
if(largest !=i)
{
int temp=*arr[i];
*arr[i]=*arr[largest];
*arr[largest]=temp;
}
heapify(*arr,n,largest);
}
void buildh(int *arr,int n,int r,int c)
{
int i;
for(i=n/2-1;i>=0;i--)
heapify(*arr,n,i);
output(*arr,r,c);
}
void output(int *arr,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d",*arr[i*c+j]);
}
printf("\n");
}
}
int main()
{
int i,j,r,c;
printf("enter the number of rows");
scanf("%d",&r);
printf("enter the number of columns");
scanf("%d",&c);
int n=r*c;
int *arr=malloc(n*sizeof(int));
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&arr[i*c+j]);
}
buildh(*arr,n,r,c);
}
I'm getting 9 errors which are all the same
invalid argument type of unary '*'( have int)
Your arr variable is of type pointer to int:
int *arr=malloc(n*sizeof(int));
So when you call buildh, which takes the same type, you have to pass it as-is:
buildh(arr,n,r,c);
Same for the other cases.
The problem is the dereference of arr, across your funtions in multiple places, and the passing of dereferenced *arr in your functions to int * parameters, you should pass arr, try:
//...
void heapify(int *arr, int n, int i)
{
int largest = i;
int l = 2 * i + 1; // left node
int r = 2 * i + 2; // right node
if (l <= n && arr[l] >= arr[i]) //here
largest = l;
if (r <= n && arr[r] <= arr[i]) //here
largest = r;
if (largest != i)
{
int temp = arr[i]; //here
arr[i] = arr[largest]; //here
arr[largest] = temp; //here
}
heapify(arr, n, largest); //here
}
void buildh(int *arr, int n, int r, int c)
{
int i;
for (i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i); //here
output(arr, r, c); //here
}
void output(int *arr, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d", arr[i * c + j]); //here
}
printf("\n");
}
}
int main()
{
//...
buildh(arr, n, r, c); //here
}

Finding next smallest palindrome, infinite loop

I am writing a code to find the next smallest palindrome(integer) . I am (must) using array to deal with too large numbers like below:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
void check_pal(int a[],int max)
{
int i,j,ctr,k;
while(1)
{
for(i=0;i<max;i++)
printf("%d",a[i]);
ctr=0;
k=max-1;
while(a[k]==9)
{
a[k--]=0;//add corner case when k==0
}
a[k]++;
for(i=0,j=max;i<max/2;i++,j--)
{
printf("%d",i);
if(a[i]!=a[j])
{
ctr=1;
break;
}
}
if(ctr==0)
for(i=0;i<max;i++)
{
printf("%d",a[i]);
if(i==max-1)
return;
}
}
}
void int_convert(char * m,int a[] )
{
int i,max;
for(i=0;i<strlen(m);i++)
{
// printf("%c",m[i]);
a[i]=m[i]-'0';
}
max=strlen( m);
printf("%d\n",max);
check_pal(a,max);
}
void main()
{ int a[200],max;
char * m=malloc(sizeof(char)*200);
scanf("%s",m);
int_convert(m,a);
getch();
}
The output result is an infinite loop .
For e.g. for input 45 the output must be 55 but it is resulting in 0000000 ..
Please tell me where I am wrong .
It is not difficult to recognize palindromes:
int is_palindrome(int a[], int max) {
for (int i = 0; i < max/2; i++) {
if (a[i] != a[max-i-1]) {
return 0;
}
}
return 1;
}
It is not difficult to increment the value:
void next_value(int a[], int max) {
int i = max - 1;
a[i]++;
while (i > 0 && a[i] > 9) {
a[i] = 0;
a[i-1]++;
i--;
}
}
It's easy to display the value:
void show(int a[], int max) {
for (int i = 0; i < max; i++) {
printf("%d", a[i]);
}
printf("\n");
}
With this support it's trivial to find the smallest following palindrome:
void check_pal(int a[], int max) {
while (!is_palindrome(a, max)) {
next_value(a, max);
}
show(a, max);
}
By the way, I would call the function find_pal rather than check_pal.

Sorting output from high to low

I made a simple program that would just show the input as the output.
My main problem is that I want to sort the output from high to low.
Instead of being sorted from high to low, the output is just the same order as the input.
Can someone check my codes and see why it is not sorting.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define size 7
#include<stdlib.h>
struct books
{
int profit;
};
void load(struct books b[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("Enter profit:\n");
scanf("%d", &b[i].profit );
}
}
void print(struct books b[], int n)
{
int i;
for (i = 0; i<n; i++)
{
printf("Profit is:%d\n",b[i].profit);
}
}
void sort(struct books b[], int n)
{
int i; int j;
books t;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-1 ; j++)
if (b[j].profit < b[j + 1].profit)
{
t = b[j];
b[j] = b[j + 1];
b[j+1] = t;
}
}
void main()
{
books b[size];
load(b, size);
print(b, size);
sort(b, size);
system("pause");
}
If you want to print the sorted list, you need to call sort before calling print:
void main()
{
books b[size];
load(b, size);
sort(b, size);
print(b, size);
system("pause");
}
Also, I think you need to define the books struct as
struct books b[size];
if you want to avoid compiler errors.
Finally, to print the list from low to high rather than high to low you can either modify the sorting algorithm as suggested in the another answer, or you can modify the printing algorithm as below:
void print(struct books b[], int n)
{
int i;
for (i = n-1; i>0; i--)
{
printf("Profit is:%d\n",b[i].profit);
}
}
Use something like this(inverted bubble sort):
void inverted_sort(books b[], int size){
int profit;
bool swap;
do{
swap = false;
for (int i= 0; i < (size - 1); i++){
if (b[i].profit < b[i + 1].profit){
profit = b[i].profit;
b[i].profit = b[i + 1].profit;
b[i + 1].profit = profit;
swap = true;
}
}
} while (swap);
}
And remember to change the functions order, inverted_sort() must go before print().
void main()
{
books b[size];
load(b, size);
inverted_sort(b, size);
print(b, size);
}
Hope this helps!
use this :
void sort(struct books b[], int n)
{
int i; int j;
books t;
for (i = 0; i < n; i++)
for (j = i + 1; j < n ; j++)
if (b[j].profit > b[i].profit)
{
t = b[j];
b[j] = b[i];
b[i] = t;
}
}

data types error - program to find the union of two arrays (c)

I am trying to write a program in C to create a union between two arrays, then output the total number of elements in the new array. I am getting the following errors when compiling my code (gcc).
test.c:44:11: error: expected ‘{’ before ‘(’ token
void union(int arrA[], int arrB[], int m, int n)
^
test.c:44:6: error: two or more data types in declaration specifiers
void union(int arrA[], int arrB[], int m, int n)
^
I've checked through for missing semicolons, etc. So unless I'm just missing it, I can't figure out where the issue is coming from. Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n;
int m;
int i;
int k;
printf("Enter the size of array A: ");
scanf("%d",&n);
int arrA[n];
printf("Enter the element(s) of array A: ");
for(i=0;i<n;i++)
{
scanf("%d",&arrA[i]);
}
for(i=0; i<n; i++)
{
printf("%d",arrA[i]);
}
printf("\n");
printf("Enter the size of array B: ");
scanf("%d",&m);
int arrB[m];
printf("Enter the element(s) of array B: ");
for(i=0;i<m;i++)
{
scanf("%d",&arrB[i]);
}
for(i=0; i<m; i++)
{
printf("%d",arrB[i]);
}
printf("\n");
printf("%d\n",k);
return 0;
}
int union(int arrA[], int arrB[], int m, int n)
{
int i = 0;
int j = 0;
int k = 0;
int l = 0;
if(n > m)
{
n = l;
}
else
{
m = l;
}
int arrC[l];
while ((i < n) && (j < m))
{
if (arrA[i] < arrB[j])
{
arrC[k] = arrA[i];
i++;
k++;
}
else if (arrA[i] > arbB[j])
{
arrC[k] = arrB[j];
j++;
k++;
}
else
{
arrC[k] = arrA[i];
i++;
j++;
k++;
}
}
if (i == n)
{
while (j < m)
{
arrC[k] = arrB[j];
j++;
k++;
}
}
else
{
while (i < n)
{
arrC[k] = arrA[i];
i++;
k++;
}
}
return(k);
}
As pointed out by sawims in the comments, union is a reserved word and you had a typo on else if (arrA[i] > arbB[j]), changing the function's name and fixing the typo your code compiles.
http://ideone.com/ubB1eG

Resources