Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ENSE504 Introduction to Computing

Laboratory 2

Laboratory 2 – Numbers, Data Types, and Math Functions

Lab Objectives:

To gain experience with

· defining and using variables,

· using mathematical operators and functions,

· design of simple programs,

Visual Studio

Lab Procedure

Write programs for the following specifications.

For the Visual Studio programs that run on the PC, the following program template is recommended (vs_template.cpp).

You can copy and paste the below code to the Visual Studio.

In Visula Studio clik on File, New File, and select C++ File (.cpp) and save the below code as “vs_template.cpp” in your folder.

/*****************************************

Program Title:

File Name:

Author:

Date:

Description:

*****************************************/

#include 

using namespace std;

int main()

{

// Display program title.

// Define variables.

/**** Program operation (Input, Process, Output) ****/

// Get the input values.

// Calculate the answers.

// Display the results.

return 0; // end of program

}

These Visual Studio programs follow the pattern of:

· display the program title

· define variables, and then

· get input from the keyboard,

· perform calculations,

· display the answers.

This pattern describes an appropriate design for these programs.

Program 1. Integer Arithmetic

Write a program in C++ to input one integer and one double number from the keyboard.

Then calculate and display the answer for each of the five arithmetic operators:

add +, subtract -, multiply *, divide / and modulo %.

One example of running the program is shown below.

User input is shown underlined, but it will not be underlined on the computer screen when you run your program.

INTEGER ARITHMETIC

Enter first interger number: 35

Enter second double number:18.5

35 + 18.5 = 53.5

35 – 18.5 = 16.5

35 * 18.5 = 647.5

35 / 18.5 = 1.9

35 % 18.5 = Error

The arithmetic symbol and spaces can be displayed as strings, such as:

cout << " + " and cout << " = "

Remember you can display any number of items in a single statement, such as:

cout << x << " + " << y;

· What error do you get when using Build Solution?

· How do you fix this error?

· Test your program with several different pairs of numbers, including negative numbers and zero.

· What happens when the second number is zero?

· To test the % operation, change the type of both numbers to an integer.

· When testing % operation, what happens when both numbers are double?

Program 2. Simultaneous Equations

Write a program in C++ to solve two simultaneous equations in two variables, where the equation coefficients and constants may be real numbers.

From mathematics, the two simultaneous equations

and

have the solution

and

To make the program look like the mathematics, I suggest you use variable names a1, b1, c1 and a2, b2, c2 for the equation coefficients and constants.

Sample runs of the program are shown below.

SIMULTANEOUS EQUATIONS

Equation 1

Enter coefficient a1: 2

Enter coefficient b1: 3

Enter    constant c1: 3

Equation 2

Enter coefficient a2: 1

Enter coefficient b2: 2

Enter    constant c2: 2.5

The solution of   2.0 x +  3.0 y =  3.0

and   1.0 x +  2.0 y =  2.5

is x = -1.5

y =  2.0

When you are confident your program works correctly, try these values:

a1 = 2.1, b1 = 3.2, c1 = 1.3 and a2 = -2, b2 = 3.9, c2 = -2.5

For the next two programs, you need #include as well as #include because these programs are using the cmath library.

The cmath library contains an accurate value of p named M_PI and to access this, you need

#define _USE_MATH_DEFINES

before all the #include statements.

Program 3. Radius of a Circle

Write a program in C++ to input the area of a circle and then calculate and display the radius of the circle, using the formula

Hint: Use the cmath sqrt() function to calculate the square root.

Program 4. Radius of a Sphere

Write a program to input the volume of a sphere and then calculate and display the radius of the sphere, using the formula (the cube root of ).

Hint: Use the cmath pow() function with y = 1.0/3.0 to calculate the cube root.

Program 5. Data Types

Using the “sizeof” command, write a program in C++ to find the size of fundamental data types.

Provide the sceenshot of the output of your code that should look similar to the below chart:

The size of fundamental data types :
============================
The sizeof (char) is: 1 byte
The sizeof (short) is: 2 bytes
The sizeof (int) is: 4 bytes
The sizeof (long) is: 8 bytes
The sizeof (long long) is: 8 bytes
The sizeof (float) is: 4 bytes
The sizeof (double) is: 8 bytes
The sizeof (long double) is:   16 bytes
The sizeof (bool) is: 1 byte