How do you write an algorithm to swap the values of x and y using a temporary variable t?

1 Answer

Answer :

To swap the values of two variables, x and y, using a temporaryvariable t:t = x;x = y;y = t;To implement this algorithm as a function, the variables must bepassed by reference. For efficiency, particularly with large orcomplex objects that are expensive to copy, use move semantics. TheC++ standard library provides the following implementation (sinceC++11):namespace std {template<typename T>void swap (T&& x, T&& y) {T t = std::move (x);x = std::move (y);y = std::move (t);}};The C++ standard library swap function (std::swap) uses theabove implementation (since C++11). If a type does not support themove semantic, copy semantics will be used instead.

Related questions

Description : How can I swap two integers without temporary variables using JavaScript code?

Last Answer : answer:This is a trick using the exclusive or operator ^ that was used when computers were short on memory. a = (a ^ b) /* a^b, b */ b = (a ^ b) /* a^b, a */ a = (a ^ b) /* b, a */ You can also do it using subtraction for numeric ... = a - b /* a - b, b */ b = b -a /* a-b, a */ a = b - a /* b, a */

Description : Write a program to swap two integer values by using call by reference.

Last Answer : #include #include void swap(int *a,int *b) { int c; c=*a; *a=*b; *b=c; } void main() { int a,b; couta; coutb; cout

Description : The term ___________ is used for a depth-first search that chooses values for one variable at a time and returns when a variable has no legal values left to assign. a) Forward search b) Backtrack search c) Hill algorithm d) Reverse-Down-Hill search

Last Answer : b) Backtrack search

Description : Which values are independant in minimax search algorithm? a) Pruned leaves x and y b) Every states are dependant c) Root is independant d) None of the mentioned

Last Answer : a) Pruned leaves x and y

Description : Create a simple code fragment that will swap the values of two variables num1 and num2.

Last Answer : [c] int temp; temp = num1; num1 = num2; num2 = temp; [/c]

Description : Which allows for a simple and matrix implementation of all the basic algorithm? a) HMM b) Restricted structure of HMM c) Temporary model

Last Answer : b) Restricted structure of HMM

Description : How do you Write a pseudo code algorithm that reads in three values and writes out the result of subtracting the second value from the sum of the first and third values?

Last Answer : Pseudocode means you don't use any actual computer language, so you can be quite informal. Just get the general idea. In this case, for instance:Input number to aInput number to bInput ... a + c - bShow resultYour teacher may have specific requirements for pseudocode; try to incorporate those.

Description : Write a C++ program to swap two integer numbers and swap two float numbers using function overloading.

Last Answer : #include<iostream.h> #include<conio.h> void swap(int a,int b) { int temp; temp=a;  a=b;  b=temp; cout<<"\nInteger values after swapping are:"<<a<<" "< ... { clrscr(); swap(10,20); swap(10.15f,20.25f); getch(); }

Description : Write a program to swap two numbers using call be value.

Last Answer : #include<stdio.h> #include<conio.h> void swap(int a, int b) { int temp; temp=a; a=b; b=temp; printf("Numbers after swapping no1=%d and no2=%d",a,b); } void ... ;Numbers before swapping no1=%d and no2= %d",no1, no2); swap(no1,no2); getch(); }

Description : Below are the few steps given for scan-converting a circle using Bresenham's Algorithm. Which of the given steps is not correct? (1) Compute d = 3 – 2r (where r is radius) (2) Stop if x> y (3) If d

Last Answer : If d≥0,then d=4 *(x-y)+10, x=x+1 and y=y+1

Description : Write a C++ program to declare a class addition with data members as x and y. Initialize values of x and y with constructor. Calculate addition and display it using function ‘display’.

Last Answer : #include<iostream.h> #include<conio.h> class addition { int x,y; public: addition(int,int); void display(); }; addition::addition (int x1,int y1) { x=x1; y=y1; } void addition: ... y); } void main() { addition a(3,4); a.display(); getch(); }

Description : Which is omitted in prolog unification algorithm? a) Variable check b) Occur check c) Proposition check d) Both Occur & Proposition check

Last Answer : b) Occur check

Description : The BACKTRACKING-SEARCH algorithm in Figure 5.3 has a very simple policy for what to do when a branch of the search fails: back up to the preceding variable and try a different value for it. This is ... also possible to go all the way to set of variable that caused failure. a) True b) False

Last Answer : a) True

Description : . In simplex algorithm, which method is used to deal with the situation where an infeasible starting basic solution is given? a. Slack variable b. Simplex method c. Big M Method d. None of the above

Last Answer : b. Simplex method

Description : The minimax algorithm computes the minimax decision from the current state. It uses a simple recursive computation of the minimax values of each successor state, directly implementing the defining equations. The ... are backed up through the tree as the recursion unwinds. a) True b) False

