Respuesta :

Answer:

Here is the C++ and Python programs:

C++ program:

#include<iostream>  //to use input output functions

using namespace std;  //to identify objects like cin cout

void swapvalues(int&  userVal1, int& userVal2); //function to swap values

int main() {  //start of main function

  int integer1,integer2;  //declare two integers

  cout<<"Enter first integer: ";  //prompts user to enter the first number

  cin>>integer1;  //reads first number from user

     cout<<"Enter second integer: "; //prompts user to enter the second number

  cin>>integer2;  //reads second number from user

 cout<<"Integers before swapping:\n";  //displays two numbers before swapping

 cout<<integer1<<endl<<integer2;  

  swapvalues(integer1, integer2);  //calls swapvalues method passing two integers to it

  cout << "\nIntegers after swapping:\n"; //displays two numbers after swapping

 cout<<integer1<<endl<<integer2; }

void swapvalues(int&  userVal1, int& userVal2){  //method that takes two int type numbers and swaps these numbers

  int temp;  //temporary variable

  temp = userVal1;  //temp holds the original value of userVal1

  userVal1 = userVal2;  //the value in userVal2 is assigned to userVal1

  userVal2 = temp; } //assigns value of temp(original value of userVal1) to userVal2

The program prompts the user to enter two integers and then calls swapvalues() method by passing the integers by reference. The swapvalues() method modifies the passed parameters by swapping them. The method uses a temp variable to hold the original value of first integer variable and then assigns the value of second integer variable to first. For example if userVal1 = 1 and userVal2 =2. Then these swapping statements work as follows:

temp = userVal1;  

temp = 1

userVal1 = userVal2;  

userVal1 = 2

userVal2 = temp;

userVal2 = 1

Now because of these statements the values of userVal1 and userVal2 are swapped as:

userVal1 = 2

userVal2 = 1

Explanation:

Python program:

def swap_values(user_val1, user_val2):  #method to swap two numbers

   return user_val2,user_val1   #returns the swapped numbers

integer1 = int(input('Enter first integer :')) #prompts user to enter first number

integer2 = int(input('Enter second integer :'))  #prompts user to enter second number

print("Numbers before swapping") #displays numbers before swapping

print(integer1,integer2)

print("Numbers after swapping") #displays numbers after swapping

integer1,integer2 = swap_values(integer1,integer2) #calls method passing integer1 and integer2 to swap their values

print(integer1,integer2) #displays the swapped numbers

The screenshot of programs and outputs is attached.

Ver imagen mahamnasir
Ver imagen mahamnasir

The program that swaps variables will return the reverse of the users input.  The second integer input will be printed first followed by the first integer input.

The program that swaps the user integer input.

def swap_variables(first_input, second_input):

    return second_input, first_input

first_input = input("input the first integer: ")

second_input = input("input the second integer: ")

print(f"numbers before swapping are {first_input} and {second_input}")

print(f"numbers after swapping are {second_input} and {first_input}")

Code explanation

The code is written in python.

  • The first line of code we defined a function named "swap_variables" and it accept parameters as first_input and second_inputs.
  • Then we returned the swapped items.
  • The first_input is used to store the users first input
  • The second_input is use to store the users second input
  • Then, we print the original values
  • Finally, we print the swapped users input

learn more on python variables here: https://brainly.com/question/14039335

Ver imagen vintechnology
Ver imagen vintechnology
ACCESS MORE
EDU ACCESS
Universidad de Mexico