Heap Code Error - c

I am getting an error in the following code. I am building a heap and for that I use the buildup and move-up functions whereas for deleting the values I use move-down and deletion functions. And for inserting the values I use insert function.
My output is showing error for the deletion part.
#include<stdio.h>
#include<stdlib.h>
int a[100];
int size;
void display();
void moveup(int);
void insert(int);
void buildup();
int deletion();
void movedown(int);
int h;
void main()
{
size=5;
int i,j;
printf("enter the elements\n");
for(i=1;i<=5;i++)
{
scanf("%d",&a[i]);
}
a[0]=32767;
printf("\n");
buildup();
display();
insert(9);
display();
printf("\n after deletion\n");
deletion();
display();
}
void buildup()
{
int i;
for(i=2;i<=size;i++)
moveup(i);
}
void moveup(int i)
{
int p,temp;
temp=a[i];
p=i/2;
while(temp>a[p])
{
a[i]=a[p];
i=p;
p=i/2;
}
a[i]=temp;
}
void display()
{
int i;
for(i=1;i<=size;i++)
printf("%3d\n",a[i]);
}
void insert(int t)
{
printf("\n after insertion\n");
if(size==99)
printf("insertion not possible");
a[++size]=t;
moveup(size);
}
int deletion()
{
int t;
t=a[1];
h=a[size--];
a[1]=h;
movedown(1);
return t;
}
void movedown(int i)
{
int l;
int r;
i=2*i;
r=l+1;
while(r<=size)
{
if(h>a[l]&&h>a[r])
{
a[i]=h;
return;
}
else
{
if(a[l]>a[r])
{
a[i]=a[l];
i=l;
}
else
{
a[i]=a[r];
i=r;
}
}
l=2*i;
r=l+1;
}
if(l<=size&&a[l]>h)
{
a[i]=a[l];
i=l;
}
a[i]=h;
}

Related

why this show() function produce two * and the element will stop sometimes .There is no error in the code

This is a plane game function I wrote. I use a two-dimensional array to represent the game variables but The running result is abnormal, and * will jump suddenly.
And there will be two * , at the same time, and the plane also will stop
There should be no two * in the process of traversing the two-dimensional array. I tried to modify the position of * but I still couldn't.It's OK to run part of the code alone, but when you use the key operation, the program makes an error, but I don't know what's wrong
#include<stdio.h>
#include <windows.h>
#include<stdlib.h>
#include<conio.h>
#define enemynum 3
int element [20][30];
int position_x,position_y;
int enemy_x[enemynum],enemy_y[enemynum];
int score;
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()
{ element[20][30]={0};
position_x=10;position_y=15;
element[position_x][position_y]=1;
for(int k=0;k<enemynum;k++)
{
enemy_x[k]=rand()%3;enemy_y[k]=rand()%20;
element[enemy_x[k]][enemy_y[k]]=3;
}
HideCursor();
}
This is an encapsulated pointer callback function. I don't think we need to consider the above functions
void show()
{ int i,j;
gotoxy(0,0);
for(i=0;i<20;i++)
{
for(j=0;j<30;j++)
{ if(element[i][j]==1)
{
printf("*");
}else if(element[i][j]==3)
{
printf("#");
}
else
printf(" ");
}
printf("\n");
}
}
void updatewhithout()
{
int i,j;
for(i=0;i<20;i++)
{
for(j=0;j<30;j++)
{
if(element[i][j]==2)
{
element[i][j]=0;
if(i>0)
element[i-1][j]=2;
}
}
} static int flag;
if(flag<20)
flag++;
if(flag==20)
{ for(int k=0;k<enemynum;k++)
{
if(enemy_x[k]==20)
{
enemy_x[k]=rand()%3;
enemy_y[k]=rand()%20;
}
element[enemy_x[k]][enemy_y[k]]=0;
enemy_x[k]++;
element[enemy_x[k]][enemy_y[k]]=3;
flag=0;
}
}
}
void updatewhith()
{ char ch;
if( kbhit())
ch=getch();
if(ch=='a')
{ element[position_x][position_y]=0;
position_y--;
element[position_x][position_y]=1;
}
if(ch=='d')
{ element[position_x][position_y]=0;
position_y++;
element[position_x][position_y]=1;
}
}
int main()
{startup();
while(1)
{show();
updatewhithout();
updatewhith();
}
}