Last Answer : a) True

Description : Which algorithm includes repeated addition of two predetermined values A and S to a product P and then performs a rightward arithmetic shift on P. a. Booth’s algorithm b. Usual algorithm c. Multiplication algorithm d. None of these

Last Answer : a. Booth’s algorithm

Description : Quantitative attributes are A. A reference to the speed of an algorithm, which is quadratically dependent on the size of the data B. Attributes of a database table that can take only numerical values C. Tools designed to query a database D. None of these

Last Answer : B. Attributes of a database table that can take only numerical values

Description : Develop a program to swap two numbers using pointer and add swaped numbers also print their addition.

Last Answer : #include<stdio.h> void swap(int *a,int *b) {  int temp;  temp=*a;  *a=*b;  *b=temp; } void main() { int x,y,sum; printf("\n Enter value for x:"); scanf(" ... x); printf("\ny=%d",y); sum=x+y; printf("Sum of x+y = %d",sum); }

Description : Write Java code to assign the value 10 to variable x and store its square value in another variable y. -Technology

Last Answer : class Square{public static void main (String args [ ]){int x. y ; x = 10 : y = x * x ;

Description : One of the potential health effects of exposure to high levels of noise is called tinnitus (pron: ti - NI - tes), which means: w) a permanent loss of hearing. x) a persistent ringing ... ) a temporary loss of hearing, especially of upper frequencies. z) abdominal discomfort from low frequency noise.

Last Answer : ANSWER: X -- A PERSISTENT RINGING OR HISSING IN THE EARS AFTER EXPOSURE.

Description : If x = 3 and y = -1, find the values of each of the following using in identity: -Maths 9th

Last Answer : Formula, (a+b+c)2=a2+b2+c2+2ab+2bc+2ca Given, (2+x−2y)2=22+(x)2+(−2y)2+2(2)(x)+2(x)(−2y)+2(2)(−2y) =4+x2+4y2+4x−4xy−8y

Description : A ______ contains specific rules and words t hat express the logical steps of an algorithm a) Programming language b) Syntax error c) Programming structure d) Logic chart e) None of these

Last Answer : a) Programming language

Description : A scheduling Algorithm assigns priority proportional to the waiting time of a process. Every process starts with priority zero (lowest priority). The scheduler re-evaluates the process priority for every 'T' time ... (A) Priority scheduling (B) Round Robin Scheduling (C) Shortest Job First (D) FCFS

Last Answer : (B) Round Robin Scheduling

Description : Article 370 of t he Constitution of India provides for – (1) temporary provisions for Jammu & Kashmir (2) special provisions In respect of Nagaland (3) special provisions in respect of Nagaland (4) provisions in respect of the financial emergency

Last Answer : (1) temporary provisions for Jammu & Kashmir Explanation: Article 370 of the Indian constitution grants special autonomous status to Jammu and Kashmir.

Description : Which one of the following statements is not correc t? (a) Parallax error is eliminated when there is no change in the staff reading when eye is moved up and down (b) The objective lens is ... (d) The eyepiece need not be adjusted after the first set up when the same surveyor is taking readings

Last Answer : (b) The objective lens is to be focused towards a white or bright background for clear visibility of cross-hairs

Description : An assignment problem is a special case of transportation problem, where a. Number of rows equals number of columns b. All rim conditions are 1 c. Values of each decision variable is either 0 or 1

Last Answer : D.variable is either 0 or 1

Description : A set of values X1,X2……..Xn which satisfies the constraints of the LPP is called_______ a. Solution b. Variable c. Linearity d. None of the above

Last Answer : a. Solution

Description : A condition or characteristic that can take on different values or categories is called ___. a. A Constant b. A Variable c. A Cause-and-Effect Relationship d. A Descriptive Relationship

Last Answer : b. A Variable

Description : The meaning of variable is Options: A) The quality having different values B) The nature of happening which influences other happenings through its presence C) Both of the above D) None of the above

Last Answer : C) Both of the above 

Description : If you could change the main character to a side character in anything, who would you swap?

Last Answer : answer:Anikan Skywalker for any one of the Jawa's in StarWars Episode one. Scavenging parts & being shuffled out of bustling street markets, include some Hutt issues, boosting pod racers & tracking ... sure there is a better story in there than the immaculate born slave boy of prophecy story arc

Description : What will happen if I attempt to swap chips in two identical voice memo recorders?

