Function string_to_table is breaking on 3rd if c - 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;
}

Related

C language- C90-printing multiple copies of specific shape next to each other

I have created a program which takes as an input:
a number which determines the shape the user wants to be printed,lets say copies
a number which the determines the 'size' of the shape
and a number which determines how many times the shape will be printed..
What I have tried is a for loop in main() function below each 'if' but the copies are printed one down another..I also came up with the idea of printing the first line <'copies'> times with the necessary padding but that practice seems to be different for every shape (and yes I cannot implement this logic in my code)...What can I do?
/*
Version 5*/
#include <stdio.h>
#include <stdio.h>
int getchoice(void);
int getsize(void);
void oof(int size,int copies);
void printrhombus(int size);
void printrighttriangle(int size);
void printisoscelestriangle(int size);
void printspace(void);
void printsymbol(void);
void printnewline(void);
void printrowno(int i);
int getcopies(void);
int main(void)
{
int choice = 0;
int size;
int copies;
for(; (choice = getchoice()) != -1 ;)
{
size = getsize();
copies = getcopies();
if(choice == 0)
{
oof(size,copies);
}
else if(choice == 1)
{
printrhombus(size);
}
else if(choice == 2)
{
printrighttriangle(size);
}
else if (choice == 3)
{
printisoscelestriangle(size);
}
}
return 0;
}
int getchoice(void)
{
int choice;
printf("Please enter which shape you want to be printed (0-3):\n");
scanf("%d",&choice);
printf("Your choice is %d\n",choice);
return choice;
}
int getsize(void)
{
int size;
printf("Please enter the number of rows-the size of your shape:\n");
scanf("\n%d",&size);
printf("Your size is %d\n",size);
return size;
}
void oof(int size,int copies)
{
int i,j,k,l;
for(i = 0; i< size; i++)
{
for(l = 0; l<copies; l++)
{
for(j = 0;j<i;j++)
{
printsymbol();
}
}
for(k =0;k<size;k++)
{
if(k == 0 || i == 0 || k == size-1 || i == size-1)
{
printrowno(i);
}
else printsymbol();
}
printnewline();
}
}
void printrhombus(int size)
{
int i,j;
int rows1 = (size/2)+1;
for(i = 1;i<=rows1;i++)
{
for(j = 1;j<=((2*rows1)-1);j++)
{
if((j==(rows1+(i-1))) || j==(rows1-(i-1)))
{
printrowno(i);
}
else if(j>rows1+i-1)
{
printspace();
}
else if(j<rows1+i-1 && j!= rows1+1-i)
{
printsymbol();
}
}
printnewline();
}
for(i = rows1-1;i>=1;i--)
{
for(j=1;j<=((2*rows1)-1);j++)
{
if((j==(rows1+(i-1))) || j==(rows1-(i-1)))
{
printrowno(size-i+1);
}
else if(j>rows1+i-1)
{
printspace();
}
else if(j<rows1+i-1 && j!= (rows1+1-i))
{
printsymbol();
}
}
printnewline();
}
}
void printrighttriangle(int size)
{
int rowno,colno;
for(rowno = 1; rowno<=size; rowno++)
{
for(colno = 1; colno<= rowno; colno++)
{
if ((colno==1) || (rowno == size) || colno == rowno)
{
printrowno(rowno);
}
else if(colno>rowno)
{
printspace();
}
else
{
printsymbol();
}
}
printnewline();
}
}
void printisoscelestriangle(int size)
{
int i,j;
for(i = 1; i<= size; i++)
{
for(j = 1;j<= ((2*size)-1);j++)
{
if(j ==(size-(i-1)) ||j == (size+(i-1))|| i == size)
{
printrowno(i);
}
if (j<size-(i-1))
{
printspace();
}
if(j>(size-(i-1)) && j<(size +(i-1))&& i!= size)
{
printsymbol();
}
}
printnewline();
}
}
void printspace(void)
{
printf(" ");
return;
}
void printsymbol(void)
{
printf("-");
return;
}
void printrowno(int i)
{
printf("%d",i);
}
void printnewline(void)
{
printf("\n");
return;
}
int getcopies(void)
{
int copies;
printf("Please enter number of copies:\n");
scanf("\n%d",&copies);
return copies;
}

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

Struct with pointers changes value without reassigning

