i’ve searched what does it mean by temporary variable but i couldn’t & i couldn’t understand. i’ve done the if-else structures without a temporary variable and now i’m trying to do if-else structure and a temporary variable. I couldn’t find the difference.
the question is to
- write a program that reads three integers(a,b and c) and prints the largest of values using if else structure and a temporary variable.
- if else structures without a temporary variable.
I’ve done the first question.
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int a,b,c;
cout<<"a : ";
cin>>a;
cout<<"b : ";
cin>>b;
cout<<"c : ";
cin>>c;
if(a>b && a>c)
{
cout<<"largest : "<<a;
}else{
if(b>a && b>c)
{
cout<<"largest : "<<b;
}else{
if(c>a && c>b)
{
cout<<"largest : "<<a;
}else{
cout<<"error!";
}
}
}
getch();
return 0;
}
but for the second question is it like this?
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int a,b,c;
int max;
cout<<"a : ";
cin>>a;
cout<<"b : ";
cin>>b;
cout<<"c : ";
cin>>c;
if(a>b && a>c)
{
max=a;
cout<<"largest : "<<max;
}else{
if(b>a && b>c)
{
max=b;
cout<<"largest : "<<max;
}else{
if(c>a && c>b)
{
max=c;
cout<<"largest : "<<max;
}else{
cout<<"error!";
}
}
}
getch();
return 0;
}
i just want to be confirm because i don’t exactly understand what does it mean by temporary variable.
thanks.
Actually, you’ve done the second question as your first result. The second version (answering the first question) should look more like this:
The purpose of the “temporary” variable is to avoid repeating all the print statement as your first example has.
I don’t like this use of the word temporary. Temporary variables have a special meaning in C++ totally related to variables created by the compiler implicitly when you use certain code constructs. “max” is not a temporary variable, in that sense. Your instructor is perhaps being loose in his use of the terminology.