Class 6a ruby math

Post on 18-Nov-2014

2.154 views 0 download

description

In this second half of Class 6, we will investigate a few of Ruby's capabiities to complete advanced formatting and mathematical calculation.

Transcript of Class 6a ruby math

MATH HACKS IN RUBYClass 6a – Ruby Math

Introduction

Ruby Basics Puts Gets Some basic math operations

“+”, “-”, “*”, “/” Formatting and Advanced Math

Line formatting (right/left justification, centering)

Exponents, roots, the Math Object

Agenda

Review the Assignment 3 solution Add formatting

to make interface and output more attractive

To make interface more intuitive To make output more functional

Advanced Math Exponents and Roots Absolute and Modulus Math::pi and rand

Formatting

Example – our class average assignment

Preferred format/ line length of 60 Student MarksSam 75Susan 78George 87Anne 92Joe 67_____ _____Average 76.5

Demonstration

IPO Input Processing Output

Code

Ruby Math: Exponent and Root

Exponent Notation a**b a to the power of b Example

Root Using Math Object

Math.sqrt(val) Using math formula

x**0.5

Ruby Math: Absolute & Modulus

Absolute Notation a=x-y.abs Example

Modulus (gizinta, what’s left?) Notation a=x%y Example: Kids Inc. Summer Camp

Ruby Math: Random & Pi

Random Use: rand(max_val) Result is zero based Example

Math:Pi Use Math::Pi*(val) Example: Volume of a cylinder IPO first?

Summary

Exponent (a**b) Root (sqrt or a**b/c.to_f) Absolute (abs) Modulus (a%b) Random (rand(x)) Pi (Math::PI)

Project 1: Calculator/Converter

Objectives Apply mathematical formulas to real

problems Apply a programmatic approach to a

solution Apply basic ruby mathematical

operations Apply basic ruby formatting

Grading – in 3 parts Due Date: Week of Nov 15 Next Week: Part of class for project

work, problems, challenges, ideas