Objectives

In this lab, you will investigate int and float data types and arithmetic expressions. You will also practice using output manipulators and string operations.

Arithmetic Expressions and Output Manipulators

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;
}

Exercise 1

Compile and run the program Convert. What value is written to the screen?

Exercise 2

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.

Exercise 3

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)

Exercise 4.

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.

Exercise 5.

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?

Increment and Decrement Operators

Exercise 6

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.

Exercise 7

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?

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.