Tejas: In our Science class we discovered that the number of petals on flowers is mostly one of the numbers in a sequence called the Fibonacci seauence.

13 petals 5 petals

Moz: What are fibonacci numbers and how do you get the sequence of Fibonacci numbers?

Jyoti: In mathematics, the Fibonacci numbers are the numbers in the following integer sequence:

0, 1, 1, 2,3,5,8, ......

Tejas: The first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two numbers. For example, the next number (shown above) is 5+8 = 13.

Jyoti: Let us write a program to generate the Fibonacci sequence of numbers, upto 1000.

Tejas: Let us first write the algorithm for generating the sequence.

Fibonacci number generation algorithm / Fibonacci sequence
Start
a = 0
b =1
next_sequence_no=a+b = 1
Print next_sequence_no
a = b (since b=1, a =1)
b = next_sequence_no = 1
next_sequence_no =a+b=1+1=2
Pint next_sequence_no
a=b (since b =1, a =1)
b=next_sequence_no=2
next_sequence_no=a+b= 1+2 =3
Print next_sequence_no
a=b (since b=2, a =2)
b=next_sequence_no=3
Continue till next_sequence_no < 1000 / 1
2
3

Tejas: The repeat statement “For” will not work here as we do not know the number of times that we need to repeat a set of statements. Let us see if we have other repeat statements that we can use.

Jyoti: We can use “While” statement.

Info: While

Repeat - Do something while a condition remains true
Syntax
while condition
statement(s)
end while /
  • Do the statements in the block over and over again while the condition is true.
  • The statements will be executed zero or more times.
/

Jyoti: We can also convert text to speech. In the above program before the “input” statement we can put the following statement and the computer reads out the text.

Tejas: Wow! This is cool!

Text to Speech
Syntax
say expression / Say “Hello how are you?”
The say statement is used to make BASIC-256 read an expression aloud, through the computer's speakers. /
Hello How are you

Jyoti: Now I want to build a program for a simple calculator.

Tejas and Jyoti write the algorithm for the calculator:

  1. Ask the user for the operation to be performed. (Addition, subtraction, multiplication, division).
  2. Ask th user to enter two numbers num1, num2 for the operation.
  3. Use a condition block to identify the operation selected by the user.
  4. Write statements for performing each operation.
  5. Print the result.

Moz: You can use subroutines for each of the operations.

Tejas: What are subroutines?

Moz: We have seen that there are many times we want to reapeat certain blocks of code multiple times. A loop (For and while statement) repeats these blocks one after the other. But if we want to repeat the block of code at different times then we use a subroutine. Subroutine is a block of statements which has a label. Whenever you want these block of statements to be executed, you just use the gosub statement. This is called a call to the subroutine.

Jyoti: Why should we use subroutines?

Moz: Subroutines help you to understand a program easily. For example look at the following example. Explain what the program does?

Jyoti: This is a simple calculator. Depending on the choice of the operation the program just calls the subroutine and does the calculation.

Tejas: If the code was written in the program itself, instead of call to subroutines then we would have taken some time to give the answer. Yes. It is very easy to read and understand.

Jyoti: When we work in teams then if we divide the program into subtasks each one of us can write part of the code and also independently test it. Then we can combine the code.

Moz: Right.

Jyoti: Can we call the subroutine any number of times.

Moz: Yes. You can.

Concept: subroutine

  • Subroutine is used to repeat a block of code at different places in a program.
  • When a subroutine is called the control flow of the program moves into a subroutine. When the block of code is executed the control flow of the program continues from the next statement from where the subroutine was called.
  • Advantages of using a subroutine:

◦A program is easy to read.

◦Programs are easier to design

◦Teams - When more than one programmer works on a single program at the same time, each team member can decide on which part of the code he/she wants to write and independently test it.

Subroutines
  • A subroutine has a label followed by a sequence of statements and ends with a return statement.
  • You may have multiple subroutines in a single program.
/
Syntax Label
  • A label name is followed with a colon (:)
  • Must be on a line
with no other statements;
  • Must begin with a letter.
  • May
contain letters and numbers, and are case sensitive. /
Syntax gosub label
  • The gosub statement causes the execution to jump to the
subroutine defined by the label.
Syntaxreturn
To send
control back to where the subroutine was called from a return is used.

BASIC Worksheets:

  1. Complete the following programs by filling in the blanks:

a)Find numbers whose square is less than 100.

N = 1

square_of_N = N*N

While ______

if ______then

Print N

N = N+1

square_of_N = N*N

