1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Arlecino [84]
3 years ago
6

6.21 LAB: Swapping variables Write a program whose input is two integers and whose output is the two integers swapped. Ex: If th

e input is:

Computers and Technology
1 answer:
Korvikt [17]3 years ago
4 0

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.

You might be interested in
In order to create a horizontal 2-inch-thick dotted green line exactly 2 inches away from the top of the canvas, a graphic artis
Harrizon [31]

Answer:

I Think meter .

Explanation:

Not so Sure

4 0
3 years ago
Read 2 more answers
Cite 3 funções para o uso de uma Rede de Computadores
Andru [333]

Answer: Servidores, Clientela, Medios de transmisión

8 0
3 years ago
Read 2 more answers
43
pshichka [43]

Answer:

c

Explanation:

5 0
2 years ago
Read 2 more answers
Complete the steps to evaluate the following
kirill115 [55]

Answer:

log base 3a= -0.631.log a/3 base 3

Now, -log m= log 1/m

hence,

log base 3a= 0.631.log 3/a base 3

log base 3a/log 3/a base 3 =0.631

log base 3 ( a.3/a) =.631 since, log m/logn =log n(m)

log base 3 3=0.631

Hence, answer is log base 3 3=0.631

Explanation:

Please check the answer section.

3 0
3 years ago
Read 2 more answers
OO<br>(A) 3 and 5<br>(B) 4 and 8<br>(C) 2 and 0<br>(D) 6 and 9<br>2. There are twelve books on a shelf and four children in a ro
dlinn [17]

Explanation:

2.there will be 8 books left on the shelf if each child takes one

8 0
3 years ago
Other questions:
  • Hey im trying to get a refund because i got charged 24$ for the yearly plan and i didnt know i would get charged can i get the y
    11·1 answer
  • Which building-block feature is available in the Text grouping on the Insert tab?
    14·1 answer
  • Can someone help me to give this guy brainliest the button is not there
    8·1 answer
  • Create an application that writes a series of random numbers to a file. Each random number should be in the range of 1 through 1
    9·1 answer
  • A series circuit contains a generator, two devices, and connecting wires. The resistances of the two devices are 15 ohms and 10
    10·2 answers
  • What is the unemployment rate if the GDP gap is 13.5%?<br><br> A. 6.25%<br> B. 6.75%<br> C. 6.7%
    8·1 answer
  • I WILL GIVE BRAINLIEST TO WHO ANSWERS FIRST AND CORRECTLY.
    12·2 answers
  • 2. How do cell phone users stay connected to the network as users move between cells?
    10·1 answer
  • An anagram of a string is another string with the same characters in the same frequency, in any order. For example 'ab', 'bca, a
    5·1 answer
  • Who is primarily responsible for ensuring cybersecurity in society? Select 3 options.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!