Please, can someone help me in this exercise, because I really don't know too much about swaping!...

_____

Write the code that swaps the value of two int variables. Then round both variables to the nearest tenth. example: (If x contains the value of 43 and y the value of 87, then after the swap, the x should contain the value 87, and y the value 43. After rounding, x should contain a value of 90, and y the value of 40. )

THANK YOU!

Do you mean to write a function that swaps two variables' values, and rounds to the nearest tens (not tenths, according to your example)? Or to just write code that does it in your main body? You'll use the same mechanism in either case.

Do you know how to implement rounding of any sort? Think of what it means, and the various arithmetic tools you have in C++ to do it - it's really quite easy.

What have you written so far? You have to show us some work, and we'll help point you in the right direction.

Well, I mean I haven't done anything because it was given to us just like that. To write the code, I don't need to initialize anything or execute it, I'm just worried how to write this code if any in a piece of paper.

Well think of how you would do things if you had a red ball in your right hand, a green ball in your left. What steps would get the balls to be in the opposite hands? Keeping in mind, you can have at most one ball in any hand at any time.

Ohh I see, probably I must create a temporal variable, like z, put one number in there, change the value of x to y and the value in y that is in z put it back into variable x.

Bingo, give that student a gold star!

Now on to your rounding problem. Remember that you will only need some of the basic arithmetic operations + - * / %
First, how can you convert any number to simply end in 0, that is, eliminate the units value. Then, modify that so rounding is occurring.

This article has been dead for over six months. Start a new discussion instead.