In this lab, you will investigate int and float data types and arithmetic expressions. You will also practice using output manipulators and string operations.
Open Microsoft Visual C++ 2010 Express; create a new project called Convert (use the same configuration as lab #1). Enter the following program to your project as Convert.cpp. Use this program for exercises 1-6.
// Program Convert converts a temperature in Fahrenheit to
// Celsius and a temperature in Celsius to Fahrenheit
#include < iostream >
using namespace std;
int main()
{
const int TEMP_IN_F = 32;
const int TEMP_IN_C = 0;
int fToC; // Place to store Celsius answer
int cToF; // Place to store the Fahrenheit answer
cToF = (9 * TEMP_IN_C / 5) + 32;
fToC = 5 * (TEMP_IN_F - 32) / 9;
cout << TEMP_IN_F << " in Fahrenheit is " << fToC
<< " in Celsius. " << endl;
return 0;
}
Compile and run the program Convert. What value is written to the screen?
Notice that the program computes two values (cToF and fToC) but only one value is written on the screen. Insert a second output statement that prints the value of cToF in similar format on the next line.
Consider the program in Exercise 2. Change the values for constants TEMP_IN_F and TEMP_IN_C to the following values and compile and rerun the program after each change. Record the values for fToC and cToF for each set of values. Clearly write the table in your lab report.
TEMP_IN_F | TEMP_IN_C | fToC | cToF | |
(a) | 212 | 100 | ||
(b) | 100 | 50 | ||
(c) | 122 | 37 | ||
(d) | (you choose) | (you choose) |
Examine the output in Exercise 3 from (b) and (c). There seems to be an inconsistency. Describe the inconsistency and make a hypothesis to explain it.
Change the integer constants and variables in the program Convert to type float and rerun the program with the same data you used in Exercise 3 and fill the table again. Do the results confirm your hypothesis?
Use the following program for Exercise 6.
// Program DecInc works with the Increment and Decrement operators
#include < iostream >
using namespace std;
int main()
{
int i=0, j=0;
// Statement Group 1
cout << "Post-increment of i = " << i++ << endl;
cout << "Pre-decrement of i = " << --i << endl;
// Statement Group 2
cout << "Pre-increment of j = " << ++j << endl;
cout << "Post-decrement of j = " << j-- << endl;
return 0;
}
Use the following table to make your prediction of the output prior to running the program
Post-increment value of i | Pre-decrement value of i | |
Statement Group 1 | ||
Pre-increment value of j | Post-decrement value of j | |
Statement Group 2 |
Now create and execute the program. Explain why your prediction was correct for each statement and variable combination or why it wasn't. Use a similar table to the one above for your explanations.
Modify the program from Exercise 6 to print out the values of the variables i and j just prior to the return statement and record the values. Explain why the variables have their current values?