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.
Related
I need help with the program to display first 4 perfect numbers in the standard output and also funciton perfect(int,int*). Arguments of this function are natural number and the adress where you neeed to write the adders (of the perfect number I suppose). Function has to return 1 if the number is perfect, and 0 if it's not. This is what I've done so far. Help please.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int perfect(int,int*);
int main()
{
int *arr,a;
int i,j;
perfect(a,arr);
}
int perfect(int n,int *arr)
{
int lim=8128,i,sum;
for(n=1;n<=lim;n++)
{
sum=0;
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(n==sum)
printf("%d ",n);
}
}
I think this code is helpful for you. The perfect function returns 1 when perfect otherwise return 0. The global array divisor which is used for collecting the addr. When the perfect function returns 1 then I print the divisor array and initiate the deivisor_count =0. Please have a look:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int divisor[1000], deivisor_count = 0;
int perfect(int n)
{
int sum=0, i, j = 0;
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
divisor[deivisor_count]= i;
deivisor_count = deivisor_count + 1;
}
}
if(n==sum){
return 1;
}
return 0;
}
int main()
{
int i, j =0, is_perfact, n=100000, k;
for (i =2 ; i<=n; i++){
deivisor_count = 0;
is_perfact = perfect(i);
if(is_perfact == 1){
j = j + 1;
for(k = 0; k <deivisor_count; k++){
printf("%d", divisor[k]);
if (deivisor_count -1 == k){
printf("=");
}
else{
printf("+");
}
}
printf("%d\n", i);
}
if (j==4){
break;
}
}
return 0;
}
This is the code i wrote for multiple occurrences in linear search.Can you please help me point out the mistake ? I want the function to store multiple values in the pointer array and then later to print the array
#include <stdio.h>
void linearsearch(int n,int a[n],int x,int count,int *b[count])
{
count=0;
int i;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
count+=1;
}
}
int j=0;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
b[j]=i;
j++;
}
}
}
int main()
{
int n;
scanf("%d",&n);
int a[n];
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int x;
scanf("%d",&x);
int count;
int *b[count];
linearsearch(n,a,x,count,b);
for(i=0;i<count;i++)
{
printf("%d",*b[i]);
}
return 0;
}
I think there are several errors.
count must be initialized, like int count = 0;
count variable is not changed after linearsearch function.
b array should allocated dynamically.
suggested patch is:
#include <stdio.h>
void linearsearch(int n,int a[n],int x,int *count,int **b)
{
count=0;
int i;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
count+=1;
}
}
int j=0;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
*b = realloc(*b, sizeof(int) * (j + 1));
(*b)[j]=i;
j++;
}
}
}
int main()
{
int n;
scanf("%d",&n);
int a[n];
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int x;
scanf("%d",&x);
int count = 0;
int *b = NULL;
linearsearch(n,a,x,&count,&b);
for(i=0;i<count;i++)
{
printf("%d",b[i]);
}
return 0;
}
Well the ideal way would be to allocate the memory dynamically based of the number of x found. But well let's look at the errors first. Maybe after this discussion you can write the code.
b[j]=i;
In the called function let's dissect the type of this.
i is of type int. And b[j] is of type int*. Then you assigned them (type mismatched). Then %d expects an int but you passed something of type int*. This is undefined behavior.
Now there was another flaw you had - you passed count and somehow you expect the value you changed in search will be there in main(). C is pass by value and you changed to a local variable. That change is lost when the called function ends.
#include <stdio.h>
#include <stdlib.h>
int* linearsearch(int n,int* a,int x,int* count)
{
*count=0;
if( n <= 0){
fprintf(stderr, "%s\n","Error" );
exit(1);
}
int *t = malloc(sizeof(*t)*n);
if( t == NULL){
perror("Malloc failed");
exit(1);
}
for(int i = 0; i < n; i++)
if(a[i] == x)
t[(*count)++] = i;
int *temp = realloc(t,sizeof(*temp)* (*count));
if( temp == NULL){
perror("Realloc failed");
exit(1);
}
t = temp;
return t;
}
int main(void)
{
int n;
if( scanf("%d",&n)!= 1){
fprintf(stderr, "%s\n", "Error in input");
exit(1);
}
if( n <= 0 ){
fprintf(stderr, "%s\n", "Error in input : must be greater than 0");
exit(1);
}
int a[n];
for(int i=0; i < n; i++)
if(scanf("%d",&a[i])!=1){
fprintf(stderr, "%s\n","Error in input." );
exit(1);
}
int elmt_to_find;
if( scanf("%d",&elmt_to_find)!= 1){
fprintf(stderr, "%s\n", "Error in input : Element to find(must be integer)");
}
int count;
int *b = linearsearch(n,a,elmt_to_find,&count);
for(int i = 0; i < count; i++)
printf("%d ",b[i]);
printf("%s","\n");
free(b);
return 0;
}
If you want to stick with using VLA for b, you can alter your linearsearch to return count only if b is NULL. Then, you can create b as VLA, and pass it back to linearsearch again to be populated.
int count = linearsearch(n, a, x, 0, 0);
int b[count];
linearsearch(n, a, x, count, b);
Then, your function could look like:
int linearsearch(int n,int a[n],int x,int count,int *b[count])
{
int i;
if(count==0)
{
for(i=0;i<n;i++)
{
if(a[i]==x)
{
count+=1;
}
}
}
if (b==0)
{
return count;
}
int j=0;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
b[j]=i;
j++;
}
}
return count;
}
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);
}
}
}
this is my code
#include <stdio.h>
void show(int*,int);
int sort (int*,int);
int BinSearch(int[],int,int);
int Insertelem(int*,int,int,int*);
int main(void)
{
int array[100]={15,30,40,10,25,5,35,20,45};
int len=9;//length
int *pos=0;//position
int elem=0;//element
int ok=0;
printf("array : ");
show(array,len);
sort(array,len);
printf("array after sorting : ");
show(array,len);
printf("enter the number you want to add :");
scanf("%d",&elem);
*pos=BinSearch(array,elem,len);
ok=Insertelem(array,len,elem,pos);
show(array,len);
return 0;
}
void show(int *array,int len)//print the array
{
for(int i=0;i<len;i++)
{
printf("%d,",array[i]);
}
printf("\n");
}
int sort(int *array,int len)//sort the array
{
int help=0;
for(int i=len-1;i>0;i--)
{
for(int j=0;j<i;j++)
{
if(array[j]>array[j+1])
{
help=array[j];
array[j]=array[j+1];
array[j+1]=help;
}
}
}
return *array;
}
int BinSearch(int array[100],int elem,int len)//searching for the position
{
int first=1;
int last=len-1;
int middle=(first+last)/2;
int pos;
while(first<=last)
{
if(elem<array[middle])
{
last=middle-1;
middle=(first+last)/2;
}
else if(elem>array[middle])
{
first=middle+1;
middle=(first+last)/2;
}
else if(elem==array[middle])
{
break;
}
}
pos=array[middle]+1;
return pos;
}
int Insertelem(int *array,int len,int elem,int *pos)//inserting the element
//in the right position
{
len++;
for(int i=len-1;i>*pos+1;i--)
{
array[i+1]=array[i];
}
elem=array[*pos];
return *array;
}
note : there is no compiler error
I think the problem is in the binsearch function or Insertelem function because I tested all other functions and they are working as expected
when I run this code it works so good until the user enter the element he wont and the program just crash
int pos=-1;//change not pointer
...
pos=BinSearch(array,elem,len);
ok=Insertelem(array,len,elem,&pos);//pos is no need for a pointer, because since not changed.
len += 1;//The length must be changed(or by inside Insertelem)
...
int BinSearch(int array[100],int elem,int len)//searching for the position
{
int first=0;//not 1
...
pos=first;//not midlle, not element of array(array[middle])
return pos;
}
int Insertelem(int *array,int len,int elem,int *pos)
{
...
for(int i=len-1;i>= *pos;i--)//not i>*pos+1
...
array[*pos]=elem;//not elem=array[*pos];
return *array;//??
}
I was trying to print Pascal's Triangle using the following code. But after printing the first '1' the compiler runs into an error and I have to manually stop the execution. What might be the error in the code?
EDIT: fact function has been changed as shown with no difference whatsoever.
I'm using Codeblocks 10.05. A dialog window pops up and says that .exe has stopped working and Windows is searching for a solution.
#include <stdio.h>
void comb(int,int);
int fact(int);
int main()
{
int n,row;
scanf("%d",&n);
int j,k;
for(row=1;row<=n;row++)
{
for(j=0;j<n-row;j++)
{
printf(" ");
}
for (k=0;k<2*row-1;k++)
{
comb(row-1,k);
}
printf("\n");
}
return 0;
}
void comb(int a,int b)
{
if(a==0)
printf("%d",1);
printf("%d",fact(a)/(fact(a-b)*fact(b)));
}
int fact(int num)
{
if(num == 1)
{
return 1;
}
else return num*fact(num-1);
}
Your problem is that fact(0) will go into an infinite loop calling fact(-1), then fact(-2), and so on. Eventually it'll crash due to recursion running out of stack space.
How does fact(0) happen? when fact(a-b) is called when a == b.
This currently happens when row == 2, k == 1.
There are two mistakes that I can see
fact function: it's infinitely recursive
improper bounds of loop which calls comb function. There are row + 1 elements in each row, not 2 * row - 1. your formula works only for the first two rows.
Try this code
#include <stdio.h>
void comb(int,int);
int fact(int);
int main()
{
int n;
printf("Height: ");
scanf("%d",&n);
for(int row = 1; row <= n; row++)
{
if( row == 1)
printf(" ");
for(int j = 0;j < n - row; j++)
{
printf(" ");
}
for (int k = 0;k < row + 1;k++)
{
if ( row != 1)
comb(row,k);
else
{
printf("1");
break;
}
}
printf("\n");
}
return 0;
}
void comb(int a,int b)
{
printf("%d ",fact(a)/(fact(a-b)*fact(b)));
}
int fact(int num)
{
if(num > 1)
return num * fact(num-1);
else
return 1;
}
int main(){
int n,row;
scanf("%d",&n);
int j,k;
for(row=1;row<=n;row++)
{
for(j=0;j<n-row;j++)
{
printf(" ");
}
for (k=0;k<=row-1;k++)
{
comb(row-1,k);
}
printf("\n");
}
return 0;
}
void comb(int a,int b){
printf("%d",fact(a)/(fact(a-b)*fact(b)));
}
int fact(int num){
if(num < 2)
return 1;
else
return num*fact(num-1);
}