when I call the function, it says that argument 5 of while(visite(matrice,i,j,deja_visite,&m)!
void capture_chaine(char matrice[][DIMENSION+1],int i,int j,int **a_visiter,int **deja_visite,int *p, int *m)
{
while(visite(matrice,i,j,deja_visite,&m)!=4)
{
voisin_egaux(matrice,i,j,a_visiter,deja_visite,p,m);
deja_visite[*m][0]=i;
deja_visite[*m][1]=j;
printf("%c",matrice[i][j]);
++(*m);
capture_chaine(matrice,a_visiter[*m][0],a_visiter[*m][1],a_visiter,deja_visite,p,m);
printf("\n\nhere");
}
for (int i=0;i<*m;i++)
matrice[deja_visite[i][0]][deja_visite[i][1]]='.';
}
Here is the other function
void voisin_egaux(char matrice[][DIMENSION+1],int x, int y,int **a_visiter,int **deja_visite,int *p,int *m)
And the other
int visite(char matrice[][DIMENSION+1],int x,int y,int **deja_visite, int *m)
{
int s=0;
if (matrice[x+1][y]!='.' && matrice[x+1][y]==matrice[x][y])
{
for (int i=0;i<*m;i++)
{
if (x+1==deja_visite[i][0] && y==deja_visite[i][1])
s++;
}
}
if (matrice[x-1][y]!='.' && matrice[x-1][y]==matrice[x][y])
{
for (int i=0;i<*m;i++)
{
if (x-1==deja_visite[i][0] && y==deja_visite[i][1])
s++;
}
}
if (matrice[x][y+1]!='.' && matrice[x][y+1]==matrice[x][y])
{
for (int i=0;i<*m;i++)
{
if (x==deja_visite[i][0] && y+1==deja_visite[i][1])
s++;
}
}
if (matrice[x][y-1]!='.' && matrice[x][y-1]==matrice[x][y])
{
for (int i=0;i<*m;i++)
{
if (x==deja_visite[i][0] && y-1==deja_visite[i][1])
s++;
}
}
return s;
}
Related
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;
}
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;
}
I am passing the pointer of count in all function but its value does not change and remain 0
int binarysearch(int a[],int l,int r,int k,int *count)
{
int m = (r+l)/2;
if (r > l)
{
if (a[m] > k){
*count++;
return binarysearch(a,l,m-1,k,count);
}
if (a[m] < k)
{
*count++;
return binarysearch(a,m+1,r,k,count);
}
if (a[m] == k)
{
*count++;
return m;
}
}
if (r <= l)
{
*count++;
if (k>a[l]) return l+1;
else return l;
}
}
void insertati (int a[],int i,int *count)
{
int t = binarysearch(a,0,i-1,a[i],count);
int y = a[i],j = i-1;
while (j >= t)
{
a[j+1] = a[j];
j--;
}
a[j+1] = y;
}
void insetionsort(int a[],int n,int *count)
{
for (int i = 1; i < n; ++i)
{
insertati(a,i,count);
}
}
int main()
{
int n,count =0;
scanf("%d",&n);
int arr[n];
for (int i = 0; i <n; ++i)
{
scanf("%d",&arr[i]);
}
insetionsort(arr,n,&count);
printf("%d\n",count);
for (int i = 0; i <n; ++i)
{
printf("%d ",arr[i]);
}
return 0;
}
Surround dereferencing with parentheses:
int binarysearch(int a[],int l,int r,int k,int *count) {
int m = (r+l)/2;
if (r > l)
{
if (a[m] > k){
(*count)++;
return binarysearch(a,l,m-1,k,count);
}
if (a[m] < k)
{
(*count)++;
return binarysearch(a,m+1,r,k,count);
}
if (a[m] == k)
{
(*count)++;
return m;
}
}
if (r <= l)
{
(*count)++;
if (k>a[l]) return l+1;
else return l;
}
}
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;
}
#include <stdio.h>
#include <string.h>
typedef struct word
{
char *p;
int index;
}Word;
typedef struct wordarray
{
Word **array;
int count;
}arr;
arr* createarray(int count)
{
arr *temp=(arr*)malloc(sizeof(arr));
temp->array=(Word**)malloc(sizeof(Word*)*count);
temp->count=count;
return temp;
}
int my_partition(char p[],int start,int end)
{
int left=start;
start++;
while(start<=end)
{
while(start<=end && p[start] < p[left])
start++;
while(start<=end && p[end]> p[left])
end--;
if(start<end)
p[start]=(p[start] ^ p [end]^ (p[end]=p[start]));
}
p[end]=(p[left] ^ p [end]^ (p[left]=p[end]));
return end;
}
void my_sort(char *p, int start, int end)
{
//printf("\n%d %d\n",start,end);
if(start<=end)
{
int pivot=my_partition(p,start,end);
my_sort(p,start,pivot-1);
my_sort(p,pivot+1,end);
}
}
void addWord(arr* temp, char p[][10])
{
int i=0;
for(i=0;i<4;i++)
{
temp->array[i]=(Word*)malloc(sizeof(Word));
temp->array[i]->p=(char*)malloc(strlen(p[i])+1);
temp->array[i]->index=i;
strcpy(temp->array[i]->p,p[i]);
temp->array[i]->p[3]='\0';
// printf("\n %c \n",temp->array[i]->p[0]);
char *q=(temp->array[i]->p);
//printf("\n %s \n",q);
my_sort(q,0,2);
}
}
int partition(arr *temp, int start, int end)
{
int left=start;
start++;
while(start<=end)
{
while(start<=end && (strcmp(temp->array[start]->p,temp->array[left]->p) < 0))
start++;
while(start<=end && (strcmp(temp->array[end]->p,temp->array[left]->p)> 0))
end--;
if(start<end)
{
Word *temp1=temp->array[start];
temp->array[start]=temp->array[end];
temp->array[end]=temp1;
//temp->array[start]
}
}
Word *temp1=temp->array[left];
temp->array[left]=temp->array[end];
temp->array[end]=temp1;
return end;
}
void qsort(arr *temp, int start, int end)
{
if(start <end)
{
int pivot=partition(temp,start,end);
qsort(temp,start,pivot-1);
qsort(temp,pivot+1,end);
}
}
void print(arr *temp)
{
int i=0;
for(i=0;i<(temp->count);i++)
printf("\n %s \n",temp->array[i]->p);
}
main()
{
arr *temp=createarray(4);
char p[][10]={"act","cat","pat","cap"};
addWord(temp,p);
qsort(temp,0,3);
print(temp);
}
In above program my_sort will sort the string. Now when I pass the p[i] instead of q to my_sort function, it works fine. But when i pass q, it goes into infinite loop. I can print the value of q in above statement. what is wrong here?
"act","cat" is change to "act","act" by my_sort.
at partition
while(start<=end)
{
while(start<=end && (strcmp(temp->array[start]->p,temp->array[left]->p) < 0))
start++;
while(start<=end && (strcmp(temp->array[end]->p,temp->array[left]->p)> 0))
end--;
if(start<end)
{
Word *temp1=temp->array[start];
temp->array[start]=temp->array[end];
temp->array[end]=temp1;
//temp->array[start]
}
}
Stuck in this while-loop when 0 == result of strcmp(temp->array[start]->p,temp->array[left]->p)