In C++ Language (please use hint) :Write a function which will swap its arguments if the first argument is greater than its second argument, but will not interchange them if the first argument is samller than or equal to the second argument. The function should return 1 if a swap was made, and 0 otherwise. (Hint: Make sure to use call by reference.)Write also a short test driver (i.e. a main() invoking your function).

Question
Asked Nov 4, 2019

In C++ Language (please use hint) :

Write a function which will swap its arguments if the first argument is greater than its second argument, but will not interchange them if the first argument is samller than or equal to the second argument. The function should return 1 if a swap was made, and 0 otherwise. 

(Hint: Make sure to use call by reference.)

Write also a short test driver (i.e. a main() invoking your function).

 

 

check_circleExpert Solution
Step 1

Call by reference: In this approach, passing the arguments to a method copies the address or reference of an argument into the formal parameter. The argument inside this method is used to access the real parameters used in the function call.

Syntax:

return_type name_of_function(data_type variable_name);

example-

int sum(int a);  

Step 2

Below is the approach and code of a C++ program to swap two numbers by using call by reference technique.

Approach:

  • Import essential headers.
  • Using namespace std.
  • The prototype of the swap function with two arguments is given.
  • The main function is declared.
  • Declaring the variables as per the requirement.
  • Prompt user to take input.
  • Calling the swap function and storing its return value into variable ‘s’.
  • If statement to display the message “swapping is done” when s = = 1.
  • If statement to display the message “swapping is not done” when s = = 0.
  • Definition of swap function having two reference variables as arguments.
  • If statement to check the first argument is greater than the second argument.
  • Swapping is done inside the if statement and function will return 1.
  • In else, the part function will return 0 as the first argument is less than the second argument.   
Step 3

Code:

//importing header file

#include <iostream>

//using namespace

using namespace std;

//prototype of swap function

int swap(int &l, int &m);

//main function declared

int main ()

{

//declaring variables as per the requirement 

int i;

int j;

//displaying message   

cout << "Before swapping the value of i is :";

//prompt user to take input

cin>>i;

//displaying message

cout << "Before swapping the value of j is :";

//prompt user to take input

cin>>j;

//calling swap function to swap the values of i and j using reference variables l and m

int s = swap(i, j);

//displaying the value of i after swapping   

cout << "After swapping the value of i is :" << i << endl;

//displaying the value of j after swapping

cout << "After swapping the value of j is :" << j << endl;

//if statement    

...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour*

See Solution
*Response times may vary by subject and question
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: Vectors Write a for loop to print all NUM_VALS elements of vector courseGrades, following each with ...

A:  Note: As the user given index value is constant and so the value of index could not be changed whil...

question_answer

Q: First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ...

A: Program AlgorithmStartCreate a java project named AtmSimulator and then create a class named as AtmS...

question_answer

Q: Write a loop that reads positive integers from standard input and that terminates when it reads an i...

A: Following is the loop which prints the sum of all even and odd integers entered by the user and will...

question_answer

Q: WHAT ARE THE BARRIERS FOR THE ADOPTION OF CRYPTOCURRENCY WORLDWIDE?

A: There are several barriers for the adoption of cryptocurrency worldwide. A few of them are as follow...

question_answer

Q: Assignment First, launch NetBeans and close any previous projects that may be open (at the top menu ...

A: A Java program that satisfies the given criteria is given below,Program://Import necessary packagesi...

question_answer

Q: I need help tackling this program:     Write a C++ console application that allows your user to ente...

A: For solving this problem following variables and data type are used to execute this file :1.Variable...

question_answer

Q: This program is written in pseudocode."Design a program that generates a 7-ddigit lottery number. Th...

A: This pseudocode explains how to create a 7-digit lottery number. These 7 digits are randomly generat...

question_answer

Q: I have written a C++ class called 'Birthday' and I want to write a method that can compare with anot...

A: Programming instructions:First, create the constructor to initialize the variables with the initial ...

question_answer

Q: Python 3.7.4 Write the definition of a function twice, that receives an int parameter and returns an...

A: Program code: