Nptel-pascal programming assignment - c

i am pursuing the course on programming and data structures in nptel's MOOC.
A programming assignment in the course requires us to calculate sum of selected coefficients.
Now the program i wrote calculated the answer , but i am experiencing a runtime error.
Now i had this inclination of using gets() instead of scanf() so as to speed up my input.
how do i do that?
the code is as follows:
#include<stdio.h>
int combi(int ,int);
long fact(int);
int main()
{
int r,i,v[20],p[20],t,l=1,b=0,sum=0,a=0,flag=0;
char ch='a';
scanf("%d",&r);
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&v[0]);
if(getchar()==' ')
{
if(r<v[0])
{
while(ch!='\n')
{
scanf("%d",&v[l]);
l++;
if(getchar()=='\n')
{
p[a]=-1;
a++;
l=1;
break;
}
}
}
else
{
while(ch!='\n')
{
scanf("%d",&v[l]);
if(v[l]>v[0])
{
flag=1;
}
l++;
if(getchar()=='\n')
{
if(flag>0)
{
p[a]=-1;
a++;
l=1;
sum=0;
flag=0;
break;
}
else
{
for(b=1;b<l;b++)
{
sum=sum+combi(v[0],v[b]);
}
p[a]=sum;
a++;
sum=0;
l=1;
break;
}
}
}
}
}
}
for(i=0;i<t;i++)
{
printf("%d\t ",p[i]);
}
printf("\n");
}
int combi(int x,int y)
{
int a=fact(x)/(fact(x-y)*fact(y));
return a;
}
long fact(int z)
{
int i=1;
long f=1;
while(i<=z)
{
f=f*i;
i++;
}
return f;
}

Related

Why is my code giving right output in codeBlocks but wrong output in HackerRank?

I am trying to solve a problem of HackerRank. The problem wants me to find out if a string can be changed into a palindrome string or not with a certain number of changing characters of that string. That certain number will be given by input. I am giving the link of full description of the problem below.
In my code, I first checked if the string is already palindrome or not. Then I checked some cases under the first checking for the given number of changes to find out if that string can be changed into palindrome after changing the characters.
But I am getting different output for my code. Output that I found from code blocks is not the same as the output which I found in HackerRank.
Here is the link of the full problem statement
my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print(char str[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%c",str[i]);
}
}
void cpy(char qtr[],char str[],int n)
{
int i;
for(i=0;i<n;i++)
{
qtr[i]=str[i];
}
}
void rev(char str[],int n)
{
int i;
char qtr[100000];
for(i=0;i<n;i++)
{
qtr[i]=str[n-1-i];
}
for(i=0;i<n;i++)
{
str[i]=qtr[i];
}
}
int main()
{
char str[100],str2[100000],qtr[100000],c;
int n,k,i;
scanf("%d",&n);
scanf("%d",&k);
fflush(stdin);
gets(str);
cpy(qtr,str,n);
rev(str,n);
cpy(str2,str,n);
int same=0;
if(strcmp(str2,qtr)==0)
{
same=1;
}
if(k==n)
{
c=qtr[i]+1;
for(i=0;i<k;i++)
{
if(qtr[i]==c)
{
c=c+1;
i=-1;
continue;
}
}
for(i=0;i<k;i++){qtr[i]=c;}
print(qtr,n);
return 0;
}
if(same==1)
{
if(n%2!=0)
{
if(qtr[n/2]!='9'){qtr[n/2]='9';}
else
{qtr[n/2]='1';}
k--;
for(i=0;i<n;i++)
{
if(k<0){printf("-1");break;}
if(k==0){printf(qtr,n);break;}
if(qtr[i]=='9')
{
qtr[i]='1';
qtr[n-1-i]='1';
k=k-2;
}
else
{
qtr[i]='9';
qtr[n-1-i]='9';
k=k-2;
}
}
}
else
{
for(i=0;i<n;i++)
{
if(k<0){printf("-1");break;}
if(k==0){print(qtr,n);break;}
if(qtr[i]=='9')
{
qtr[i]='1';
qtr[n-1-i]='1';
k=k-2;
}
else
{
qtr[i]='9';
qtr[n-1-i]='9';
k=k-2;
}
}
}
}
else
{
if(n%2==0)
{
int arr[n];
for(i=0;i<n;i++){arr[i]=0;}
while(k>=0)
{
int g=0;
for(i=0;i<n/2;i++)
{
if(qtr[i]!=qtr[n-i-1])
{
arr[i]=1;
arr[n-i-1]=1;
qtr[i]=qtr[n-i-1];
k--;
g=1;
}
}
if(k==0){print(qtr,n);return 0;}
if(k%2!=0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==1)
{
arr[i]=2;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k--;g=1;
break;
}
}
}
if(k==0){print(qtr,n);return 0;}
if(k%2==0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==0)
{
arr[i]=3;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k=k-2;g=1;
break;
}
}
}
if(k==0){print(qtr,n);return 0;}
if(g==1){printf("-1");return 0;}
}
printf("-1");return 0;
}
else
{
int arr[n];
for(i=0;i<n;i++){arr[i]=0;}
while(k>=0)
{
int g=0;
for(i=0;i<n/2;i++)
{
if(qtr[i]!=qtr[n-i-1])
{
arr[i]=1;
arr[n-i-1]=1;
qtr[i]=qtr[n-i-1];
k--;
g=1;
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(k%2!=0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==1)
{
arr[i]=2;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k--;g=1;
break;
}
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(k%2==0)
{
for(i=0;i<n/2;i++)
{
if(arr[i]==0)
{
arr[i]=3;
qtr[i]=qtr[i]+1;
qtr[n-i-1]= qtr[i];
k=k-2;g=1;
break;
}
}
}
if(k==1){qtr[n/2]=qtr[n/2]+1;k--;print(qtr,n);return 0;}
if(k==0){print(qtr,n);return 0;}
if(g==1){printf("-1");return 0;}
}
printf("-1");return 0;
}
}
}

Getting negative numbers or zero as output of factorial in C

I've written a very simple program which calculate the factorial of a number, the problem is that from 30 to 60 approximately, it returns a negative number and from 70 it returns 0.
I don't what I've done wrong. Could this problem depend on the computing power of my computer?
Here's the code:
#include <stdio.h>
int main(){
int x, i;
long long int f = 1;
printf("Insert a number:");
scanf("%d", &x);
if (x == 0){
printf("0! = 1");
}
else {
for (i = 1; i <= x; i++){
f *= i;
}
printf("%d! รจ = %lli", x, f);
}
return 0;
}
Here is my code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void Carry(int bit[],int pos)
{
int i,carray=0;
for(i=0;i<=pos;i++)
{
bit[i]+=carray;
if(bit[i]<=9)
{
carray=0;
}
else if(bit[i]>9&&i<pos)
{
carray=bit[i]/10;
bit[i]%=10;
}
else if(bit[i]>9&&i>=pos)
{
while(bit[i]>9)
{
carray=bit[i]/10;
bit[i]%=10;
i++;
bit[i]=carray;
}
}
}
}
int main()
{
int num,pos,digit,i,j,m,n;
double sum=0;
int *fact;
printf("input want to calculute factorial num:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
sum+=log10(i);
}
digit=(int)sum+1;
if(!(fact=(int *)malloc((digit+1)*sizeof(int))))
{
printf("malloc failed\n");
return 0;
}
for(i=0;i<=digit;i++)
{
fact[i]=0;
}
fact[0]=1;
for(i=2;i<=num;i++)
{
for(j=digit;j>=0;j--)
{
if(fact[j]!=0)
{
pos=j;
break;
}
}
for(j=0;j<=pos;j++)
{
fact[j]*=i;
}
Carry(fact,pos);
}
for(j=digit;j>=0;j--)
{
if(fact[j]!=0)
{
pos=j;
break;
}
}
m=0;
n=0;
for(i=pos;i>=0;i--)
{
printf("%d",fact[i]);
m++;
if(m%4==0)
{
printf(" ");
}
if(m==40)
{
printf("\n");
m=0;
n++;
if(n==10)
{
printf("\n");
n=0;
}
}
}
printf("\n\n");
return 0;
}

Why does the program shows memory limit exceeded in test 6 and runtime error in test 8,9,10 when i use local array variable?

