C++ ⎼ Two similar functions but performing very differently - c++

I am writing a quicksort program. For that I need to partition the array. The partitioning is done by a function paritionIt(). I wrote a code of partitioning the array which is as follows:
int partition(int beg,int end,double key)
{
int pLeft = beg;
int pRight = end-1;
while(pLeft<pRight)
{
while(array[++pLeft]<key);
while(array[--pRight]>key);
if(pLeft<pRight)
swap(pLeft,pRight);
}
swap(pLeft,end-1);
return pLeft;
}
This block seems to work to work fine when executed in isolation. However, when ran along with the other functions, it seems to generate wrong answer. The following code given to me makes all the problems vanish but it doesn't seem much different from my code.
int partitionIt(int left, int right, double pivot)
{
int leftMark = left; //right of first elem
int rightMark = right - 1; //left of pivot
while(true)
{
while( theVect[++leftMark] < pivot ) //find bigger
; // (nop)
while( theVect[--rightMark] > pivot ) //find smaller
; // (nop)
if(leftMark >= rightMark) //if pointers cross,
break; // partition done
else //not crossed, so
swap(leftMark, rightMark); //swap elements
} //end while(true)
swap(leftMark, right-1); //restore pivot
return leftMark; //return pivot location
} //end partitionIt()
The block seems to be similar to mine but is giving the right answer whereas mine is not. Can you please me by telling what's the difference between partition() and partitionIt().

The difference is where you're breaking out of your looping structure.
In your code, you're making two conditional checks, whereas in the given code, you're only making one.
Assume you've been iterating through the loop for a while. (No pun intended).
You'll hit this code:
if(pLeft<pRight)
swap(pLeft,pRight);
Then you'll hit the bottom of the while loop, come back to the top, and then check again if pLeft<pRight. If this isn't true, we exit the loop.
In the given code, you perform the swap, but then you do the following:
while( theVect[++leftMark] < pivot ) //find bigger
; // (nop)
while( theVect[--rightMark] > pivot ) //find smaller
; // (nop)
You then check to see if you break out of the loop.
This seems to be where the difference lies.
Edit: To clarify - what happens if while(pLeft>=pRight) when you first enter the loop?
In the given code, you continue through the while loop until it breaks, but in your code, you never enter the body of the loop.

The only thing I see immediately is that the functions will
behave differently if called with left + 1 == right: your
function will not enter the loop, but will return beg; the
function from the book will enter the loop, thus incrementing
leftMark and decrementing rightMark before doing the final
swap and returning leftMark.

Related

knights tour in c++ using recursion

I have created a class Board which deals with 2d vectors specifically for this purpose. I am trying to solve the Knight's Tour. I want to print out the thing when it is done. Using the recursive voyagingKnight() function I find that it does not do anything, does not print the result. It seems that I would want to increment the step number for the recursive call but this is not working.
The vector argument incs is a 2d vector of increments for moving the knight, in each row a row move in the first colum and a column move in the second column.
Does anyone have any suggestions as to a flaw in my reasoning here?
The relevant code
bool voyaging_knight( Board &board, int i, int j, int steps ,vector< vector<int> > &increments)
{
if( !newplace(theboard, i, j) ) return false;
board.setval(i,j,step);
if( gone_everywhere( board, steps) )
{
cout <<"DONE" << endl;
board.showgrid();
return true;
}
int n;
int in, jn;
for(n=0; n<8; n++ )
{
in = i + increments[n][0];
jn = j + increments[n][1];
if( inboard(board, i, j)&& newplace(board,i,j) )
{
voyaging_knight( board, in, jn, steps+1 ,increments);
return true;
}
}
theboard.setval(i,j,-1);
}
Yes, change this:
voyagingKnight( theboard, inext, jnext, step+1 ,incs);
return true;
To this:
return voyagingKnight( theboard, inext, jnext, step+1 ,incs);
In addition, it seems that you need to return something (probably false) at the end of the function.
BTW, I'm assuming that you have all the entries in theboard initialized to -1.
I'm guessing that you want 1 continuous path made by horse movements on a (chess)-board found by backtracking. In that case you have to pass the board by value, so each path you take has its own instance to fill. By passing by reference, every path fills the same board, so you can never take all the steps.
Also you should pass a result by value and fill it with the positions you visited and return that from the recursive function, so each path has its own instance of resulting positions and by returning it, you end up with the final result.
You should not pass inc because that is just a helper container that doesn't change.
Make the board a global variable, and build up a sequence of visited squares in a global variable too. Make sure that when retracting each tentative step you undo any changes (square visited, last step of sequence). Call your knight's tour function, make it return success if it reaches the end, and do any output after finishing.
Package the whole shebang in a file or as a class, so as to not expose private details to prying eyes.

