Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

C++ epic win!


Admiral Refuge
 Share

Recommended Posts

_NOTE: There should be a "Programming Discussion" board or something like that!_

Okay, maybe not an EPIC win, but I did use only notepad, and I did manage to do it WITHOUT google…

My professor challenged me to swap two variables (x and y) in c++; simple right?
int temp;
x = temp;
x = y;
y = temp;

Yea, but he said to do it without using a 3rd variable XD

So, about 2-3 mins on my TI-89 (best calculator in the world!), I figured it out, and wrote this program in 5 mins (notepad ftw!):
```
//Swapping two values without a 3rd.
//Algorithm made by Anthony Ianozi
//Written in Notepad ;)
//Epic win!
#include using namespace std;
/*Declare functions*/
void swap(double&, double&); // Swaps two numbers.
void getNumbers(double&, double&); //Gets two numbers.
void displayNumbers(double, double); //Displays two numbers.
int main()
{
  double x, y;
  getNumbers(x, y);
  cout << "Before swap:\n";
  displayNumbers(x, y);
  swap(x, y);
  cout << "After swap:\n";
  displayNumbers(x, y);
  return 0;
}
void swap(double& a, double& b)
{
/* Let's assume a=5; b=2; */
  if (a > b)
  {
      a = a - b; //a holds 3
      b = b + a; //b holds 5
      a = b - a; //a holds 2
  }
  else
  {
      b = b - a; //b holds 3
      a = a + b; //a holds 5
      b = a - b; //b holds 2
  }
}
void displayNumbers(double first, double second)
{
  cout << "First variable holds " << first << " and variable number holds " << second << ".\n";
}
void getNumbers(double& ex, double& why)
{
  cout << "Hello! Please type two numbers, seperating them with a space, and hit enter.\n";
  cin >> ex >> why;
}

```
I'll just pick out the part where it swaps them:
```
void swap(double& a, double& b)
{
/* Let's assume a=5; b=2; */
  if (a > b)
  {
      a = a - b; //a holds 3
      b = b + a; //b holds 5
      a = b - a; //a holds 2
  }
  else
  {
      b = b - a; //b holds 3
      a = a + b; //a holds 5
      b = a - b; //b holds 2
  }
}
```
:azn:
Link to comment
Share on other sites

@V-Rage:

> lol I've come across this method a lot, never used it, its a waste of time and it doesnt work for all data types

You're talking to someone who figured out how to solve square roots to any decimal with just a pencil and paper.

It was just a nice (pretty easy) puzzle to solve; it makes you think, and keeps your brain young :)
Link to comment
Share on other sites

@ÅÐмiядζ:

> You're talking to someone who figured out how to solve square roots to any decimal with just a pencil and paper.
>
> It was just a nice (pretty easy) puzzle to solve; it makes you think, and keeps your brain young :)

so, found out whats the square root of -2 yet?
(without using i as an imaginary integer constant)
:P
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...