I wanted to ask why the first code functions and the second is not? Both are recursions and should calculate the binomial coefficient when going up the pascal triangel. Thanks.
#include <stdio.h>
#include <stdlib.h>
int rekurs(int n, int k)
{ if((k==0)||(n==0)||(n==k))
{
return 1;
}
else{
return rekurs(n-1,k-1)+ rekurs(n-1,k);
}
}
int main(int argc, const char *argv[])
{
int spalte = atoi(argv[1]);
int zeile= atoi(argv[2]);
printf("%d,%d",zahlen,rekurs(spalte,zeile));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int rekurs(int n, int k)
{ int ergebnis;
if((k==0)||(n==0)||(n==k))
{
ergebnis = 1;
}
else{
return ergebnis = rekurs(n-1,k-1)+ rekurs(n-1,k);
}
}
int main(int argc, const char *argv[])
{
int spalte = atoi(argv[1]);
int zeile= atoi(argv[2]);
printf("%d,%d",zahlen,rekurs(spalte,zeile));
return 0;
}
The second recursive function returns nothing if the expression in the if statement evaluates to true
int rekurs(int n, int k)
{ int ergebnis;
if((k==0)||(n==0)||(n==k))
{
ergebnis = 1;
}
else{
return ergebnis = rekurs(n-1,k-1)+ rekurs(n-1,k);
}
}
At least you need to write
int rekurs(int n, int k)
{ int ergebnis;
if((k==0)||(n==0)||(n==k))
{
ergebnis = 1;
return ergebnis;
}
else{
return ergebnis = rekurs(n-1,k-1)+ rekurs(n-1,k);
}
}
though using the variable ergebnis is redundant.
Also it seems the arguments of the functions must be non-negative values but the functions do not take this into account.
So it will be better to declare the function like
unsigned long long int rekurs( unsigned int n, unsigned int k )
{ if((k==0)||(n==0)||(n==k))
{
return 1;
}
else{
return rekurs(n-1,k-1)+ rekurs(n-1,k);
}
}
Related
Question Link : https://leetcode.com/problems/roman-to-integer/
My code is running fine on my computer but during my attempts to upload it to leet code it shows the following:
Line 20: Char 18: runtime error: index 10 out of bounds for type 'int [10]' [solution.c]
int romanToInt(char * s){
char a[7]={'M','D','C','L','X','V','I'};
int b[7]={1000,500,100,50,10,5,1};
int x;
x=strlen(s);
int c[x];
for(int i=0;i<x;++i)
{
for(int j=0;j<7;++j)
{
if(s[i]==a[j])
{
c[i]=b[j];
break;
}
}
}
int sum=0;
for(int i=0;i<x;++i)
{
if(c[i]<c[i+1]&&i!=x-1)
{
sum-=c[i];
}
else
{
sum+=c[i];
}
}
//printf("%d",sum);
return sum;
}
Running the same code on my computer using it as a custom function works just fine
#include <stdio.h>
#include<string.h>
int romanToInt(char * s);
void main()
{
char a[100];
char* p;
gets(a);
p=a;
int sum= romanToInt(p);
printf("%d",sum);
}
int romanToInt(char * s){
char a[7]={'M','D','C','L','X','V','I'};
int b[7]={1000,500,100,50,10,5,1};
int x;
x=strlen(s);
int c[x];
for(int i=0;i<x;++i)
{
for(int j=0;j<7;++j)
{
if(s[i]==a[j])
{
c[i]=b[j];
break;
}
}
}
int sum=0;
for(int i=0;i<x;++i)
{
if(c[i]<c[i+1]&&i!=x-1)
{
sum-=c[i];
}
else
{
sum+=c[i];
}
}
//printf("%d",sum);
return sum;
}
Why isn't leet code accepting this result?
Write an ascending function (int n, char type, ...) that receives a number n of values and returns 1 if they are in strictly ascending order, otherwise 0. The type character indicates the type of values and can be 'd' - you, 'in' - double.
Here is my try,but i'm kinda stuck.
double asc(int n,char tip,...)
{
va_list va;
va_start(va,tip);
va_list var;
va_start(va,tip);
while(--n)
{
if(tip=='d')
{
int va1=va_arg(va,int);
if( va>va1)
return 1;
else
return 0;
}
else
if(tip=='f')
{
double va2=va_arg(va,double);
if(va>va2)
return 1;
else
return 0;
}
}
return 0;
}
int main(int argc, char const *argv[])
{
asc(3,'d',-1,7,9);
return 0;
}
int asc(int n,char tip,...)
{
va_list va;
va_start(va,tip);
int result = 1;
union
{
int i;
double d;
}val1, val2;
if (n >= 2)
{
for(int i = 0; i < n-1; i++)
{
switch(tip)
{
case 'i':
if(!i) {val1.i = va_arg(va, int);}
val2.i = va_arg(va, int);
if( val1.i > val2.i)
{result = 0; goto function_return;}
val1.i = val2.i;
break;
case 'd':
if(!i) {val1.d = va_arg(va, double);}
val2.d = va_arg(va, double);
if( val1.d > val2.d)
{result = 0; goto function_return;}
val1.d = val2.d;
break;
default:
break;
}
}
}
function_return:
va_end(va);
return result;
}
int main(int argc, char const *argv[])
{
printf("%d\n", asc(4,'d',-1.0,7.0,9.0,0.0));
return 0;
}
This code trying to perform queue, but that's queue has two fields: number and word. My problem is that field "word" prints incorrectly(field "number" is fine)
Expected output:
22
abc
12
efg
654
xyz
Unfortunately output looks like this
https://ibb.co/gjF446F
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#define MAX_capacity 1000
#define Max_len_napis 100
typedef struct{
int number;
char word[];
} data;
data intArray[MAX_capacity];
int peak = 0;
int rear = -1;
int itemCount = 0;
int front() {
return intArray[peak].number;
}
bool isEmpty() {
return itemCount == 0;
}
bool isFull() {
return itemCount == MAX_capacity;
}
int size() {
return itemCount;
}
void insert(data x) {
if(!isFull()) {
if(rear == MAX_capacity-1) {
rear = -1;
}
int indeks = ++rear;
intArray[indeks].number = x.number;
strcpy (intArray[indeks].word, x.word);
itemCount++;
}
}
data remove() {
data dat = intArray[peak++];
if(peak == MAX_capacity) {
peak = 0;
}
itemCount--;
return dat;
}
void print(int N){
for(int i=0;i<N;i++){
data n = remove();
printf("%d\n",n.number);
printf("%s\n",n.word); // that's line doesn't work correctly
}
}
int main() {
data tab[3];
tab[0].number = 22;
strcpy (tab[0].word, "abc");
insert(tab[0]);
tab[1].number = 12;
strcpy (tab[1].word, "efg");
insert(tab[1]);
tab[2].number = 654;
strcpy (tab[2].word, "xyz");
insert(tab[2]);
int siz = size();
print(siz);
return 0;
}
I think that printf("%s\n",n.word) is not work correctly. But if I dont use struct, all works properly.
You need to allocate memory for word. For example like this:
typedef struct{
int number;
char word[100];
} data;
Better way is to allocate memory for word dynamically.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#define MAX_capacity 1000
#define Max_len_napis 100
typedef struct{
int number;
char word[100];
} data;
data intArray[MAX_capacity];
int peak = 0;
int rear = -1;
int itemCount = 0;
int front() {
return intArray[peak].number;
}
bool isEmpty() {
return itemCount == 0;
}
bool isFull() {
return itemCount == MAX_capacity;
}
int size() {
return itemCount;
}
void insert(data x) {
if(!isFull()) {
if(rear == MAX_capacity-1) {
rear = -1;
}
int indeks = ++rear;
intArray[indeks].number = x.number;
strcpy (intArray[indeks].word, x.word);
itemCount++;
}
}
data remove() {
data dat = intArray[peak++];
if(peak == MAX_capacity) {
peak = 0;
}
itemCount--;
return dat;
}
void print(int N){
for(int i=0;i<N;i++){
data n = remove();
printf("%d\n",n.number);
printf("%s\n",n.word); // that's line doesn't work correctly
}
}
int main() {
data tab[3];
tab[0].number = 22;
strcpy (tab[0].word, "abc");
insert(tab[0]);
tab[1].number = 12;
strcpy (tab[1].word, "efg");
insert(tab[1]);
tab[2].number = 654;
strcpy (tab[2].word, "xyz");
insert(tab[2]);
int siz = size();
print(siz);
return 0;
}
For exact question see this link
Here I defined three function, called them selves one within another.
Function call is not being done
#include<stdio.h>
int primegen(int x1,int x2);
int isprime(int j);
int main(){
int x,n1,n2,i;
printf("Enter the number of test cases:");
scanf("%d",&x);
for(i=0;i<x;i++){
printf("enter the starting point and ending point:");
scanf("%d %d",&n1,&n2);
primegen(n1,n2);
}
return 0;
}
int primegen(int x1,int x2){
int k;
if(x2>x1){
for(k=x1;k<x2;k++){
if(isprime(k))
{
printf("%d",k);
}
}
return 0;
}
}
int isprime(int j){
int i,c=0;
for (i=1;i<=j;i++)
{
if(j%i==0){
c++;
}
if(c!=2){
return 0;
}
else{
return 1;
}
}
}
Output
There is no output for this code.
Take following outside loop:
if(c!=2){
return 0;
}
else{
return 1;
}
The problem is in your loop in isprime().
In this you are using the value of c when it is still 0. So, c!=2 would result in true and 0 will be returned and you would not get any answer.
Remove this particular statement from the for loop as you need to calculate total no. of divisors.
for (i=1;i<=j;i++)
{
if(j%i==0)
c++;
}
if(c!=2)
return 0;
else
return 1;
Or you can do like this:
int isprime(int j){
int i,k;
k=sqrt(j); //include math.h for this
for(i=2;i<=k;i++)
{
if(j%i==0)
return 1;
}
return 0;
}
You only need to find any divisor up to the square root of the number.
It's better to return 0 if it is found to be divisible instead of using counter and incrementing it.
int isprime(int j)
{
int i;
for(i=2;i<j;i++)
if((j%i)==0)
return 0;
return 1;
}
#include <stdio.h>
#include <math.h>
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
unsigned int low,high,i=0,j=2,k,x=0,y=0,z;
unsigned long int a[200000],b[200000];
scanf("%d",&low);
scanf("%d",&high);
for(i=low;i<=high;i++)
a[x++]=i;
for(i=2;i<=32000;i++)
b[y++]=i;
i=0;
while(b[i]*b[i]<=high)
{
if(b[i]!=0)
{
k=i;
for(;k<y;k+=j)
{
if(k!=i)
{
b[k]=0;
}
}
}
i+=1;j+=1;
}
for(i=0;i<y;i++)
{
if(b[i]!=0 && (b[i]>=low && b[i]<=sqrt(high)))
printf("%d\n",b[i]);
}
int c=0;
for(i=0;i<y;i++)
{
if(b[i]!=0 && (b[i]>=1 && b[i]<=sqrt(high)))
b[c++]=b[i];
}
int m=a[0];
for(i=0;i<c;i++)
{
z=(m/b[i])*b[i];k=z-m;
if(k!=0)
k += b[i];
for(;k<x;)
{
if(a[k]!=0)
{
a[k]=0;
}
k+=b[i];
}
}
for(i=0;i<x;i++)
{
if(a[i]!=0 && (a[i]>=2 && a[i]<=(high)))
printf("%d\n",a[i]);
}
printf("\n");
}
return 0;
}
I have implemented push pop and get minimum in O(1) complexity. I have seen many solutions in C++. This is an implementation in C itself. Is the following program correct?
#include <stdio.h>
#include <stdlib.h>
int stack[15],aux[15];
int top=-1,count=-1,aux_count=-1,temp_aux=-1;
void push_auxilary(int ele)
{
aux[++aux_count] = ele;
}
void push_stack(int ele)
{
stack[++top]=ele;
}
void push(int ele)
{
if(top < 0 && aux_count < 0)
{
push_auxilary(ele);
push_stack(ele);
}
else
{
if(ele > aux[aux_count])
{
push_auxilary(aux[aux_count]);
push_stack(ele);
}
else
{
push_stack(ele);
push_auxilary(ele);
}
}
}
int pop_stack()
{
return stack[top--];
}
int pop_auxilary()
{
return aux[aux_count--];
}
int pop()
{
int a = pop_stack();
pop_auxilary();
return a;
}
void display()
{
for (int i = top; i >= 0; i--)
{
printf("%d\n",stack[i]);
/* code */
}
}
int get_min()
{
return aux[aux_count];
}
int main(int argc, char const *argv[])
{
int i=0;
push(5);
push(9);
push(1);
push(6);
push(1);
push(54);
push(34);
push(9);
push(3);
push(4);
push(7);
push(12);
push(02);
printf("the %d\n",get_min() );
for (i = aux_count; i >= 0; i--)
{
printf("%d\n",aux[i]);
}
return 0;
}
Looks like it gets the job done in O(1) indeed. Algorithmic-ally correct, but terrible from code reusage point of view.