128k views
0 votes
Write the definition of a function named swapints that is passed two int variables. The function returns nothing but exchanges the values of the two variables. So, if j and k have (respectively) the values 15 and 23, and the invocation swapints(j,k) is made, then upon return, the values of j and k will be 23 and 15 respectively. Instructor Notes: Hint: Replace XXX with the correct function definition line and submit all the code:

User Tura
by
5.5k points

1 Answer

7 votes

Answer:

The program to this question can be given as:

Program:

#include <iostream>

//include header file (iostream).

using namespace std;

//using name space.

void swapints(int &j, int &k); // function declaration

void swapints(int &j, int &k) //function definition

{

int t; //define variable temp

//perform swapping

t = j;

j = k;

k = t;

}

int main () //define main method

{

int a = 15; //define variable.

int b = 23; //define variable.

cout <<"Before swapping"<<endl<<"value of a and b :"<<endl << a <<endl<<b;//print value.

swapints(a,b); //calling function.

cout <<endl<<"After swapping"<<endl<<"value of a and b :"<< endl << a <<endl<<b; //print value.

return 0;

}

Output:

Before swapping

value of a and b :

15

23

After swapping

value of a and b :

23

15

Explanation:

The description of the above program can be given as:

  • In above C++ program firstly we include the header file that is "iostream". Then we define a method that is "swapints()". This method takes two integer parameter that is "j and k".
  • swapints method is use to perform the swapping. In this method we define a variable that is "t" variable t holds the value of variable j and variable j holds variable k value and in the last variable k holds variable t value for this process all the value is swapped.
  • In the main method, we define two integer variable that is "a and b" and assign the value "15 and 23" that is given in question and pass this variable into swapints() method and call it.

User Douglas Held
by
5.1k points