1. Relational Operators and the if Statement Consider the following code // This
ID: 3846136 • Letter: 1
Question
1. Relational Operators and the if Statement
Consider the following code
// This program tests whether or not an initialized value
// is equal to a value input by the user
// PLACE YOUR NAME HERE
#include <iostream>
using namespace std;
int main( )
{
int num1, // num1 is not initialized
num2 = 5; // num2 has been initialized to 5
cout << "Please enter an integer" << endl;
cin >> num1;
cout << "num1 = " << num1 << " and num2 = " << num2 << endl;
if (num1 = num2)
cout << "Hey, that’s a coincidence!" << endl;
if (num1 != num2)
cout << "The values are not the same" << endl;
return 0;
}
Exercise 1: Run the program several times using a different input each time. Does the program do what you expect? Is so, explain what it is doing. If not, locate the error and fix it.
Exercise 2: Modify the program so that the user inputs both values to be tested for equality. Make sure you have a prompt for each input. Test the program with pairs of values that are the same and that are different.
Exercise 3: Modify the program so that when the numbers are the same it prints the following lines:
The values are the same.
Hey that’s a coincidence!
Exercise 4: Modify the revised Exercise 3 program by replacing the two if statements with a single if/else statement. Run the program again to test the results.
Explanation / Answer
Execise 1 : Yes program does what it intends to do which is comparing the value initialized within the program with the value that has been inputted by the user.
Exercise 2:
#include <iostream>
using namespace std;
int main( )
{
int num1, // num1 is not initialized
num2; // num2 has been initialized to 5
cout << "Please enter an integer" << endl;
cin >> num1;
cout << "Please enter an integer" << endl;
cin >> num2;
cout << "num1 = " << num1 << " and num2 = " << num2 << endl;
if (num1 == num2)
cout << "Hey, that’s a coincidence!" << endl;
if (num1 != num2)
cout << "The values are not the same" << endl;
return 0;
}
Exercise 3:
#include <iostream>
using namespace std;
int main( )
{
int num1, // num1 is not initialized
num2; // num2 has been initialized to 5
cout << "Please enter an integer" << endl;
cin >> num1;
cout << "Please enter an integer" << endl;
cin >> num2;
cout << "num1 = " << num1 << " and num2 = " << num2 << endl;
if (num1 == num2)
cout << "The values are the same. Hey that’s a coincidence!" << endl;
if (num1 != num2)
cout << "The values are not the same" << endl;
return 0;
}
Exercis 4 :
#include <iostream>
using namespace std;
int main( )
{
int num1, // num1 is not initialized
num2; // num2 has been initialized to 5
cout << "Please enter an integer" << endl;
cin >> num1;
cout << "Please enter an integer" << endl;
cin >> num2;
cout << "num1 = " << num1 << " and num2 = " << num2 << endl;
if (num1 == num2)
cout << "The values are the same. Hey that’s a coincidence!" << endl;
else
cout << "The values are not the same" << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.