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
The technology that identifies people through bodily characteristics is known as
vlabodo [156]
C. Biometrics, examples of common biometric devices are finger print scanners (iphones, phones, laptops etc..) and pupil scanners.  
3 0
3 years ago
After you save a table, the new table name appears ____.
kobusy [5.1K]
Table1/2.....hope it helps
5 0
3 years ago
Read 2 more answers
Which one Bc I’m struggling
amid [387]
14 is c and 16 a btw do you go to connection academy
6 0
3 years ago
Read 2 more answers
Features present in most GUIs include _____.
atroni [7]
Forms, Icons, Menus, Windows
3 0
4 years ago
Read 2 more answers
Which of the following is true regarding data acquisition? Because data acquisition is often technical, the research team does n
elixir [45]

Answer:

Data acquisition should follow a detailed collection plan that is set in advance.

Explanation:

The process of collection of data over computer with the help of electronic sensors is called data acquisition. The can be related to voltage, current, temperature etc.

For example

Data collection with the help of sensors to monitor weather conditions is an example of data acquisition. In this process data has been gathered on already planned format. Each column or row of data will be helpful to predict the weather condition such as, rain predictions, earthquake prediction etc.

That's why to collect this type of data, collection plan should be set in advance to gather relevant information with the help of data.

4 0
4 years ago
Read 2 more answers
Other questions:
  • The following code in a different program is not working properly. The message should display the value of the intCounter variab
    14·1 answer
  • Filtering removes data from the spreadsheet. A. True B. False
    9·2 answers
  • before donating a computer you should use a program to wipe the hardest to remove all of his data true or false
    6·1 answer
  • HELP PLEASE NOW ASAP BRAINLIEST
    13·2 answers
  • To run a PHP application that has been deployed on your own computer, you can enter a URL in the address bar of your browser tha
    13·1 answer
  • Which feature of a website takes you to a different part of the website or a totally different website when you click on it?
    12·2 answers
  • How many address bits are needed to specify each byte in a 512 byte memory unit?
    5·1 answer
  • What are cell phones used for?
    8·2 answers
  • Biosolids are used as a chemical free natural choice for fertilizing.
    5·1 answer
  • Could anyone help me with this assignment please?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!