Tried Knap snap problem but its not working

#include<stdio.h>
int max(int a,int b)
{
if(a>b)
return a;
return b;
}
void knacksnap(int n,int a[][n+1],int* val,int* weight,int maxweight)
{
if(n==0 || maxweight==0)
{
a[maxweight][n]=0;
return;
}
if(a[maxweight][n]!=-1)
return;
if(weight[n-1]>maxweight)
{
knacksnap(n-1,a,val,weight,maxweight);
a[maxweight][n]=a[maxweight][n-1];
}
else
{
knacksnap(n-1,a,val,weight,maxweight-weight[n-1]);
knacksnap(n-1,a,val,weight,maxweight);
a[maxweight][n]=max(val[n-1]+a[maxweight-weight[n-1]][n-1],a[maxweight][n-1]);
}
}
int main()
{
int n;
scanf("%d",&n);
int val[n],weight[n];
int i;
for(i=0;i<n;i++)
scanf("%d",&val[i]);
for(i=0;i<n;i++)
scanf("%d",&weight[i]);
int maxweight;
scanf("%d",&maxweight);
int a[maxweight+1][n+1];
int j;
for(i=0;i<maxweight+1;i++)
{
for(j=0;j<n+1;j++)
{
a[i][j]=-1;
}
}
knacksnap(n,a,val,weight,maxweight);
printf("\n");
printf("%d",a[maxweight][n]);
}
It not taking updated values by the knacksnap function. for example the inner knacksnap funtions are not generating correct values. eventhough their values are updated its not taking them. can someone please help

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

Why does my function go into infinite loop sometimes?