Inexplicable behavior in algorithm that should create 4 lists based on the items of a list

The entire program is a coursework based on the classic snake game - we have a list that stores the sequence of the for directions - up, down, left right. Thus far everything is tested and works correct. The next part is a procedure that creates 4 lists - one for each direction, using the list of the directions, for example - if we have input up, up, left, left, right, left, right, up, we have two ups next to each other and then one separate, which will result in the up list storing the values 2, 1. However, the last element is not stored and some elements are stored in the wrong lists. Here is the original code of the procedure:
void separate() {
point current=headSnake;
int count=0;
int lastMove=5;
do{
if (lastMove == current->value){
count++;
}
else {
switch(lastMove){
case UP:addToList(count,headUp);break;
case RIGHT:addToList(count,headRight);break;
case DOWN:addToList(count,headDown);break;
case LEFT:addToList(count,headLeft);break;
}
count=1;
lastMove=current->value;
}
current=current->next;
}while (current);
}
UP, RIGHT, DOWN and LEFT are predefined constants, equaling 0, 1, 2 and 3. headSnake, headUp, headRight, headDown and headLeft point to the first elements of the corresponding lists. point is a typedef of ListElem*, which is a struct. addToList(int, point) is a procedure that is confirmed to work and it adds the integer as a last element to the list which's head we are providing as a second argument.
The following behavior we cannot explain:
with input:
up up left left right left right down right right
the procedure outputs up: 2 ;right: 1, 1, 1; down: 1, 2; left: 2;
which is clearly wrong. Where is the mistake in the algorithm?
If your input ends in several equal elements (example: up down right right) you will not reach the else statement for the last two rights, since you only enter it when your input changes (and not when it actually runs out). You should add a check for when your input ends, something like this:
point current=headSnake;
int count=0;
int lastMove=5;
do
{
if (lastMove == current->value)
{
count++;
}
else
{
saveMove(lastMove, count);
count=1;
lastMove=current->value;
}
if(!current->next)
{
saveMove(current, count);
break;
}
else
{
current=current->next;
}
}
while (true);// true or current - same result
public void saveMove(point move, int count)
{
switch(move)
{
case UP:addToList(count,headUp);break;
case RIGHT:addToList(count,headRight);break;
case DOWN:addToList(count,headDown);break;
case LEFT:addToList(count,headLeft);break;
}
}

Two questions about quicksort

I have two questions when learning quicksort currently. The standard code is here(copy from my book):
void Quicksort(int a[], int low, int high){
if(low<high){
int pivotpos=Patrition(a,low,high);
Quicksort(a,low,pivotpos-1);
Quicksort(a,pivotpos+1,high);
}
}
int Patrition(int a[], int low, int high){
int pivot=a[low]; //first elemnent as pivot
while(low<high){
while(low<high&&a[high]>=pivot) --high; //1
a[low]=a[high];
while(low<high&&a[low]<=pivot) ++low; //2
a[high]=a[low];
}
a[low]=pivot;
return low;
}
My questions are in the above code(marked 1 and 2):
a. why the program cannot be performed(core dumped) when I type this in 1:
while(a[high]>=pivot) --high (similarly in 2). It seems that I have to add the condition(low<high) in the second and third while loop or it said core dumped?
b. another question is why there must be a operator= in second and third while loop. I am confused why it won't work when I type while(low<high&& a[high]>pivot (similarly in 2). If I do so, this program will keep looping and never goes to end.
Thanks for your attention. Any suggestion will be highly appreciated.
The reason for having to write low < high in each while-loop is because it first checks the condition for the first while (the big one), starts looping, then it check the condition in while //1 and keeps looping, but during that time, the first while's (the big one) condition is never checked.
Example:
a = 6;
while (a > 5) {
while (a > 3) {
--a;
}
}
In this code, the second loop only stops when a = 3.
As for question b, it's because it never actually reaches the value that needs to be changed, hence never actually sorting it.
Example: If you'd want the loop to stop at a = 3, the following loop would stop one step earlier.
a = 5;
while (a > 4) {
--a;
}
Value a never reaches 3, it stops at a = 4,

