Lab: Simple Arithmetic

Tue Jun 17 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Purpose

This lab introduces students to working with floating-point and integer data types by combining user input, arithmetic operations, and formatted output. Students will write a program that takes two numbers as floating-point inputs, performs arithmetic operations (addition, subtraction, multiplication, division) using floating-point calculations, and then converts the inputs to integers to demonstrate integer division. This lab emphasizes the differences between floating-point and integer arithmetic, reinforcing key programming concepts that are fundamental for understanding data types and numerical operations in programming.

Task

This lab consists of the following tasks:

  1. Prompting for and Reading User Input:

    • Prompt the user to input two numbers as floating-point values (doubles).
    • Store the input values in appropriately typed variables.
  2. Performing Floating-Point Arithmetic:

    • Using the floating-point numbers provided by the user, perform addition, subtraction, multiplication, and division.
    • Store the results of these operations in variables.
  3. Demonstrating Integer Division:

    • Convert the floating-point inputs to integers.
    • Using the integer values, perform integer division and calculate the modulus.
    • Store the results in variables.
  4. Displaying Results:

    • Format and display the results of the floating-point operations with clear, descriptive messages.
    • Display the results of integer division and modulus with a message explaining the operation.

Steps to Complete the Lab

  1. Create variables to store two user-provided floating-point numbers.

  2. Use input/output statements to prompt the user and read their numbers.

  3. Perform floating-point arithmetic (addition, subtraction, multiplication, division) and store the results.

  4. Convert the floating-point inputs to integers.

  5. Perform integer division and modulus operations using the integer values and store the results.

  6. Display the results of all operations (floating-point and integer) with descriptive and formatted messages.

Criteria for Success

To successfully complete this lab, follow the checklist below:

  1. User Input:

    • Prompt the user to enter two numbers and store their inputs as floating-point values.
    • Handle typical inputs (valid doubles) without errors.
  2. Floating-Point Arithmetic:

    • Correctly calculate the sum, difference, product, and quotient of the user-provided floating-point numbers.
    • Store each result in a variable.
  3. Integer Conversion and Operations:

    • Convert the floating-point inputs to integers.
    • Correctly calculate the integer division and modulus of the converted values.
    • Store the results of these operations in variables.
  4. Output Results:

    • Display all results (floating-point and integer operations) with clear, formatted messages.
    • Include explanations for the floating-point and integer results to highlight differences between the two types of arithmetic.
  5. Code Quality:

    • Use clear, descriptive variable names that indicate their purpose.
    • Include comments to explain the steps and logic of the program.
    • Ensure the program is well-structured and properly formatted.
  6. Submission:

    • Save the program as \texttt{main.cpp} and ensure it compiles without warnings or errors.
    • Submit the completed file to CodeGrade before the deadline. Late submissions will not be accepted.

Example Output

Enter the first number (floating-point): 12.7 Enter the second number (floating-point): 3.4 Floating-point results: Sum: 16.1 Difference: 9.3 Product: 43.18 Quotient: 3.73529 Integer results (using truncated values): Integer Division Quotient: 3 Modulus: 0

Hand-In Procedure

  1. Save: Save your program as main.cpp. Do not ignore this step or save the file with different names. If CANVAS adds a -# to the file name it is because CANVAS saves all submissions, this is normal.

  2. Submit: You have until 11:59p on the due date to complete this lab. There will be no extensions for any reason. Your program must be submitted before the due day/time via the submission link(s) on CANVAS. No emailed submissions will be accepted.