CPSC170
Fundamentals of Computer Science II

Lab 1

Variables and Data Types (Types)

Some Important Commands

For removing files and directories: rm

Tutorialspoint

For moving files and directories: mv

Howtoforge

For removing empty directories:

rmdir

Howtoforge.

Age in Seconds Using int

Details

Create a C++ program that prints your age in seconds. Assume there are 365 days in a year. You don’t need to compute your exact age, just convert your age in years to seconds. The program should use integer types (int), arithmetic operations to perform the calculations, and should print the number of seconds nicely formatted and labeled.

Example

John's age in seconds is 1198368000

Age in Seconds using unsigned

Details

Repeat the previous exercise using unsigned instead of int.

What other types can be used in the previous exercise?

What type do you think is best suited?


Voyager 1

Write a program that prints how long it takes a signal from Voyager 1 to reach the Earth. Since Voyager 1 is constantly moving away from Earth, the program should have a variable for Voyager’s current distance from Earth, in kilometers. The Voyager 1 communicates with radio waves, which travel at the speed of light, 299,792,458 m/s.

Example

It takes 69663.7 seconds for a message from Voyager to reach Earth.

Questions

Is it possible to implement your program without using floating point numbers?

What type do you think is best suited?


Euclidean distance

Write a program in main that declares four floating-point variables, x_1, y_1, x_2, y_2. These variables are the coordinates of two points on the Euclidean plane. The coordinate of the first and second points are x_1, y_1 and x_2, y_2 respectively. The distance can be computed using the formula [(x_1-x_2)^2 + (y_1-y_2)^2]^0.5. Use the pow function to compute the exponent. You will need to include the header file Math.h. An example showing the use of the pow function can be found here. The output of your program should have the following form.

Example

The distance between the points (0,4) and (3, 0) is 5.

Arithmetic overflow

Write a program that prints out the largest long.
What happens when you add one to it?


Leap year

Write a program to determines whether or not a year is leap year. For a year to be a leap year it must satisfy the following conditions:

  1. It must be divisible by 4.
  2. It must not be divisible by 100 unless it is also divisible by 400.

Your program should print out a statement of the following form:

The year 1900 is not a leap year.
The year 2020 is a leap year.