Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Challenge: Division int and double | Introduction
C++ Data Types

bookChallenge: Division int and double

We will not stop on most operations since they work as expected. Instead, let's look at the reasons behind the incorrect result obtained from the division operation. Those are explained in a video below:

It's not just a problem limited to division. When you use operators such as +, -, *, or / on variables of certain types, the resulting value will have the same type as the operands.

It's important to be mindful of these possibilities and take them into account.

Taak

Swipe to start coding

Imagine you're building a taxi meter program: the GPS gives a fractional distance (12.7 km), but the meter rounds it down to a whole number. You need to write a function that calculates the customer's fare.

  1. Use the existing function calculatePayment, which has a return type of int and two parameters:
    • double distance_km — traveled distance.
    • int rate_per_km — price per kilometer.
  2. Multiply distance_km by 1 to convert it to an int and assign the result to the variable distance_km_int.
  3. Declare a type for payment_exact that should store an integer value.
  4. Multiply the distance by the rate per kilometer and assign the result to payment_exact.
  5. Return the result.

Oplossing

solution.cpp

solution.cpp

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 3
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain how to ensure the result of division is a floating-point number in C++?

What are some common mistakes beginners make with type conversion in C++?

Can you give more examples of type-related issues with arithmetic operations?

close

Awesome!

Completion rate improved to 4.35

bookChallenge: Division int and double

Veeg om het menu te tonen

We will not stop on most operations since they work as expected. Instead, let's look at the reasons behind the incorrect result obtained from the division operation. Those are explained in a video below:

It's not just a problem limited to division. When you use operators such as +, -, *, or / on variables of certain types, the resulting value will have the same type as the operands.

It's important to be mindful of these possibilities and take them into account.

Taak

Swipe to start coding

Imagine you're building a taxi meter program: the GPS gives a fractional distance (12.7 km), but the meter rounds it down to a whole number. You need to write a function that calculates the customer's fare.

  1. Use the existing function calculatePayment, which has a return type of int and two parameters:
    • double distance_km — traveled distance.
    • int rate_per_km — price per kilometer.
  2. Multiply distance_km by 1 to convert it to an int and assign the result to the variable distance_km_int.
  3. Declare a type for payment_exact that should store an integer value.
  4. Multiply the distance by the rate per kilometer and assign the result to payment_exact.
  5. Return the result.

Oplossing

solution.cpp

solution.cpp

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 3
single

single

some-alt