Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94
Here’s a neat guide I found for learning C to Python or Python to C provided by David Brezeale.
Here’s a neat guide I found for learning C to Python or Python to C provided by David Brezeale.
Here’s an example of jQuizMe.
Goal: Make a quiz for Addition, Subtraction and Multiplication for two digit numbers.
Steps:
Step 1:
I wrote a random number generator in javascript here.
1 2 3 4 | var getRandomNum = function( i, decLen ){ var powOf10s = Math.pow( 10, decLen || 0 ); return i ? Math.floor( powOf10s * Math.random() * i ) / powOf10s : Math.random(); }; |
Or.. you could just use Math.floor( Math.random() * number ).
Step 2 & 3: (Due to time constraints I combined step 2 and 3.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | var link = "http://bateru.com/news/2011/04/learn-sig-figs-in-no-time/"; var ops = operations = [ " + ", " - ", " * ", " / " ]; var precision = 2; var arrOfRandomNum = function( arrLength, max, decLen ){ if( arrLength < 0 ){ return false; } var arr = [], i = arrLength; while( i-- ){ arr[ i ] = getRandomNum( max, decLen ); } return arr; }; var makeQuiz = function(){ var quiz = { "fill":[] }; var question = '', nums = []; var i = questionLength = 10; while( i-- ){ nums = arrOfRandomNum( 2, 19 ); question = nums.join( ops[ getRandomNum( ops.length ) ] ); quiz.fill.push({ ques: question, ans: eval( question ).toPrecision(precision) }); } return quiz; } |
Step 4:
1 2 3 | $( "#quiz" ).jQuizMe( makeQuiz(), { intro: ops.join(',') + " quiz.<br>Note: Your answer must have " + precision + " <a href='"+link+"'>sig figs</a>." }); |
Here’s the working demo.
Today’s Code of the Day is about how to find the digit of a number at a given position. I decided to go with the C / C++ programming languages to time, but you can easily translate the algorithms to other languages.
C Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h> #include <math.h> // Function: getDigitFromNum returns a digit at a given index of a integer. // Goes from right to left with the starting index of 0. int getDigitFromNum( int num, int digit ){ num /= pow( 10, digit ); return num % 10; } // Function: getDigitFromDec returns a digit at a given index of a double. // Goes from left to right with the starting index of 0. int getDigitFromDec( double dec, int digit ){ dec *= pow( 10, digit ); return (int)dec % 10; } // Function: getDigitFromNum returns the decimal values of a double. double getDecimals( double dec ){ return dec - (int)dec; } |
Example:
Input File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // Programmed by Larry Battle, bateru.com/news // Date: April 26, 2011 // Purpose: To demonstrate how to get a digit by a position index. #include <stdio.h> #include <math.h> int getDigitFromNum( int num, int digit ){ num /= pow( 10, digit ); return num % 10; } int getDigitFromDec( double dec, int digit ){ dec *= pow( 10, digit ); return ((int)dec) % 10; } double getDecimals( double dec ){ return dec - (int)dec; } void main(){ double dec = 1234.56789; printf( "\n%d", getDigitFromNum( (int)dec, 0 )); printf( "\n%d", getDigitFromDec( dec, 2 )); printf( "\n%f", getDecimals( dec )); } |
Output:
1 2 3 | 4 6 0.567890 |
1 2 3 4 5 6 7 8 | //Programmer: Larry Battle //Date: April 18, 2011 //Purpose: Get Fibonacci numbers in O(1) time. var getFibonacciNum = function( n ){ var s5 = Math.sqrt(5); var phi1 = (1+s5)/2, phi2 = (1-s5)/2; return Math.round((Math.pow( phi1, n ) - Math.pow( phi2, n) )/s5); }; |
Demo:
Output for 0 to 4
F(0) = 0 F(1) = 1 F(2) = 1 F(3) = 2 F(4) = 3