If you have problems, ask a classmate or the instructor for help as needed.

Run the code and verify you get the message, "Hello, World!"

$ ./hellome
Hello, World!

Try changing the message to personally greet you with your own name, like the following:

Hello, Ed Parrish!

The code that starts with /** and ends with */ is known as a block comment. Comments are parts of code that are ignored by the compiler. Delete the entire block comment and then recompile and rerun your code.

You should see no change in how your code compiles or runs. If you see a difference, ask a classmate or the instructor for help as needed.

Look at the following line of the code:

} // end of main function

The last part of the line is another type of comment that starts with // and lasts until the end of the line. Delete the comment and then recompile and rerun your code.

You should see no change in how your code compiles or runs. If you see a difference, ask a classmate or the instructor for help as needed.

Remove the directive using namespace std; and then try to recompile the code.

Your program should not compile and you should get an error message. If you have a different experience, ask a classmate or the instructor for help as needed.

Restore the directive using namespace std; back into your program and verify that it compiles.

If you have problems, ask a classmate or the instructor for help as needed.

Through the miracles of computer science, we will now convert your $500 computer into a $5 calculator! Along the way, we learn how to work with arithmetic using C++.

Specifications

Type the following program into a text editor, save it as arithmetic.cpp, and then compile and run the starter program to make sure you typed it correctly.

Within the curly braces of the main() function, declare two double variables named a and b, and assign them a value of 5 and 2 respectively. For instance:

double a = 5, b = 2;

Add a line of code to display the arithmetic expression (a + b) and then recompile and run the program.

cout << "a + b = " << a + b << endl;

Notice that the last letter on endl is a lower-case "L", NOT a one. The output when you run the program should look like this:

a + b = 7

If you do not see this output, please ask a classmate or the instructor for help.

Add three more lines of code like the previous one that computes the expressions: a - b, a * b and a / b. Compile and run your program again and make sure your program now displays the following output:

a + b = 7
a - b = 3
a * b = 10
a / b = 2.5

The order of operations matters in C++ just like it does in algebra. Multiplication and division are performed before addition and subtraction. Add the following two statements to your program:

Note how the output of the two statements is different. You can change the order of operation using parenthesis, just like in algebra. For more information on the order of operations see section: 2.3.2: Arithmetic.

As you can see, arithmetic in C++ works much like you would expect. However, there are some mysteries when working with integer variables which we will explore in the next section:

Truncation in integer division

Modulus (%) operator

Save your arithmetic.cpp file as we will add to it in the following sections.

Specifications

Mathematical functions: More complex mathematical operations require the use of a function in C++. One such function is sqrt(number) which calculates the square root of the number inside the parenthesis.

Add the following statement to your program:

cout << "sqrt(a + b) = " << sqrt(a + b) << endl;

You program will not compile with this new statement because you must include a library of the mathematical functions. Add the statement: #include <cmath> to the top of your program like this:

Save your program source code that displays all eight (8) calculator operations so you can submit it to Canvas as part of assignment 2.

When completed, please help those around you. Then compare your code to the example below. Your code need not be exactly the same but it is helpful to see other solutions after you have solved the problem yourself.

Completed Program

Discussion Questions

How natural does arithmetic seem in C++ compared to what you normally use?

A mathematical function has an input and an output. What is the input and output of the C++ sqrt() function?