answersLogoWhite

0

// Swap

// Demonstrates passing references to alter argument variables

#include

<iostream>

using

namespace std;

void

badSwap(int x, int y);

void

goodSwap(int& x, int& y);

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& x, int& y)

{

int temp = x;

x = y;

y = temp;

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

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;


How do you swap two numbers in a C program?

Ellipses (...) used to emulate indentation... swap(int *a, int *b) { ... int temp; ... temp = *a; ... *a = *b; ... *b = temp; }


Even number progamm in c language?

swap(&amp;grades[num],&amp;grades[num+1]); what it make in a program?


A program to swap values using pass by value?

#include&lt;conio.h&gt; main() { int a,b; scanf("%d%d",&amp;a,&amp;b); printf("Before swapping A= %d B=%d",a,b); swap(a,b); getch(); } void swap(int a,int b) { int c; c=a; a=b; b=c; printf("After swapping A= %d B=%d",a,b); }


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

a=a^b; b=a^b; a=a^b;


How do you write a program in c to swap two values by using functions?

#include using namespace std; void swap(int &amp;a, int &amp;b); int main() { int x=5,y=3; cout


C program to swapping two numbers using call by value method?

You have to pass the address of the variables.void swap (int *pa, int *pb){...}


How do we write c program without using swap to interchange values by accessing another variable?

int a,b; a=a+b; b=a-b; a=a-b; that's it simple


Write a program to swap two numbers using function?

swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }


How develop c program that swap two number with out using temporary variable?

void main() { int a=2,b=5; b=a+b; a=b-a; b=b-a; getch(); }


How do you write a program in C to swap two variables using the third variable?

Consider the following declarations:int x = 0;int y = 1;In order to swap the values, we need to use a temporary variable:int t = x;x = y;y = t;However, it is possible to swap the values without using a third variable:x ^= y ^= x ^= y;


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.