Custom Search

 

 

 

 
 

 

Swapping Values


Binary Search | Bubble Sort | Selection Sort | Finding Maximum Value | Linear Search | Celsius to Fahrenheit | 10 Pseudo-random numbers | Calculating Average | Swapping Values | Lowercase/ Uppercase

Program to Swap two values entered by the user at runtime

C++ Source Code

#include <iostream.h>

void swap(double *, double *); // function prototype

int main()
{
double firstnum = 20.5, secnum = 6.25;

cout << "The value stored in firstnum is: " << firstnum << endl;
cout << "The value stored in secnum is: " << secnum << "\n\n";

swap(&firstnum, &secnum); // call swap

cout << "The value stored in firstnum is now: " << firstnum << endl;
cout << "The value stored in secnum is now: " << secnum << endl;

return 0;
}

void swap(double *nm1Addr, double *nm2Addr)
{
double temp;

temp = *nm1Addr; // save firstnum's value
*nm1Addr = *nm2Addr; // move secnum's value in firstnum
*nm2Addr = temp; // change secnum's value

return;
}

Copyrights

Gary Bronson. "A First Book of C++: From Here to There " Thomson Learning.

                        

 

 

 

Custom Search
 

Home      Disclaimer      Advertise      Contact      Privacy Policy     

Copyright 2004-15 Paked.com. All rights reserved.

Note: Site best viewed at 1024 x 768 or higher screen resolution