C++ do while loop

I have a vector holding 10 items (all of the same class for simplicity call it 'a'). What I want to do is to check that 'A' isn't either a) hiding the walls or b) hiding another 'A'. I have a collisions function that does this.
The idea is simply to have this looping class go though and move 'A' to the next position, if that potion is causing a collision then it needs to give itself a new random position on the screen. Because the screen is small, there is a good chance that the element will be put onto of another one (or on top of the wall etc). The logic of the code works well in my head - but debugging the code the object just gets stuck in the loop, and stay in the same position. 'A' is supposed to move about the screen, but it stays still!
When I comment out the Do while loop, and move the 'MoveObject()' Function up the code works perfectly the 'A's are moving about the screen. It is just when I try and add the extra functionality to it is when it doesn't work.
void Board::Loop(void){
//Display the postion of that Element.
for (unsigned int i = 0; i <= 10; ++i){
do {
if (checkCollisions(i)==true){
moveObject(i);
}
else{
objects[i]->ResetPostion();
}
}
while (checkCollisions(i) == false);
objects[i]->SetPosition(objects[i]->getXDir(),objects[i]->getYDir());
}
}
The class below is the collision detection. This I will expand later.
bool Board::checkCollisions(int index){
char boundry = map[objects[index]->getXDir()][objects[index]->getYDir()];
//There has been no collisions - therefore don't change anything
if(boundry == SYMBOL_EMPTY){
return false;
}
else{
return true;
}
}
Any help would be much appreciated. I will buy you a virtual beer :-)
Thanks
Edit:
ResetPostion -> this will give the element A a random position on the screen
moveObject -> this will look at the direction of the object and adjust the x and Y cord's appropriately.
I guess you need: do { ...
... } while (checkCollisions(i));
Also, if you have 10 elements, then i = 0; i < 10; i++
And btw. don't write if (something == true), simply if (something) or if (!something)
for (unsigned int i = 0; i <= 10; ++i){
is wrong because that's a loop for eleven items, use
for (unsigned int i = 0; i < 10; ++i){
instead.
You don't define what 'doesn't work' means, so that's all the help I can give for now.
There seems to be a lot of confusion here over basic language structure and logic flow. Writing a few very simple test apps that exercise different language features will probably help you a lot. (So will a step-thru debugger, if you have one)
do/while() is a fairly advanced feature that some people spend whole careers never using, see: do...while vs while
I recommend getting a solid foundation with while and if/else before even using for. Your first look at do should be when you've just finished a while or for loop and realize you could save a mountain of duplicate initialization code if you just changed the order of execution a bit. (Personally I don't even use do for that any more, I just use an iterator with while(true)/break since it lets me pre and post code all within a single loop)
I think this simplifies what you're trying to accomplish:
void Board::Loop(void) {
//Display the postion of that Element.
for (unsigned int i = 0; i < 10; ++i) {
while(IsGoingToCollide(i)) //check is first, do while doesn't make sense
objects[i]->ResetPosition();
moveObject(i); //same as ->SetPosition(XDir, YDir)?
//either explain difference or remove one or the other
}
}
This function name seems ambiguous to me:
bool Board::checkCollisions(int index) {
I'd recommend changing it to:
// returns true if moving to next position (based on inertia) will
// cause overlap with any other object's or structure's current location
bool Board::IsGoingToCollide(int index) {
In contrast checkCollisions() could also mean:
// returns true if there is no overlap between this object's
// current location and any other object's or structure's current location
bool Board::DidntCollide(int index) {
Final note: Double check that ->ResetPosition() puts things inside the boundaries.

C++ Sieve of Eratosthenes finding 3 too many primes

I have a programming assignment to write a program in C++ that finds all primes less than n (user input). One half of the assignment involves the Sieve of Eratosthenes. My code is working (read: assignment is complete), but before I edited the output, it was unconditionally printing out n-3, n-2, and n-1 as primes even if they were not prime. I'm not sure why this is happening. I'd appreciate a bit of feedback and ideas as to why the program is acting the way it is. Here is the unaltered code:
Please note that I am using a ListNode class and a LinkedList class, both of which are fully functional. EDIT: partial main added; notice the second item in the for loop is size-3. If it's left at size, the program outputs 3 extra non-primes.
int main()
{
for(int i = 0; i<my_list.size()-3; i++)
{
if(marked[i]==true)
cout<<my_list[i]<<"\n";
}
}
void eratosthenes(int item)
{
bool run=true;
int p=2, count=0;
for(int i=2; i<=item; i++)
{
my_list.append(i); // Entire list is filled with integers from 2 to n
marked.append(true); // Entire list is filled with true entries
}
while(run==true&&(2*p)<item)
{
count = 0;
int i = (2*p);
do {
marked[i-2]=false; // marked values are false and not prime
i+=p;
} while(i<item-2);
for(int i=0; i<item-2; i++) // i starts at 0 and increments by 1
{ // each time through the loop
if(my_list[i]>p)
{
if(marked[i]==true) // If a value stored in a node is true
{ // (prime), it becomes the new p.
p=my_list[i]; // The loop is then broken.
break;
}
}
}
for(int j=1; j<item-2; j++)
{
if(marked[j]==false)
{
count=1;
}
}
if(count==0)
run=false;
}
Complete method
void Eratosthenes(int upperBound)
{
bool Prime[upperBound];
for(int i = 0;i<upperBound;i++)
Prime[i]=true;
for (int i = 2; i <= sqrt(upperBound); i++)
{
if (Prime[i])
{
for (int j = i * 2; j < upperBound; j += i)
Prime[j] = false;
}
}
for(int i=2;i<upperBound;i++)
{
if(Prime[i]==true)
cout<<i<<" ";
}
}
From your code:
do{
marked[i-2]=false;//marked values are false and not prime
i+=p;
}while(i<item-2);
This loop is responsible for going through all numbers i that are integer multiples of the prime number p and marking them not prime, as I understand. Why are you stopping on the condition i < item - 2? This would be fine if i were your index for the my_list and marked lists, but in this case it's not; it's the actual number you're marking not prime. I suspect this is why you're getting numbers near your limit (item) that are marked as prime—your loop here exits before i ever gets to those numbers!
By the way, you could do this as a for loop instead, which would be easier to read. The for loop has the meaning "go through each element in a set" (whether that's consecutive integers, or every nth integer, or elements in an array/list/deque, etc.), so a programmer reading your code knows that immediately and doesn't have to figure it out from your while loop.
// mark every multiple of the current prime as not prime
for(int i = 2*p; i < item - 2; i += p)
{
marked[i-2] = false;
}
(This is the same as your original code, no fixes applied).
Some general comments to improve your algorithm/code:
Try using more descriptive variable names. Your use of i two times to mean different things is confusing, and in general single letters don't mean much as to what the variable represents (although sometimes they're sufficient, e.g. a for loop where i is the index of a list/array).
Also, you're looping over your list a lot more than you need to. The minimum a sieve of Eratosthenes algorithm needs is two nested for loops (not including initializing a list/array to all true).
One example of where you're doing more work than necessary is that you're looping starting from index 0 to find the next p to use—instead of just remembering where your current p is and starting from there. You wouldn't even need to check my_list[i] > p in that case, since you know you'd be beyond it to start off. Also, your last loop could break; early and avoid continuing on after it finds a non-prime (and I'm not sure what the point of it is).
Nikola Mitev's second answer is a more efficient and more readable implementation of the sieve (but replace sqrt(upperBound) with upperBound/2 for it to work correctly; the reason for upperBound/2 should be pretty clear from the way the Sieve works), although he didn't really give much comment or explanation on it. The first loop is "go through every number up to upperBound"; inside it, "if the current number is a prime, go through all the multiples of that prime and mark them non-prime". After that innerloop executes, the outer loop continues, going through the next numbers—no need to start from the beginning, or even type out another for loop, to find the next prime.
EDIT: sqrt(upperBound) is correct. I wasn't thinking about it carefully enough.
Why don't you work with array of booleans for simplicity starting from index 2, and when you will print the result, you will print indices with value of true