Last Answer : 90% sure that swapping the chips would be effective. Chips are solid state and your thoughts are stored digitally. When your voice recorder went into the wash, it died because the electronics (battery to chip ... in the new device, you will be successful. It would be a great experiment. Let us know.

Description : Why don't we swap out rechargable batteries for laptops, cell phones and electric cars ?

Last Answer : A battery pack of a Tesla weighs 1200 pounds. In order to swap an empty pack for a fully charged one you would have to bring it with you. That's another 1200. Which greatly would reduce your ... thus heavier battery pack still. Which would reduce your mileage even further. Which would mean .......

Description : If there was a version of "Wife Swap" of fictional wives, which wives would you like to watch swap families?

Last Answer : What?

Description : The Nigerian Rebels have offered to swap the kidnapped school girls for imprisoned militants. What should the Nigerian President do?

Last Answer : If they do the swap there will be more of them to just turn around and steal the girls again. That’s a tough situation. They should do the swap and ideally then go in and take care of the problem. I doubt it’s that easy though. This is probably not going to end well.

Description : Public phone personality swap?

Last Answer : answer:You must feel the environment you are standing in at that time requires that? I possibly do modify my responses to suite where I am. Perhaps with so many people on cell phones we do need to be more entertaining!

Description : If you could house swap, would you?

Last Answer : Ummm… I love snow and winter, but sub-zero is too cold for even me. Thanks anyway!!

Description : How do I swap motherboards without having to wipe out all my data?

Last Answer : answer:Depends on what you don't want to wipe out. I'll suggest copy all your data (pictures, docs, non-windows app install files) to the 500GB drive and plan to re-install Windows fresh onto ... for the heck of it. Plug and play. Of course before either approach, back up ALL important stuff.

Description : IPhone with a physical problem: would Genius Bar send it away for repairs or swap it out on the spot?

Last Answer : $199 and they’ll replace it.

Description : How do I career swap to become a P.E teacher?

Last Answer : I am not a teacher, but my understanding is that the majority of physical education teachers teach other subjects in addition to physical education, so it would be safe to assume you would be required ... I would certainly look into your state's teaching requirements, and go from there. Good luck!!

Description : Fluther Life-Swap: Have you ever wondered what it would be like to live another Jelly's life for a day?

Last Answer : It’d really be cool to switch with Symbeline or Fiddle. Both of them are quite bad ass and interesting!

Description : Is there a book swap site in which the person getting the book pays for the shipping, not the other way around?

Last Answer : I believe goodreads has what you’re looking for, though I’ve never done the book swap thing myself.

Description : How do I swap image text in Dreamweaver?

Last Answer : Upload the new image into Dreamweaver and just replace the image that you have in the editor with the new one. You should be able to just highlight that area of script, or click on the image itself and it should give you the option to delete/edit.

Description : How to swap the content on two Mac computers?

Last Answer : answer:That is a bit tricky since you want to swap. How I would do this is a external drive. Make two partitions. Make sure both partitions are big enough to hold what is on your respective computers. ... bit risky and I won't post them since I don't want to be responsible if something goes wrong.

Description : How much would an engine swap cost?

Last Answer : Look on your city’s Craigslist page. You can find a decent used engine for just a few hundred, or a completely built motor for couple thousand. In Oklahoma City, I can usually find a Chevy 350 and matching transmission for less than $1,000 dollars.

Description : Are you having a Yankee Swap (aka White Elephant) in your office today?

Last Answer : surprised to hear of any office having parties this year- my husbands place which in the past had the most luxurious coctail parties and gift baskets and bottles of scotch sent home – they are having nothing this year

Description : Good gift ideas for a yankee swap?

Last Answer : 15 year olds , being one myself, like funny and kitschy type things. try going to the dollar store and getting random items with a well-known cartoon character on it. Everyone I know would enjoy that :)

Description : Does the motorola droid allow you to swap batteries without turning it off?

Last Answer : Doubt it. This “feature” is no different than any other phone (except iPhone).

Description : If you did a Wife Swap what kind of family would you be paired up with?

Last Answer : Very conservative, close-minded, extremely religious, very strict, all work and no fun types.

Description : BluRay and normal DVD, how do they swap?

Last Answer : Current Blu Ray players will play DVDs. To my knowledge, DVD players will not play Blu Ray discs. I bought my first Blu Ray player this weekend (+HDTV). The quality is simply unbelievable and is a significant upgrade from DVD – you won’t be disappointed.

Description : Ideas for White Elephant / Yankee Swap / Barona Gift Exchange / Thieving Secret Santa / Selfish Santa / Nasty Christmas / Dirty Santa / Scrooge's Christmas / Rob Your Neighbor / Thieving Elves / Chinese Gift Exchange?

Last Answer : A blanket, A calendar, lottery ticket (sucks when someone wins and doesn’t share though – LOL!) puzzle, DVD and popcorn, candle, gift card to Starbucks or Dunkin’s. I love Yankee Swaps! They are a lot of fun and I got a really nice blanket last year, fleece with color snowflakes on it. Have fun.