answersLogoWhite

0

What is swap pointing c plus plus?

Updated: 10/23/2022
User Avatar

Wiki User

11y ago

Best Answer

// Swap Pointer

// Demonstrates passing constant pointers to alter argument variables

#include

<iostream>

using

namespace std;

void

badSwap(int x, int y);

void

goodSwap(int* const pX, int* const pY);

int

main()

{

int myScore = 150;

int yourScore = 1000;

cout <<

"Original values\n";

cout <<

"myScore: " << myScore << "\n";

cout <<

"yourScore: " << yourScore << "\n\n";

cout <<

"Calling badSwap()\n";

badSwap(myScore, yourScore);

cout <<

"myScore: " << myScore << "\n";

cout <<

"yourScore: " << yourScore << "\n\n";

cout <<

"Calling goodSwap()\n";

goodSwap(&myScore, &yourScore);

cout <<

"myScore: " << myScore << "\n";

cout <<

"yourScore: " << yourScore << "\n";

return 0;

}

void

badSwap(int x, int y)

{

int temp = x;

x = y;

y = temp;

}

void

goodSwap(int* const pX, int* const pY)

{

//store value pointed to by pX in temp

int temp = *pX;

//store value pointed to by pY in address pointed to by pX

*

pX = *pY;

//store value originally pointed to by pX in address pointed to by pY

*

pY = temp;

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is swap pointing c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is null object in c plus plus?

a pointer that is not pointing to anything


How to swap two numbers by call by reference in c plus plus?

void swap(int&amp; a, int&amp; b ) { a^=b^=a^=b; }


Bubble sorting in c plus plus?

bubble_sort (int N, int *A) { int i; swap = 1; while (swap) { swap = 0; for (i=0; i&lt;N-1; ++i) { if (A[i] &gt; A[i+1]) { swap = 1; A[i] ^= A[i+1]; A[i+1] ^= A[i]; A[i] ^= A[i+1]; } } } }


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


Coding in c plus plus to swap two nos using pointer?

void swap (int &amp;pa, int &amp;pb) { *pa ^= *pb; *pb ^= *pa; *pa ^= *pb; }


What are the conditions to swap in c plus plus without temporary?

You can swap two integers without temporary storage by bitwise exclusive-or'ing them in a specific sequence...a ^= b;b ^= a;a ^= b;


What is swap in c plus plus programming language?

It is not a reserved word, so can be an identifier (name of a type/variable/function).


How do you swap two numbers in c plus plus?

void swap(int &amp;x, int &amp;y) { x ^= y ^= x; } - or - void swap(int &amp;x, int &amp;y) { int t = x; x = y; y = t; }


How do you swap two numbers with bitwise operator in c plus plus?

// Note: ^ is the XOR operator a = a ^ b b = b ^ a a = a ^ b


How do you swap two arrays in c plus plus using string?

You can't. While a string is a character array, an array is not necessarily a string. Treating arrays as if they were strings simply to swap them is madness. The correct way to physically swap arrays A and B is to copy A to a new array, C, then copy B to A, then C to B. If the arrays are the same size this is not a problem. If they are different sizes, you can only swap them if they are dynamic (not static). This means you must reallocate them. To speed up the process, copy the smallest array to C, first. A much better approach would be to point at the two arrays and swap the pointers instead.


How do you program c plus plus to arrange 3 numbers in ascending order using swap?

void sort (int&amp; a, int&amp; b, int&amp; c) { if (a&gt;b) std::swap (a, b); if (b&gt;c) std::swap (b, c); else return; if (a&gt;b) std::swap (a, b); } Note that this is based upon a bubble sort algorithm. Although usually inefficient as a general sorting algorithm, given that we know there are only three elements means we can implement it reasonably efficiently without any additional space complexity. There will always be 2 or 3 comparisons but at most there will be 3 swaps. The only improvement we could really make is to implement a type of selection sort: void sort (int&amp; a, int&amp; b, int&amp; c) { if (a&gt;b &amp;&amp; a&gt;c) std::swap (a, c); else if (b&gt;c) std::swap (b, c); if (a&gt;b) std::swap (a, b); } Here we either make 3 or 4 comparisons but only 2 swaps at most. The assumption here is that a comparison is a quicker operation than a swap thus the selection sort method is more efficient. However, unless you were to sort millions of sets of three one after the other, you are unlikely to see any measurable difference in performance.


Do you place the arrow pointing of an air filter toward the fan?

a/c filter with an arrow, is is placed in the unit with the arrow pointing toward the a/c fan??