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]
2 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]2 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
What chage command should you use to set the password for jsmith to expire after 60 days and give a warning 10 days before it ex
nata0808 [166]

chage -M 60 -W 10 jsmith Forces jsmith to keep the password 60 days before changing it and gives a warning 10 days before changing it.

<h3>Which command should you use to set the expiration date for a user account?</h3>

The “chage” command is used to modify the password expiry date of the user's account in which you can change the current status of the expiry date, set expiry date to lock the account, active and passive status, and warning days before the account get expired.

<h3>What is chage command Linux?</h3>

The chage command is self-described as the "change user password expiry information" utility. According to the chage man page: The chage command changes the number of days between password changes and the date of the last password change.

To learn more about chage command, refer

brainly.com/question/13084023

#SPJ4

7 0
1 year ago
Matthew is working to select an authentication method for his company that will support REST as well as many web-based and mobil
vitfil [10]

Answer:

C) OpenID Connect

Explanation:

The best option is OpenID Connect because is based in OAuth, and supports multiple web based and mobile clients, and supports REST.

With OAuth, we cannot make any user authentication just provide a token to access data.

RADIUS is a networking protocol, and Shibboleth is a single sign-on log-in system for computer networks and the Internet, both not support REST.

6 0
2 years ago
Write a function named “createPurchaseOrder” that accepts the quantity (integer), the cost per item(double), and the description
Leona [35]
#include
Program: using namespace std;
string createPurchaseOrder0;
int main(
{
cout<return 0;
}
string createPurchaseOrder(
{
int qty;
double costPerltem;
string description,info="":
cout<<"Enter Quantity:
cin>>qty;
cout<<"Enter cost per item: "
cin>>costPerltem;
cout<<"Enter Description: "
cin>>description;
if(qty<0 I| costPerltem<0
Idescription.compare(''"')==0)
cout<<'InThe entered data is invalid!":
info="":
else
"
cout<<"'InThe entered data is valid!":
info=info+"'(nQuantity: "+to_string (qty) +" In";
info=info+"Cost per item:
"†to_string (costPerltem)+"In";
info=info+"Description: "description+" In";
return info;

Output:
4 0
2 years ago
Energy requirements of analog and digital signals.
jek_recluse [69]
Analog signals require more energy.

Analog=Constant flowing modulated

Digital is bursts of on/off binary data.
8 0
3 years ago
Read 2 more answers
Blender questions
Arisa [49]
Hey 

7 is true 

8 <span>
the user preference settings</span>
6 0
3 years ago
Other questions:
  • Before starting a spreadsheet, it is smarter to plan ahead and think through the design. true or false
    13·1 answer
  • Can someone please help me? I have no clue what any of this is and how I'm going to find out 15 examples of these. It's due some
    5·1 answer
  • Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output tho
    9·1 answer
  • Signing up for a(n) ____ will automatically provide you with web content that is updated on a regular basis.
    15·1 answer
  • 2.2-2 Consider sorting numbers stored in array by first finding the smallest element n A of and exchanging it with the element i
    11·1 answer
  • ________models software in terms similar to those that people use to describe real- world objects.
    6·1 answer
  • Match each role to the corresponding web development task.
    14·1 answer
  • Code works, need help writing header file.
    8·1 answer
  • when you sent email your email can be set to automatically keep a copy where do you find these copies​
    13·1 answer
  • When methods have ____, other programs and methods may use the methods to get access to the private data.
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!