This question is a follow-up to a question I asked earlier on Stack Overflow:
bsort example from programming pearls
I want to show you my work and have some questions about it.
#include<string.h>
#include<iostream>
using namespace std;
void swap(char *x[],int i,int j)
{
char *t=x[i];
x[i]=x[j];
x[j]=t;
}
int get_bit(char *s,int depth)
{
int required_bit=13;
int bit=required_bit&0x7;
int byte=required_bit>>3;
int val=(s[byte]>>bit)&0x1;
return val;
}
void bsort(char *x[],int l,int u,int depth)
{
if(l>=u) return ;
for(int i=l;i<=u;i++){
if(strlen(x[i])<depth){
swap(x,i,l++);
}
}
int m=l;
for(int i=l;i<u;i++)
{
if(get_bit(x[i],depth)==0)
{
swap(x,i,m++);
}
}
bsort(x,l,m-1,depth+1);
bsort(x,m,u,depth+1);
}
int main()
{
char *x[6]={"car","bus","snow","earth","dog","mouse"};
bsort(x,0,5,1);
for(int i=0;i<6;i++)
cout<<x[i]<<" "<<endl;
return 0;
}
The code above works, compiles and shows me a result. But I am interested to know whether it sorts lexicographically or otherwise? The result is this:
car
bus
dog
snow
earth
mouse
First b is before c, so are car and bus on the right place? Please tell me what is wrong with my code.
Related
I have used Dynamic Programming to find longest common sub-sequence b/w two strings. What is wrong in the code. Why it always gives answer as 0?
#include<bits/stdc++.h>
using namespace std;
int dp[20][20];
void initialize()
{
for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
dp[i][j]=-1;
}
int lcs(string a,string b,int m,int n)
{
if(m==0||n==0)
return 0;
if(dp[m][n]!=-1)
return dp[m][n];
if(a[m-1]==b[n-1])
return dp[m-1][n-1] = 1+lcs(a,b,m-1,n-1);
if(a[m-1]!=b[n-1])
return dp[n-1][m-1]=max(dp[n][m-1]=lcs(a,b,n,m-1),dp[n-1][m]=lcs(a,b,n-1,m));
}
int main()
{
string a="AGGTAB",b="GXTXAYB";
cout<<lcs(a,b,a.length(),b.length());
}
You've forgotten to call initialize()
18th line, it should be dp[m][n], not dp[m-1][n-1]
Commented 19th line code, as it is no need & for make the code compatible for all compilers
i.e., some compiler may give warning: control reaches end of non-void function [-Wreturn-type]
Made some code change in 20th line, as it seems you confused with variables m & n.
Code:
#include<bits/stdc++.h>
using namespace std;
int dp[20][20];
void initialize()
{
for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
dp[i][j]=-1;
}
int lcs(string a,string b,int m,int n)
{
if(m==0||n==0)
return 0;
if(dp[m][n]!=-1)
return dp[m][n];
if(a[m-1]==b[n-1])
return dp[m][n] = 1+lcs(a,b,m-1,n-1);
//if(a[m-1]!=b[n-1])
return dp[m][n]=max(lcs(a,b,m-1,n),lcs(a,b,m,n-1));
}
int main()
{
string a="AGGTAB",b="GXTXAYB";
initialize();
cout<<lcs(a,b,a.length(),b.length());
}
Output:
4
Description
This code is intended to find a spanner index (i) when dealing with
Splines/NURBS basis functions based on a knot vector (U), a choosen knot (u), the degree of the desired curve (p) and the number of basis funcions (n). The algorithm was taken from the NURBS Book by Piegl and Tiller. The error, I guess, is in the way I declared variable U. Thaks in advanced!
code
# include <iostream>
using namespace std;
int n=3;
int p=2;
double u=5/2;
int U[11]={0,0,0,1,2,3,4,4,5,5,5};
int FindSpan(n,p,u,U) /* an error in this line */
{
if (u==U[n+1]) return (n);
low=p; high=n+1;
mid=(low+high)/2
while(u<U[mid] || u>=U[mid+1])
{
if (u<U[mid]) high=mid;
else low=mid;
mid=(low+high)/2
}
return (mid);
}
You have forgotten some semicolons and types!
here is the correct code:
#include <iostream>
using namespace std;
int n=3;
int p=2;
double u=5/2;
int U[11]={0,0,0,1,2,3,4,4,5,5,5};
int FindSpan(int n, int p, int u, int U[])
{
if (u==U[n+1]) return (n);
int low=p, high=n+1;
int mid=(low+high)/2;
while(u<U[mid] || u>=U[mid+1])
{
if (u<U[mid]) high=mid;
else low=mid;
mid=(low+high)/2;
}
return (mid);
}
int main() {
cout << FindSpan(n, p, u, U);
return 0;
}
I'm very new to C++, so I've probably made some really stupid mistakes. But I've looked online to solutions for this error, and I tried all I can think of.
I'm trying to have my whole program in one function, because it's going to be merged with other programs. The id error is the only error I have so far.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
using namespace std;
int ninebooking(int Endor=0, int Naboo=0, int tatooine=0);
int sevenbooking(int Endor=0, int Naboo=0, int tatooine=0);
void BookingSystem(int time, int k);
int time;
int k = 0;
int main()
{
BookingSystem(time, k);
{
while (k==0)
{
printf("\n\nMeals are served at 9pm and 7pm, please enter what time you would like to book for");
scanf("You have selected to book your meal for %d", &time);
if (time!=7||time!=9)
{
printf("Sorry, that was an incorrect time");
time = 0;
}
}
return 0;
}
system ("pause");
return 0;
}
Any help is much appreciated, I've spent a long time trying to fix this on my own with no luck.
Thank you!
You make many mistakes in your program:
1) You mix C & C++ syntax.
using namespace std, is C++
2) You include useless header files
3) You declare variables that I don't understand what for
Why do you need this?
int ninebooking(int Endor=0, int Naboo=0, int tatooine=0);
int sevenbooking(int Endor=0, int Naboo=0, int tatooine=0);
You don't use the variables above anywhere !!!!
4) You try to write a function inside main (this is not Pascal).
So, if I understand what you want, look at this:
#include <stdio.h>
void BookingSystem() // this is the function who does all job
{
int time = 0;
while ((time != 7) && (time != 9))
{
printf("\n\nMeals are served at 9pm and 7pm, please enter what time you would like to book for");
printf("\nYou have selected to book your meal for ");
scanf("%d", &time); // read the time
scanf("%*[^\n]"); // consume all caracters until the newline
scanf("%*c"); // consume the newline
if (time == 7)
{
printf("You have selected to book your slot at 7PM\n");
}
else if (time == 9)
{
printf("You have selected to book your slot at 9PM\n");
}
else
{
printf("You have selected an incorrect time, please try again\n");
}
} // end while
} // end function
int main(int argc, char *argv[])
{
BookingSystem(); // this is the calling to your function
return 0;
}
If I understand what you want, this works fine.
you can not to Define Function in to the main ..
you must to do like this...
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
using namespace std;
int ninebooking(int , int , int ); //just say data type int or ....
int sevenbooking(int , int , int );
void BookingSystem(int , int);
int main() //and in to main just call he...
{
int time;
int k = 0;
BookingSystem(time, k);//call ....and send parameter time and k
system ("PAUSE");
return 0;
}
void BookingSystem(int time,int k)
{
while (k==0)
{
printf("\n\nMeals are served at 9pm and 7pm, please enter what time you would like to book for");
scanf("You have selected to book your meal for %d", &time);
if (time!=7||time!=9)
{
printf("Sorry, that was an incorrect time");
time = 0;
}
}
// return 0;//if this function is void he can not to return any thing!!!!
}
int ninebooking(int Endor=0, int Naboo=0, int tatooine=0)
{
//......
}
int sevenbooking(int Endor=0, int Naboo=0, int tatooine=0)
{
//........
}
My aim is to construct a binomial heap. Here is my code which i have written right now:
#include<iostream>
using namespace std;
void maxheapify(int a[],int length,int i)
{
int left=2*i;
int right=2*i+1;
int largest=i;
if(left<length && a[left]>a[largest])
{
largest=left;
}
if ( right<length && a[right]>a[largest])
{
largest=right;
}
if(largest!=i)
{
int temp=a[i];
a[i]=a[largest];
a[largest]=temp;
maxheapify(a,length,largest);
}
}
void buildmax(int a[],int length)
{
for(int i=(length-1)/2;i>=0;i--)
{
maxheapify(a,length,i);
}
}
/*void heapsort(int a[],int length)
{
buildmax(a,length);
for(int i=length-1;i>0;i--)
{
int temp=a[i];
a[i]=a[0];
a[0]=temp;
maxheapify(a,i,0);
}
}
*/
void combine_heap(int a[],int n,int b[],int m,int c[])
{
}
int main()
{
int a[100];
int n=sizeof(a)/sizeof(a[0]);
int b[100];
int m=sizeof(b)/sizeof(b[0]);
int c[200];
int length=sizeof(a)/sizeof(a[0]);
for(int i=0;i<length;i++){
a[i]=34+rand()%(length-33);
b[i]=rand()%(i+1);
}
/*heapsort(a,length);*/
/*for(int i=0;i<length;i++)
cout<<a[i]<<" ";*/
return 0;
}
i think trivial solution would be to combine two array into third one and then call buildmax procedure,but i think it is not efficient,i have tried to implement this pseudo code from wikipedia
function merge(p, q)
while not( p.end() and q.end() )
tree = mergeTree(p.currentTree(), q.currentTree())
if not heap.currentTree().empty()
tree = mergeTree(tree, heap.currentTree())
heap.addTree(tree)
else
heap.addTree(tree)
heap.next() p.next() q.next()
but i dont know how to implement it,because in generally how to access subtrees?another variant is construct priority queue and by using insert function insert first from one array and then from another array,but is this optimal?please help me to write code to combine these two max heap into one efficiently
This is a good example of Binomial Heap but it is in c. You will get the basic logic to implement binomial heap. See Example here
Or get video tutorial to here to understand algorithm.
Hello i'm trying to program the addition of 2 matrices into a new one (and it does when i run the program step by step) but for some reason VS 2010 gives me an access error after it does the addition.
Here is the code.
#include <iostream>
#include <cstdio>
#include <conio>
using namespace std;
class operatii
{
typedef double mat[5][5];
mat ms,m1,m2;
int x1,x2,y1,y2;
public:
void preg();
int cit_val();
void cit_mat(int&,int&,double[5][5]);
void suma();
void afisare(int&,int&,double[5][5]);
};
void operatii::preg()
{
cit_mat(x1,y1,m1);
cit_mat(x2,y2,m2);
suma();
afisare(x1,y1,ms);
}
int operatii::cit_val()
{
int n;
cin>>n;
return n;
}
void operatii::cit_mat(int& x,int& y,double m[5][5])
{
char r;
cout<<"Matrice patratica? Y/N ";
cin>>r;
if ((r=='y')||(r=='Y'))
{
cout<<"Numar linii si coloane: ";
x=cit_val();
y=x;
}
else
{
cout<<"Numar linii: ";
x=cit_val();
cout<<"Numar coloane: ";
y=cit_val();
}
for (int i=1;i<=x;i++)
for (int j=1;j<=y;j++)
cin>>m[i][j];
}
void operatii::suma()
{
if ((x1==x2)&&(y1==y2))
for (int i=1;i<=x1;i++)
for (int j=1;i<=y1;j++)
ms[i][j]=m1[i][j]+m2[i][j];
else cout<<"Eroare";
}
void operatii::afisare(int& x,int& y,double m[5][5])
{
cout<<endl;
for (int i=1;i<=x;i++)
{
for (int j=1;j<=y;j++)
cout<<m[i][j];
cout<<endl;
}
}
void main()
{
operatii matrice;
matrice.preg();
system("PAUSE");
}
Any kind of help would be apreciated.
Arrays are 0-based in c++.
Change your various variants of for (somevar=1; somevar<=something) to for (somevar=0; somevar<something)
You're writing past the end of your arrays, which overwrites stack return address, leading to a return to nonexecutable code, again leading to an access violation.
Also,
for (int j=1;i<=y1;j++)
I think you want to use j not i here. Such errors are much easier to see if you use longer and more distinct variable names than "i" and "j", such as e.g. "Line" and "Column"