Swapping number question help

So my question asks me the following:

Declare and define a function that swaps two integer numbers:

void swap(int &n1, int &n2)

Declare and define an overloaded function that swaps two double values:

void swap(double &d1, double &d2)

In the main() function, call the first function by passing two integer varaibles and display the values of these two integer variables before and after calling the function. Then call the second function by passing two double-typed variables and display the values of these two double-typed variables before and after calling the function.

I'm confused on what this is adding me.

Here is some code I have so far, can someone help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

void Swap(int &n1, int &n2)
{
    
    int temp = n1;
    n1= n2;
    n2= temp;
    
}

int main()
{
    
  int n1 = 1;
  int n2 = 2;
  swap(n1, n2); 

}
Last edited on
You did a really good job, just left off the overloaded function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

void Swap(int &n1, int &n2)
{ 
    int temp = n1;
    n1= n2;
    n2= temp;    
}

void Swap(double &n1, double &n2)
{ 
    double temp = n1;
    n1= n2;
    n2= temp;    
}

int main()
{
  int n1 = 1;
  int n2 = 2;
  double n3 = 1.0;
  double n4 = 2.0;
  Swap(n1, n2); 
  cout << n1 << " " << n2 << endl;
  Swap(n3, n4); 
  cout << n3 << " " << n4 << endl;
  return 0;
}


2 1
2 1
Last edited on
Thanks for help
In the main() function, call the first function by passing two integer varaibles and display the values of these two integer variables before and after calling the function


What you've done so far is good. All you need to do is just output the variables:
std::cout << "n1: " << n1 << "\nn2: " << n2;

Do that before calling the "swap" function and then after calling it. This is to show that N1 and N2 use to be 1 and 2, but are now swapped and are 2 and 1 .


Then call the second function by passing two double-typed variables and display the values of these two double-typed variables before and after calling the function.


This wants you to make a SECOND "swap" function that takes double rather than int . Otherwise, it'll do the exact same thing. Make sure to output before and after calling the function.
1
2
3
4
5
6
void Swap(double &n1, double &n2)
{ 
    double temp = n1;
    n1= n2;
    n2= temp;    
}


Wait, in main() shouldn't it be:

1
2
double n1 = 1.0;
double n2 = 2.0;


Because in the function the double values are n1, n2?
Sorry, just confused.
Last edited on
No, you can't have several variables with the same name.

double n1 would conflict with int n1


Because in the function the double values are n1, n2?

The names don't have to be the same. It copies the values/memory addresses into the function. Within the function, you'll use the new name.

EX:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void m(double &n1) //Use the variable name that is here within the function
{
	n1 = 2; //even though a different NAME, this has the memory address of "max"
}

int main()
{
	double max = 100;

	m(max); //Give the function "max"

	std::cout << max; //outputs 2

	return 0;
}



Since it's "double &n1", the variable "n1" will have the same memory address as "max". So when you change one, you change the other. If the "&" was not there, "n1" would have a COPIED value of the variable "max". So changing one would NOT change the other.
Last edited on
Topic archived. No new replies allowed.