C++ Programming

 

Swapping

Program to Swap two values entered by the user at runtime


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;
}

 


DISCLAIMER

Paked and the contributors are not responsible for any errors contained and are not liable for any damages resulting from the use of this material.  (Disclaimer)

 

 

Home     Disclaimer     Advertise      Contact us     

Copyright © 2006-08 Paked.com. All rights reserved.

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

 

footer image footer image