I debugged the program below. It finds the correct result but then mmat in the recursive function ri gets changed without reassigning it by this part of the code pmat[q][h].ptes=NULL; and i get as final result that all the non pre-assigned pointers are null. I got the same problem with a global variable mat **mmat instead of passing it by reference in ri. Does anybody know the reason?
typedef struct tes_{
struct seg_{
char c;
int v;
}seg[2];
}tes;
typedef struct mat_{
tes *ptes;
int rot;
} mat;
int max=0;
int main()
{
FILE *f; char frase[M], buff[M]; int n,i,j,r,c,q=0,z=0,e=0,*pos,*esc; tes *vtes; mat **pmat, **mmat;
printf("Inserire nome file input tessere: "); scanf("%s",frase);
f=fopen(frase,"r");
if (f==NULL) exit(1);
fgets(frase,50,f); sscanf(frase,"%d",&n);
vtes=malloc(n*sizeof(tes));
for (i=0;i<n;i++) {fgets(frase,M,f); sscanf(frase,"%c %d %c %d",&(vtes[i].seg[0].c),&(vtes[i].seg[0].v),&(vtes[i].seg[1].c),&(vtes[i].seg[1].v));}
fclose(f);
printf("Inserire nome file input scacchiera: "); scanf("%s",frase);
f=fopen(frase,"r");
if (f==NULL) exit(1);
fgets(frase,M,f); sscanf(frase,"%d %d",&r,&c);
esc=(int*)malloc(r*c*sizeof(int));
pmat=(mat **)malloc(r*sizeof(mat *));
mmat=(mat **)malloc(r*sizeof(mat *));
for (i=0;i<r;i++) {pmat[i]=(mat *)malloc(c*sizeof(mat)); mmat[i]=(mat *)malloc(c*sizeof(mat));}
for (i=0;i<r;i++) {
fgets(frase,M,f); q=0;
for (j=0;j<c;j++) {
z=0;
while (frase[q]!='/') {
buff[z]=frase[q]; q++; z++;
}
q++; buff[z]='\0'; z=0;
if (atoi(buff)==-1) pmat[i][j].ptes=NULL;
else {pmat[i][j].ptes=&(vtes[atoi(buff)]); esc[e]=atoi(buff); e++;}
while (frase[q]!=' ' || frase[q]=='\n' || frase[q]=='\0') {
buff[z]=frase[q]; q++; z++;
}
q++; buff[z]='\0'; pmat[i][j].rot=atoi(buff);
}
}
pos=(int *)malloc((n-e)*sizeof(int)); q=0;
for(i=0;i<n;i++) { // pos contiene tutte le tessere inseribili
for (j=0;j<e;j++) if(esc[j]==i) break;
if (j>=e) {pos[q]=i; q++;}
}
ri(pmat,&mmat,vtes,pos,n-e,r,c);
printf("Max valore %d\n",max);
for (i=0;i<r;i++) {
for (j=0;j<c;j++) {
printf("%c %d // %c %d\t",mmat[i][j].ptes->seg[(0+mmat[i][j].rot)%2].c,mmat[i][j].ptes->seg[(0+mmat[i][j].rot)%2].v,mmat[i][j].ptes->seg[(1+mmat[i][j].rot)%2].c,mmat[i][j].ptes->seg[(1+mmat[i][j].rot)%2].v);
}
printf("\n");
}
return 0;
}
void ri(mat **pmat, mat ***mmat, tes *vtes, int *pos, int i, int r, int c){
int j,q,h,z,*pos2;
if (i==0) {
if(check(pmat,vtes,r,c)) {
*mmat=pmat;
}
return;
}
for (j=0;j<i;j++) {
for (q=0;q<r;q++) {
for (h=0;h<c;h++) {
if (pmat[q][h].ptes==NULL) break;
}
if (h<c) break;
}
if (q==r && h==c) return;
pos2=malloc((i-1)*sizeof(int));
for (z=0;z<i;z++){
if (z<j) pos2[z]=pos[z];
else if (z>j) pos2[z-1]=pos[z];
}
pmat[q][h].ptes=&(vtes[pos[j]]);
pmat[q][h].rot=0;
ri(pmat,mmat,vtes,pos2,i-1,r,c);
pmat[q][h].rot=1;
ri(pmat,mmat,vtes,pos2,i-1,r,c);
free(pos2);
if ((*mmat)[0][1].ptes==NULL) {
printf("prob");
}
pmat[q][h].ptes=NULL;
if ((*mmat)[0][1].ptes==NULL) {
printf("prob");
}
}
}
int check(mat **pmat,tes *vtes, int r, int c){
int i,j, cont=0;
for (i=0;i<r;i++) {
for (j=0;j<c-1;j++) {
if (pmat[i][j].ptes->seg[(0+pmat[i][j].rot)%2].c!=pmat[i][j+1].ptes->seg[(0+pmat[i][j+1].rot)%2].c) break;
}
if (j==c-1) {for (j=0;j<c;j++) cont+=pmat[i][j].ptes->seg[(0+pmat[i][j].rot)%2].v;}
}
for (i=0;i<c;i++) {
for (j=0;j<r-1;j++) {
if (pmat[j][i].ptes->seg[(1+pmat[j][i].rot)%2].c!=pmat[j+1][i].ptes->seg[(1+pmat[j+1][i].rot)%2].c) break;
}
if (j==r-1) {for (j=0;j<r;j++) cont+=pmat[j][i].ptes->seg[(1+pmat[j][i].rot)%2].v;}
}
if (cont>max) {
max=cont;
return 1;
}
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;
}

Nptel-pascal programming assignment

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

Resources