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
Which of the following is an example of hypertext pattern reading?
Murrr4er [49]
A is the correct answer
5 0
3 years ago
6
Novosadov [1.4K]

Answer:

government and New Zealand

Explanation:

you can see the web site name the name is gov.nz ,gov mens- government and nz means new Zealand

thank you like us

8 0
2 years ago
Read 2 more answers
Which will you see on the next line 9, 2, 3.5, 7]
elena55 [62]

Answer:

plenipotentiaries. It was by far the most splendid and

important assembly ever convoked to discuss and

determine the affairs of Europe. The Emperor of

Russia, the King of Prussia, the Kings of Bavaria,

Denmark, and Wurttemberg, all were present in

person at the court of the Emperor Francis I in the

Austrian capital. When Lymie put down his fork and

began to count them off, one by one, on the fingers

of his left hand, the waitress, whose name was Irma,

thought he was through eating and tried to take his

plate away. He stopped her. Prince Metternich (his

right thumb) presided over the Congress, and

Prince Talleyrand (the index finger) represented

France? please let me know if this is the answer you were looking for!!

5 0
3 years ago
Read 2 more answers
The following method is intended to remove all values from the ArrayList a that have the same value as val; however, this method
Alona [7]

Answer:

ArrayList a contains [2, 3, 3, 3, 4, 5, 3, 2, 1]

Explanation:

Given

The removeValue method

Required

The content of ArrayList a, after the method is called

From the question, we understand that arraylist a is:

a= [2, 3, 4, 3, 3, 4, 4, 5, 4, 3, 2, 1]

val =4

The function fails when the value to be removed appear consecutively in the list.

4 appears in index 5 and 6 of the list. Only one of the 4's will be removed

So, the updated list will be: [2, 3, 3, 3, 4, 5, 3, 2, 1]

6 0
3 years ago
Select the correct category of cybersecurity described.
Shalnov [3]

Answer:

Network security

Explanation:

Network security is a category of cybersecurity that is related to the protection of the infrastructure of the computer network from unauthorized access and intrusion that may be from intended attack or from an expedient  program which may result in the modification, malfunctioning, misuse or unacceptable disclosure of the network and the contents of the network

The category of cybersecurity that 'prevents intruders from accessing, misusing, destroying, adapting, or improperly disclosing networks, network infrastructure, or the information contained within networks' is network security.

6 0
2 years ago
Other questions:
  • HURRY UP !!!!!!’<br> Each packet is addressed to the recipient's .
    8·1 answer
  • Which statement accurately describes DTP programs?
    11·1 answer
  • How do you change the name on your brainly account?
    12·1 answer
  • Write an expression that computes the integer average of the int variables exam1 and exam2 (both declared and assigned values).
    15·1 answer
  • Unwanted email sent to large groups of people who did not request the communication is called _____
    12·1 answer
  • Describe two different examples of potential conflicts, and explain why these conflicts can have a negative impact on
    14·1 answer
  • 3. Create mode was one of the most innovative and influential features of which of the following games?
    7·1 answer
  • How do i change the default setting in word to no open in dark mode
    8·1 answer
  • Data security issues that must be addressed by hipaa implementation teams include:__________
    9·1 answer
  • Create a timeline of the evolution of computers and their impact on society
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!