Endwhile

b)A call to the subroutine and the subroutine from the program to play “Tic Tac Toe” game are given. Fill the call to the subroutine and the statements in the subroutine. In the subroutine the message “You have won” is given aloud 5 times.

# Tick Tack Toe

...

# Call subroutine to convey message, if the player wins the game

Gosub ______

End

# subroutine to display the message

Message:

For ______

Say______

Next ______

Return

c)The names of two groups Red and Blue are stored in two arrays. The member of the groups are as follows:

Red : Namita, Payal, George, Ali, Neha

Blue: Shakti, Murtaza, Nikhil, Anne, Shabana, Priyanka

The user is prompted to specify which group list should be displayed.

As per the input from the user the group names are displayed.

Complete the program by filling in the blanks.

#Declare the array for the two groups

DIM ______

DIM ______

#Assign names of members of group blue to the blue array

______

#Assign names of members of group blue to the blue array

______

# Take the choice of group from the user

Print“Press 1 for group red and 2 for group blue”

input ______

if ______then

Gosub ______

if ______then

Gosub ______

# subroutine to display the group red

______:

For ______

Print ______

Next _____

Return

# subroutine to display the group blue

______:

For ______

Print ______

Next _____

Return

2. Complete the following program using the hints given, and save the code in a file called amstrong.kbs. Run it to get the armstrong numbers between 0 to 999.

Rem PROGRAM Armstrong Numbers
CLS
PRINT "ARMSTRONG NUMBERS"
PRINT
PRINT "Program computes all Armstrong numbers in the range of 0 - 999."
PRINT "An Armstrong number is a number such that the sum of its digits"
PRINT "raised to the third power is equal to the number itself."
PRINT
#initialize total_armstrng to save the count of armstrong numbers between 0-999.
total_armstrng=0
FOR A = 0 TO 9
FOR B = 0 TO 9
FOR C = 0 TO 9
Number= A*100 + B*10 + C
Sum_Acube_Bcube_Ccube = A^3 + B^3 + C^3
IF______THEN GOSUB display
(Hint: Write comparitive statement to find if the number is armstrong number.)
NEXT C
NEXT B
NEXT A
END
display:
(Hint: Write a statement to count the total number of armstrong numbers between 0 to 999. Increment total_armstrng by 1)
PRINT______
(Hint: Write a statement to display the armstrong number, Number.)
RETURN

3. Write a program for calculating squares and cubes of a number and displaying the result:

  • Ask the user to enter a number. (Note: Use the text to speech statement for asking the user.)
  • Call a subroutine to calculate square of the number.
  • Call a subroutine to calculate cube of the number.
  • Display the square and cube of the number.

4. You are given an algorithm where a students grades for each subject are calculated and displayed. Use the algorithm to write a program in BASIC.

Algorithm to calculate and display grades in each subject of a student.

  • Define a string array subject$(4), and assign the names of the subjects {“Maths”, “Science”, “English”, “French”, “History”} to the array elements.
  • Define a string array grade(4).
  • Initialize an integer i=0, for using in while loop
  • Ask the user to input his/her name.
  • Write a while loop

◦Execute the while loop for i<4.

◦Take input from user for each subject and calculate the grade.

◦Call the subroutine calc_grade to calculate the grade.

◦Increment i by 1.

  • Call subroutine print_grade to print subject and it's grade.
  • End the program.
  • Subroutine calc_grade

◦compare the value with the guidelines given and find the grade.

◦Save the grade in the corresponding array element of grade.

  • Subroutine print_grade

◦Display a message to indicate whose grades are being diplayed. Use the name that the user inputs.

◦Write a for loop to display the subject and grade.

Grades in each subject are given using the following guidelines.

Marks / grade
Grater than or equal to 75 / A
Less than 75 and greater than or equal to 60 / B
Less than 60 and grater than or equal to 50 / C
Less than 50 / D

Activity

  1. Text messaging:

Using a cell phone is popular among teenagers. Short forms and symbols are used to abbreviate message and reduce typing. But it is a mystery to some adults. Write a program

that will continually input a short form and output the translation and say it loud, for an adult. You may add more that you think are relevant. When the user enters “TTYL” the program ends.

Hint: Use arrays to store the abbreviations and messages.

Use the following abreviations and translation table:

Abbreviations / Message
CU / see you
:-) / I’m happy
:-( / I’m unhappy
;-) / wink
:-P / stick out my tongue
TA / totally awesome
CUZ / because
TY / thank-you
YW / you’re welcome
TTYL / talk to you later