answersLogoWhite

0

What is swap program c?

Updated: 12/16/2022
User Avatar

Wiki User

11y ago

Best Answer

// 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

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is swap program c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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(); }


What is the amount of data and program instructions that can swap at a given time?

a page


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;