Its a easy sorting problem.The problem link is https://www.hackerearth.com/practice/algorithms/sorting/quick-sort/practice-problems/algorithm/kings-race-8/
When i use global array variable the program get accepted.But when i use local array variable memory limit exceeded in case 6 and runtime error in cases 8,9,10.Why this is happened?
My code with local array variable:
#include<stdio.h>
void Quick_Sort(int a[][2],int Start,int End)
{
if(Start<End)
{
int Piv_pos=Partition(a,Start,End);
Quick_Sort(a,Start,Piv_pos-1);
Quick_Sort(a,Piv_pos+1,End);
}
}
int Partition(int a[][2],int Start,int End)
{
int i=Start+1,j,temp;
int Pivot=a[Start][0];
for(j=Start+1;j<=End;j++)
{
if(a[j][0]<Pivot)
{
temp=a[j][0];
a[j][0]=a[i][0];
a[i][0]=temp;
temp=a[j][1];
a[j][1]=a[i][1];
a[i][1]=temp;
i++;
}
}
temp=a[Start][0];
a[Start][0]=a[i-1][0];
a[i-1][0]=temp;
temp=a[Start][1];
a[Start][1]=a[i-1][1];
a[i-1][1]=temp;
return i-1;
}
int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}
int main()
{
int T,i,j,N,K,prince_ind;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&K);
int a[N][2],h[K];
for(i=0;i<N;i++)
{
scanf("%d",&a[i][0]);
a[i][1]=i;
}
Quick_Sort(a,0,N-1);
/* for(i=0;i<N;i++)
{
printf("%d ",a[i][0]);
}*/
for(i=0;i<K;i++)
{
scanf("%d",&h[i]);
}
for(i=0,j=0;i<K&&j<N;i++)
{
prince_ind=a[j][1];
while(a[j][0]<h[i]&&j<N)
{
prince_ind=min(prince_ind,a[j][1]);
j++;
}
}
while(j<N)
{
prince_ind=min(prince_ind,a[j][1]);
j++;
}
printf("%d\n",prince_ind);
}
return 0;
}
My code with global array variable:
#include<stdio.h>
int a[1000000][2],h[1000000];
void Quick_Sort(int Start,int End)
{
if(Start<End)
{
int Piv_pos=Partition(Start,End);
Quick_Sort(Start,Piv_pos-1);
Quick_Sort(Piv_pos+1,End);
}
}
int Partition(int Start,int End)
{
int i=Start+1,j,temp;
int Pivot=a[Start][0];
for(j=Start+1;j<=End;j++)
{
if(a[j][0]<Pivot)
{
temp=a[j][0];
a[j][0]=a[i][0];
a[i][0]=temp;
temp=a[j][1];
a[j][1]=a[i][1];
a[i][1]=temp;
i++;
}
}
temp=a[Start][0];
a[Start][0]=a[i-1][0];
a[i-1][0]=temp;
temp=a[Start][1];
a[Start][1]=a[i-1][1];
a[i-1][1]=temp;
return i-1;
}
int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}
int main()
{
int T,i,j,N,K,prince_ind;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&K);
//int a[N][2],h[K];
for(i=0;i<N;i++)
{
scanf("%d",&a[i][0]);
a[i][1]=i;
}
Quick_Sort(0,N-1);
/* for(i=0;i<N;i++)
{
printf("%d ",a[i][0]);
}*/
for(i=0;i<K;i++)
{
scanf("%d",&h[i]);
}
for(i=0,j=0;i<K&&j<N;i++)
{
prince_ind=a[j][1];
while(a[j][0]<h[i]&&j<N)
{
prince_ind=min(prince_ind,a[j][1]);
j++;
}
}
while(j<N)
{
prince_ind=min(prince_ind,a[j][1]);
j++;
}
printf("%d\n",prince_ind);
}
return 0;
}

Function string_to_table is breaking on 3rd if c

