Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

Calculate the number of balls needed to fill a ball pit.

Ball pit

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

Just like in maths you can use brackets to change the default order of operations. Known as BIDMAS or BODMAS: Brackets, Indices, Division, Multiplication, Addition, Subtraction.

In C#, dividing two integers performs integer division (the fractional part is dropped). To divide as floating-point numbers, write at least one operand as a decimal: 4.0 / 3.0.

You can also structure your program into more than one subprogram.

Investigate

Questions to think about with this program to check your understanding:

Relation question

Explain why both int and double are used in programming. Why don’t we always use double for storing numbers?

Reveal answer

Doubles and integers are stored in different ways by the computer. Doubles usually use more memory than integers. Good programmers write programs that only use the memory they need.

Approach question

Why have comments been added to the end of the variable assignments for the units instead of storing ball_pit_radius as "1m" and the ball_pit_height as "0.2m" as you might in Maths or Science?

Reveal answer

Comments allow the programmer to remember the units of measurement. The processor can’t perform arithmetic on strings, only integer and double data types. Including the unit with the number would mean it is a string because letters are strings.

Make

Change the program below so that it:

  1. Asks the user to enter the radius and height of the ball pit.
  2. Asks the user to enter the radius of one ball in meters.
  3. Casts the number of balls to be an integer + 1.

Print the prompts as Console.WriteLine statements using the exact wording shown in the sample below.

Typical inputs and outputs from the program would be:

Enter the radius of the ball pit in meters:
2
Enter the height of the ball pit in meters:
0.5
Enter the radius of one ball in meters:
0.05
You need 9000 balls to fill the ball pit.
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Ball pit program

using System;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
    // Function to return the volume of the ball pit
    static double ball_pit_volume(double ball_pit_radius, double ball_pit_height)
    {
---
        double pi = 3.14;
---
        return pi * (ball_pit_radius * ball_pit_radius) * ball_pit_height;
---
    }

---
    // Function to return the volume of a ball
    static double ball_volume(double ball_radius)
    {
---
        double pi = 3.14;
---
        return (4.0 / 3.0) * pi * (ball_radius * ball_radius * ball_radius);
---
    }

---
    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        Console.WriteLine("Enter the radius of the ball pit in meters:");
        double ball_pit_radius = Convert.ToDouble(Console.ReadLine());
---
        Console.WriteLine("Enter the height of the ball pit in meters:");
        double ball_pit_height = Convert.ToDouble(Console.ReadLine());
---
        Console.WriteLine("Enter the radius of one ball in meters:");
        double ball_radius = Convert.ToDouble(Console.ReadLine());
---
        double packing_density = 0.75; // Volume taken up by the balls
---
        int balls = (int)((ball_pit_volume(ball_pit_radius, ball_pit_height) / ball_volume(ball_radius)) * packing_density);
---
        balls = balls + 1;
---
        Console.WriteLine("You need " + balls + " balls to fill the ball pit.");
---
    }
---
}