#include <stdio.h>
int bsearch(int ar[],int n,int key)
{
int s=0;
int e=n-1;
while(s<=e){
int mid=(e+s)/2;
if(mid==key){
return mid;
}
else if(key<mid){
e=mid-1;
}
else if(key>mid){
s=mid+1;
}
}
return -1;
}
I made the function for the binary search
int main()
{
int n,key;
int ar[n];
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("ar[%d]= ",i);
scanf("%d",&ar[i]);
}
printf("Enter key>> \n");
scanf("%d",&key);
printf("%d is the index",bsearch(ar,n,key));
return 0;
}
Then I inputted an sorted array but with repetitions. Shown in the following image.
The output is coming as 3 is the index.
But it should as come as 6 is the index.
mid is an index of element, not a value. So, I have corrected your function:
#include <stdio.h>
int bsearch(int ar[], int n, int key)
{
int s=0;
int e=n-1;
while(s <= e){
int mid = (e + s) / 2;
if(key == ar[mid]) {
return mid;
}
else if(key < ar[mid]) {
e = mid-1;
}
else if(key > ar[mid]) {
s = mid+1;
}
}
return -1;
}
Related
I saw a problem on the web,i.e, I am trying to sort a stack using another reference stack in C. I tried to do it by implementing it as an array but it is not working.
(condition is that you have to use only arrays)
#include <stdio.h>
int MAXSIZE = 5;
int stack[5];
int tmpstack[5];
int tmp;
int top = -1;
int top2=-1;
int isempty(int A[],int B) {
if(B == -1)
return 1;
else
return 0;
}
int isfull(int A[],int B) {
if(B == MAXSIZE)
return 1;
else
return 0;
}
int pop(int A[],int B) {
int data;
if(!isempty(A,B)) {
data = A[B];
B = B - 1;
return data;
}
else {
printf("Could not retrieve data, Stack is empty.\n");
}
}
int push(int data,int A[],int B) {
if(!isfull(A,B)) {
B = B + 1;
A[B] = data;
}
else {
printf("Could not insert data, Stack is full.\n");
}
}
int sortStack(int stack[])
{
int tmpStack[MAXSIZE];
while (!isempty(stack,top))
{
int tmp = stack[top];
pop(stack,top);
while (!isempty(tmpstack,top2) && tmpstack[top2] > tmp)
{
push(pop(tmpstack,top2),stack,top);
}
push(tmp,tmpstack,top2);
}
return 0;
}
int main() {
int n ,a;
n=5;
for(int i = 0;i < n ; ++i){
scanf("%d",&a);
push(a,stack,top);
}
sortStack(stack);
while (!isempty(tmpstack,top2))
{
printf("%d ",tmpstack[top2]);
pop(tmpstack,top2);
}
return 0;
}
What I did here is that I made similar functions in arrays which we used in stacks only and we are supposed to use them only for implementing our stacks.
#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
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'm trying to write sliding puzzle where I'm using 3x3 matrix as board. In main function user inputs the number of tile which swaps positions with 0. Everything works except when I enter number that is located in a position above zero it does not swap positions with it. How do I correct that?
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#define empty_space 0
int m,n,i,j,z,y;
void print_matrix(int matrix[3][3])
{
for ( m=0; m<3; m++){
for (n=0; n<3; n++)
{
printf("%d\t",matrix[m][n]);
} printf("\n");
}
printf("\n");
}
void swap(int *i, int *j) {
int t = *i;
*i = *j;
*j = t;
}
void slide(int a[3][3] , int t)
{
for ( i=0; i<3; i++){
for ( j=0; j<3; j++)
{
if (a[i][j]==t)
{
if (a[i+1][j]==empty_space && i+1<=2)
{
swap(&a[i+1][j],&a[i][j]);break;
}
if (a[i-1][j]==empty_space && i-1>=0)
{
swap(&a[i][j],&a[i-1][j]);break;
}
if (a[i][j+1]==empty_space && j+1<=2)
{
swap(&a[i][j],&a[i][j+1]);break;
}
if (a[i][j-1]==empty_space && j-1>=0)
{
swap(&a[i][j],&a[i][j-1]);break;
}
}
}
}
}
int goal_test (int a[3][3],int b[3][3])
{
int flag=0;
for ( z=0; z<3; z++){
for ( y=0; y<3; y++){
if(a[z][y]==b[z][y])
flag++;
}
}
if (flag==9)
return 1;
else return 0;
}
int main()
{
static int mat[3][3]={{1,2,3},{6,0,4},{7,5,8}};
int goal[3][3]={{1,0,3},{6,5,4},{7,8,2}};
print_matrix(mat);
int x;
while(goal_test(mat,goal)==0)
{
printf("enter tile to slide:\t");
scanf("%d",&x);
slide(mat, x);
print_matrix(mat);
}
return 0;
}
Here's what happens:
Any number can swap its position as long as it's not located above zero
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;
}