0
What is Temp in C++ and when we can use it
?
7 Respostas
+ 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.
+ 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 ;
}
+ 1
Describe this more. I don't know what you are asking about.
+ 1
Thank you so much now i get it
0
Here in this example
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 ;
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;
}