answersLogoWhite

0

How create swap program in c?

User Avatar

Anonymous

13y ago
Updated: 8/19/2019

The best way to explain how to make a swap function in C is through an example:

int x = 1;

int y = 2;

int tmp = x; New variable (temporary) is used to store the old x value.

x = y; So now both x and y are equal to 2.

y = tmp; y is now equal to 1.

So after this function x is equal to 2 and y is equal to 1.

To create a swap program you must apply this function in some way. One example being the bubble sort.

for (j = 0; j < n; j++) {

for (i = 0; i < n; i++) {

if (array[i-1] > array[i]) {

int tmp = array[i-1];

array[i-1] = array[i];

array[i] = tmp;

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

C program on left factoring in compiler design?

how to create a c program for left factoring.


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?


How you create c program without semicolon?

int main () {}


How you can make c program of attendance record system?

how can create a attendece sheet in c language


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;


Write a program in C to create a Circular Linked list?

#include&lt;iostream.h&gt;


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 can you create a c program without using header file?

C programs do not require header files. If you want a C program without header files, you can simply not create them. However, you may or may not be able to include your non-header file source files.