I am trying to create program to add two matrixes. After typing input like [12 4] program crashes when function strcat starts.
I have no idea what is wrong. func.h consists of stdio.h, _mingw.h,stdlib.h and string.h
#include"func.h"
void string_to_table(double **matrix, char inpt[])/*problematic function*/
{
printf("convertion start");
int i=strlen(inpt);
printf("%i",i);
int j=1;
int k=0,l=0;
char num[128];
double converted=0;
printf("breakpoint1");
while(j<(i-1))
{
if(inpt[j]==' ')
{
printf("first if");
converted=atof(num);
num[0]='\0';
matrix[k][l]=converted;
++l;
printf("breakpoint2");
}
else if(inpt[j]==';')
{
printf("second if");
converted=atof(num);
num[0]='\0';
matrix[k][l]=converted;
++k;
l=0;
}
else
{
printf("third if");
strcat(num,inpt[j]);/*place when everything crashes*/
}
++j;
}
printf("convert0 end");
}
void add_matrix(double **matrix1, double **matrix2,int i,int j)
{
int k=0;
int l=0;
while(k<i)
{
while(l<j)
{
matrix1[k][l]+=matrix2[k][l];
++l;
}
l=0;
++k;
}
int matrixproccesing(int *i,int *j, char m[])/*sprawdzanie poprawnosci wejscia*/
{
printf("macro start");
int columnnum=0,rownum=0,x=0,piv=0,check=0;
int textsize=strlen(m);
printf("%i",i);
printf("loop start");
printf("%i",textsize);
while(x<(textsize-1))
{
printf("%i",x);
printf("\n");
if(x==0)/*czy poczatek to [*/
{
if(m[x]!='[')
return 0;
}
else if(x==(textsize-2))/*czy koniec to]*/
{
printf("kohec");
if(m[x]==']')
break;
return 0;
}
else if((m[x]>47&&m[x]<58)||(m[x]==' ')||(m[x]=='.')||(m[x]==';')||(m[x]=='-'))/*czy liczba*/
{
if(m[x]==';')/*czy ilosc liczb w rzedzie taka sama*/
{
if(check==0)
{
check=columnnum;
}
else if(check!=columnnum)
{
return 0;
}
printf("colnum");
columnnum=0;
rownum++;
}
else if(m[x]==' ')/*czy nowa liczba/kolumna */
{
columnnum++;
}
}
++x;
}
*i=(check+1);
*j=(columnnum+1);
printf("macro end");
return 1;
}
int is_same_size(int a, int b,int c ,int d)/*test rozmiaru*/
{
if((a==c)&(b==d))
return 1;
return 0;
}
void print_da_matrix(double **matrix, int i, int j)
{
int k=0,l=0;
printf("[ ");
while(k<i)
{
while(l<j)
{
printf("%f",matrix[k][l]);
printf(" ");
}
printf(";");
l=0;
if(k<(i-1))
++k;
}
printf("]");
}
void release_the_memory(double **matrix, int i)
{
int k=0;
while(k<i)
{
free(matrix[k]);
++k;
}
free(matrix);
matrix=NULL;
}
}
int main()
{
int i=0,j=0,m1=0,m2=0,tabcr=0;
char matrix[512];
fgets(&matrix,511,stdin);
double **matrix1;
double **matrix2;
if(!matrixproccesing(&i,&j,matrix))
{
printf("zle wejscie");
return 0;
}
matrix1=(double**)malloc(i*sizeof(double *));
while(tabcr<j)
{
matrix1[tabcr]=(double*)malloc(j*sizeof(double));
++tabcr;
}
string_to_table(matrix1,matrix);
printf("\n");
printf("podaj druga macierz");
fgets(&matrix,511,stdin);
if(!matrixproccesing(&m1,&m2,matrix))
{
printf("zle wejscie");
return 0;
}
tabcr=0;
if(!is_same_size(i,j,m1,m2))
{
printf("matrixes have different size.");
return 0;
}
matrix2=(double**)malloc(i*sizeof(double *));
while(tabcr<j)
{
matrix2[tabcr]=(double*)malloc(j*sizeof(double));
++tabcr;
}
string_to_table(matrix2,matrix);
add_matrix(matrix1,matrix2,i,j);
/* print_da_matrix(matrix1,i,j);
release_the_memory(matrix1,i);
release_the_memory(matrix2,i);*/
return 0;
}

Prime Generator(spoj)

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

Resources