0% found this document useful (0 votes)
42 views6 pages

Practice 11

The document provides instructions and examples for using math operations in the Small Basic programming language. It includes a list of over 20 built-in math methods, such as Abs, Ceiling, Floor, and GetRandomNumber. Two challenges are presented that involve creating a grade calculator and quadratic formula calculator using these math functions. Code solutions are provided for both challenges that break down the steps involved and show how to use conditionals and variables to calculate the necessary values.

Uploaded by

ulzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views6 pages

Practice 11

The document provides instructions and examples for using math operations in the Small Basic programming language. It includes a list of over 20 built-in math methods, such as Abs, Ceiling, Floor, and GetRandomNumber. Two challenges are presented that involve creating a grade calculator and quadratic formula calculator using these math functions. Code solutions are provided for both challenges that break down the steps involved and show how to use conditionals and variables to calculate the necessary values.

Uploaded by

ulzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PRACTICE 11:

BASIC MATH

Page |1
Math Beyond Numbers
Math is not just about solving equations or building a
calculator. We use math in computer science for many things,
but also to create art and games, because it helps us describe
our environment.

Math Methods
Not only can we do math with our basic addition, subtraction, multiplication, and division, but
the Math object in Small Basic has a number of operations built into it that you can use as you
design art, games, and yes, even calculators!
Here is a list of just some of the operations: Abs, Ceiling, Floor, GetRandomNumber, Max, Min,
Power, Remainder, Round, SquareRoot
Each operation does something different with the numbers, and the explanations of each can be
seen in Small Basic. Below is the description of Math.Abs with an example as well. In Small Basic,
you can type ‘Math.’ in order to view all the different operations, some of which will be covered
in a later practice about more complex math.

All of these operations allow you to perform different actions with


numbers, and can be combined as well. For example, if you wanted to roll
a dice, you could use Math.GetRandomNumber(6). The 6 in the
There are parenthesis specifies that we want to get a random number between 1 and
over 20 built 6, since those are the minimum and maximum values on a dice. Another
in Math example could be that you are calculating how much money you need to
methods you
pay a friend, but you only have bills and no coins, so you can’t give them
can use!
anything that isn’t a perfect round number. Depending on if you want to
just round normally, always round up, or always round down, you could
use the Round, Ceiling, and Floor operations to round cents to dollars.

Page |2
Challenge: Grade Calculator
Have you ever wanted to calculate the percentage you
need on a final to get your grade in a class and had to
use a dozen websites? Try creating your own calculator using Small Basic’s various math
functions by using your current grade, the final’s weight, and your predicted grade. Although
this can be completed in a single step, it’s best to realize all the necessary variables and
functions to be used. Hints are available below.

First, we need to gather input from the user. This number must be a positive, whole number,
also known as an integer. For each value (current grade, weight, and grade wanted), we must
define the variable right after we give our user instruction.

Then, we use a variety of math operations that Small Basic automatically can read and
understand.

Challenge: Quadratic Formula Calculator


Remember using the quadratic formula? This equation is super helpful in finding
the zeroes of a graph. For this challenge, try unpacking the formula (on the left)
and see which Small Basic Math operations would be helpful.

There are a few steps involved in completing this challenge, and some of them require a little bit
of problem solving, so if you need some hints, see below!

The first thing to think about is the discriminant, which is the part of the quadratic equation
inside of the square root. If this is negative, the x values will end up being complex numbers,
which is something we wouldn’t be able to display using Small Basic. We need to utilize what we
learned earlier about conditionals to ensure that the rest of the code is only run if the
discriminant is positive.

Once it has been established that the discriminant is positive, we can continue with calculating
the answer. Since the quadratic equation contains a plus or minus sign, we know that there are
going to be two different answers, so we can set up two variables and continue following the

Page |3
operations of the formula to get the answers. Once this is
done, the final step is to print the answers so the user can see
them!

Still can’t figure these challenges out? See the last two pages
for the code and try to understand each line.

Discussion Questions
o What are other apps/websites you use that might need Math operations such as the
ones in Small Basic?
o Would you use any Math operations in your classroom? Why or why not?
o Could you think of any operations you use in your math class that we didn’t list?
o Why do you think Math operations are built into Small Basic? How is that helpful for
us?

Additional Resources
 Small Basic: The Math Object
o https://aka.ms/sbcurriculum3.3
 Small Basic Tutorial
o https://aka.ms/sbcurriculum3.3

Page |4
Grade Calculator Solution:

TextWindow.WriteLine("Enter your current grade:


")

current = TextWindow.ReadNumber()

TextWindow.WriteLine("Enter the weight of your final: ")

weight = TextWindow.ReadNumber()

TextWindow.WriteLine("Enter the grade you want in your class: ")

goal = TextWindow.ReadNumber()

value1 = (100-(weight))*(current)/100

TextWindow.WriteLine(value1)

value2 = (goal) - (value1)

TextWindow.WriteLine(value2)

gradeOnFinal = ((value2)/(weight))*100

TextWindow.WriteLine(gradeOnFinal)

TextWindow.WriteLine("You will need a " + gradeOnFinal + " to get an " + goal


+ " in your class.")

Page |5
Quadratic Formula Calculator
Solution:
TextWindow.WriteLine("Enter a")

a = TextWindow.ReadNumber()

TextWindow.WriteLine("Enter b")

b = TextWindow.ReadNumber()

TextWindow.WriteLine("Enter c")

c = TextWindow.ReadNumber()

'The b^2 - 4ac part of the quadratic formula is called the discriminant

discriminant = (Math.Power(b,2)) - (4*a*c)

If discriminant >= 0 Then

x1 = (-b + Math.SquareRoot(discriminant)) / (2*a)

x2 = (-b + (-1 * Math.SquareRoot(discriminant))) / (2*a)

TextWindow.WriteLine("x1 = " + x1)

TextWindow.WriteLine("x2 = " + x2)

Else

TextWindow.WriteLine("The x values are not real, sorry")

EndIf

Page |6

You might also like