I have the following code. It is running fine. But sometimes the del and ins functions are going into infinite loop but sometimes working fine. The readt function is working fine, still I have included it for your reference. What is the problem with my del and ins? Is there any memory leak?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<math.h>
#include<unistd.h>
struct node
{ int info;
struct node *link;
};
typedef struct node m;
m *search(int,m*);
m *del(int,m*);
m *ins(int,int,m*);
int posof(int,m*);
int readt(m *t_c,char path[])
{ int t,szt=0;
FILE *tfile;
char ch;
char filename[]="/temp.txt";
strcat(path,filename);
tfile=fopen(path,"r");
if(tfile==NULL)
exit(0);
fseek(tfile, 0, SEEK_END);
szt = ftell(tfile);
fseek(tfile,0,SEEK_SET);
if(szt!=0)
{ while(1)
{ fscanf(tfile,"%d%c",&t,&ch);
t_c->info=t;
t_c->link=(m*)malloc(sizeof(m));
t_c=t_c->link;
if(ch==';')
break;
}
}
t_c->link=NULL;
//free(t_c);
fclose(tfile);
return 0;
}
m *search(int Noftarget,m *t_c)
{ int i,p1,p2;
srand(time(NULL));
for(i=0;i<100;i++)
{ p1=(1+rand()%(Noftarget));
p2=(1+rand()%(Noftarget));
t_c=del(p1,t_c);
t_c=ins(p1,p2,t_c);
break;
}
return t_c;
}
m *del(int target,m *t_h)
{ m *t_c;
int j=1,i;
t_c=t_h;
i=posof(target,t_h);
if(i==1)
{ t_c=t_c->link;
t_h=t_c;
}
else
{ while(j<i-1)
{ t_c=t_c->link;
j++;
}
t_c->link=t_c->link->link;
}
return t_h;
}
m *ins(int target,int position,m *t_h)
{ int j=0;
m *swaptarget,*t_c;
t_c=t_h;
swaptarget=(m*)malloc(sizeof(m));
swaptarget->info=target;
if(position==1)
{ swaptarget->link=t_c;
t_h=swaptarget;
}
else
{ while(j<position-2)
{ t_c=t_c->link;
j++;
}
swaptarget->link=t_c->link;
t_c->link=swaptarget;
}
free(swaptarget);
return t_h;
}
int posof(int p1,m *t_c)
{ int i=1,a=0;
while(t_c->link!=NULL)
{ if(p1==t_c->info)
{ a=i;
break;
}
t_c=t_c->link;
i++;
}
return a;
}
int main()
{ int Noftarget=8,j,r=1,count=0,noi,szd_n=0,i=0,sz;
char cwd[200];
m *t_h;
getcwd(cwd, sizeof(cwd));
t_h=(m*)malloc(sizeof(m));
readt(t_h,cwd);
t_h=search(Noftarget,t_h);
free(t_h);
return 0;
}
and the content of temp file is:
1,2,3,4,5,6,7,8;
The program contains memory leaks. Memory is allocating iteratively inside the while loop but only one pointer is removing at the end. Need to remove all allocations. And there is no need to free any pointer at the ins function rather del function needs the free operation of deleted pointer. The modified code is here:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<math.h>
#include<unistd.h>
struct node
{ int info;
struct node *link;
};
typedef struct node m;
m *search(int,m*);
m *del(int,m*);
m *ins(int,int,m*);
int posof(int,m*);
int readt(m *t_c,char path[])
{ int t,szt=0;
FILE *tfile;
char ch;
char filename[]="/temp.txt";
strcat(path,filename);
tfile=fopen(path,"r");
if(tfile==NULL)
exit(0);
fseek(tfile, 0, SEEK_END);
szt = ftell(tfile);
fseek(tfile,0,SEEK_SET);
if(szt!=0)
{ while(1)
{ fscanf(tfile,"%d%c",&t,&ch);
t_c->info=t;
t_c->link=(m*)malloc(sizeof(m));
//printf("%d ",t_c->info);
t_c=t_c->link;
if(ch==';')
break;
}
}
t_c->link=NULL;
//free(t_c);
fclose(tfile);
return 0;
}
m *search(int Noftarget,m *t_c)
{ int i,p1,p2;
srand(time(NULL));
for(i=0;i<100;i++)
{ p1=(1+rand()%(Noftarget));
p2=(1+rand()%(Noftarget));
t_c=del(p1,t_c);
t_c=ins(p1,p2,t_c);
break;
}
return t_c;
}
m *del(int target,m *t_h)
{ m *t_c;
int j=1,i;
t_c=t_h;
i=posof(target,t_h);
if(i==1)
{ free(t_c);
t_c=t_c->link;
t_h=t_c;
}
else
{ while(j<i-1)
{ t_c=t_c->link;
j++;
}
free(t_c->link);
t_c->link=t_c->link->link;
}
return t_h;
}
m *ins(int target,int position,m *t_h)
{ int j=0;
m *swaptarget,*t_c;
t_c=t_h;
swaptarget=(m*)malloc(sizeof(m));
swaptarget->info=target;
if(position==1)
{ swaptarget->link=t_c;
t_h=swaptarget;
}
else
{ while(j<position-2)
{ t_c=t_c->link;
j++;
}
swaptarget->link=t_c->link;
t_c->link=swaptarget;
}
return t_h;
}
int posof(int p1,m *t_c)
{ int i=1,a=0;
while(t_c->link!=NULL)
{ if(p1==t_c->info)
{ a=i;
break;
}
t_c=t_c->link;
i++;
}
return a;
}
int main()
{ int Noftarget=7,j,r=1,count=0,noi,szd_n=0,i=0,sz;
char cwd[200];
m *t_h;
getcwd(cwd, sizeof(cwd));
t_h=(m*)malloc(sizeof(m));
readt(t_h,cwd);
print_tsp(t_h);
t_h=search(Noftarget,t_h);
print_tsp(t_h);
while(t_h!=NULL)
{ free(t_h);
t_h=t_h->link;
}
return 0;
}
It is checked by valgrind and does not have any memory leak.

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

Resources