0

What is Temp in C++ and when we can use it

?

17th Feb 2018, 5:07 PM
PrincessA
PrincessA  - avatar
7 odpowiedzi
+ 7
C++ doesn't have anything called Temp. It is likely a variable that the person who wrote the code picked for some reason. Usually, it means the value isn't needed for more than a moment (i.e. it is a temporary value to be calculated, used, and forgotten.) If you show us where you saw it, we can give you a better answer.
17th Feb 2018, 5:40 PM
John Wells
John Wells - avatar
+ 4
So like I said, it is a temporary used to save the original. Whoever coded that program, really should have done this: #include <iostream> using namespace std; int main() { int original, absolete; cout << “enter an integer ” ; cin >> original; absolete = original; if (original < 0) absolete = -absolete; cout << “the absolute value of ” << original << “ is ” << absolete; return 0 ; }
17th Feb 2018, 6:04 PM
John Wells
John Wells - avatar
+ 1
Describe this more. I don't know what you are asking about.
17th Feb 2018, 5:30 PM
Zeke Williams
Zeke Williams - avatar
+ 1
Thank you so much now i get it
17th Feb 2018, 6:06 PM
PrincessA
PrincessA  - avatar
0
Here in this example
17th Feb 2018, 5:54 PM
PrincessA
PrincessA  - avatar
0
#incluse <iostram> Using namespace std ; Int main() { Int temp , number ; Cout<< “ enter an integer ” ; Cin>> number temp= number If (number<0 ) number = -number Cout<< “the absolute value of” <<temp << “is” << number ; return 0 ;
17th Feb 2018, 5:56 PM
PrincessA
PrincessA  - avatar
0
#include <iostream> using namespace std; int main() { int a = 5, b = 10, temp; cout << "Before swapping." << endl; cout << "a = " << a << ", b = " << b << endl; temp = a; a = b; b = temp; cout << "\nAfter swapping." << endl; cout << "a = " << a << ", b = " << b << endl; return 0; } Output Before swapping. a = 5, b = 10 After swapping. a = 10, b = 5 To perform swapping in above example, three variables are used. The contents of the first variable is copied into the temp variable. Then, the contents of second variable is copied to the first variable. Finally, the contents of the temp variable is copied back to the second variable which completes the swapping process. You can also perform swapping using only two variables as below. Example 2: Swap Numbers Without Using Temporary Variables #include <iostream> using namespace std; int main() { int a = 5, b = 10; cout << "Before swapping." << endl; cout << "a = " << a << ", b = " << b << endl; a = a + b; b = a - b; a = a - b; cout << "\nAfter swapping." << endl; cout << "a = " << a << ", b = " << b << endl; return 0; }
5th Sep 2021, 6:00 AM
MJ Udít ßhãRmá
MJ Udít ßhãRmá - avatar