0% found this document useful (0 votes)
21 views16 pages

One-Dimensional Arrays in BASIC Guide

The document provides a comprehensive overview of one-dimensional arrays in BASIC, including their definition, declaration, initialization, accessing, and modifying elements. It also covers common use cases for arrays and includes examples of BASIC programs that utilize arrays for summing elements. Additionally, the document discusses various number systems, conversions between them, and provides detailed examples for converting between binary, decimal, octal, and hexadecimal formats.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views16 pages

One-Dimensional Arrays in BASIC Guide

The document provides a comprehensive overview of one-dimensional arrays in BASIC, including their definition, declaration, initialization, accessing, and modifying elements. It also covers common use cases for arrays and includes examples of BASIC programs that utilize arrays for summing elements. Additionally, the document discusses various number systems, conversions between them, and provides detailed examples for converting between binary, decimal, octal, and hexadecimal formats.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lesson Notes on One-Dimensional Arrays in BASIC

In BASIC, a one-dimensional array is a type of variable that can store multiple values of the
same type in a sequential order. A one-dimensional array can be used to simplify storing and
manipulating a collection of related data.

1. What is an Array?

 An array in BASIC is a container that can store multiple values under one variable
name.

 A one-dimensional array is simply a list of data elements arranged in a sequence.

2. Declaring a One-Dimensional Array

 To declare an array, you specify the variable name and the number of elements it will
store.

 Syntax:

DIM arrayName (size)

o arrayName is the name of the array.

o size is the number of elements you want the array to hold (starting from 0 up
to size-1).

Example:

DIM numbers(5) ' Declares an array of 6 elements (0 to 5)

3. Initializing an Array

 You can initialize an array at the time of declaration or later in the program.

 If you don't initialize it, the array elements will default to 0.

Example:

DIM numbers(4) ' Declare an array of 5 elements (0 to 4)

' Initialize the array

numbers(0) = 10

numbers(1) = 20

numbers(2) = 30

numbers(3) = 40

numbers(4) = 50

4. Accessing Array Elements


 Array elements can be accessed by their index (position in the array).

 The index starts at 0 and goes up to size - 1.

Example:

RINT numbers(0) ' Output: 10

PRINT numbers(2) ' Output: 30

5. Modifying Array Elements

 You can modify an element by assigning a new value to a specific index.

Example:

numbers(1) = 100 ' Modifies the second element (index 1) to 100

PRINT numbers(1) ' Output: 100

6. Using Loops with Arrays

 You can use loops (like FOR...NEXT) to access and manipulate each element in the
array.

Example:

DIM numbers(4)

' Initialize the array

numbers(0) = 10

numbers(1) = 20

numbers(2) = 30

numbers(3) = 40

numbers(4) = 50

' Loop to print each element

FOR i = 0 TO 4

PRINT "Element "; i; ": "; numbers(i)

NEXT i

Output:

Element 0: 10
Element 1: 20

Element 2: 30

Element 3: 40

Element 4: 50

8. Common Use Cases

 Storing lists of data: Arrays are useful for storing a list of items, such as scores,
names, or any other set of data.

 Mathematical operations: Arrays are often used in math-related tasks like storing a
series of numbers to compute averages, sums, etc.

 Sorting: Arrays can be sorted using different algorithms like bubble sort, selection
sort, etc.

9. Example Program (Summing Array Elements)

Here's an example of a BASIC program that sums the elements of a 1D array:

DIM numbers(5)

numbers(0) = 10

numbers(1) = 20

numbers(2) = 30

numbers(3) = 40

numbers(4) = 50

total = 0

FOR i = 0 TO 4

total = total + numbers(i)

NEXT i

PRINT "Total sum of array elements: "; total

Output:

Total sum of array elements: 150


Definition of Number System
A number system is a collection of symbols used to represent small numbers, together with
a system of rules for representing larger numbers.
There are various number systems, some are examined below:
Decimal Number System
The decimal numeral system (also called base ten or occasionally denary) uses various
symbols (digits) for no more than ten distinct values (0, 1, 3, 4, 5, 6, 7, 8, and 9)
Binary Number System
The binary number system is a number system in base 2 and therefore requires only two
digits, 0 and 1.
Hexadecimal Number system
The hexadecimal is base 16 digits. The digits 0 through 9 are used, along with the letters A
through F, which represent the decimal values 10 through 15.
Octal Number System
The octal numeral system, or oct for short, is the base-8 number system and uses the digits 0
to 7

Conversion From one Number System to Another

Binary Number Conversion

a. Conversion from Binary to Octal Number System


A binary-to-octal table conversion is needed to directly convert from binary to octal. The
table is given below.

Binary to Octal Table Conversion

Bin 000 001 010 011 100 101 110 111

Oct 0 1 2 3 4 5 6 7

Next, group the binary digits into sets of threes starting with the least significant (rightmost)
digits. Then look up each group in the table above.
Example 1: convert 111001012 to octal number system.
Solution
Add leading zeros or remove leading zeros to group into sets of three binary digits.
111001012 = 011 100 101
By looking up these values in the table above, 011 is 3, 100 is 4 and 101 is 5
Therefore 111001012 = 3458
Method 2
Using normal Mathematics method which involves converting 111001012 to decimal number
system and thereafter convert the result to an octal number system.
To convert 111001012 to decimal multiplying each digits by the base in an increasing power
starting with the least significant (rightmost) digit.
111001012
= 1x27+1x26+1x25+0x24+0x2 3+1x22+0x21+1x20
= 128+64+32+0+0+4+0+1
= 22910
Next convert 22910 to base eight by dividing 229 by 8 and writing down the remainder "R"

8229R

825 5

83 4

0 3

Pick “R” from bottom to top


Therefore, 111001012 = 3458

b. Binary to Hexadecimal
A binary to Hexadecimal table conversion is needed to directly convert from binary to Hex.
The table is given below.

Binary to Hexadecimal Table Conversion

Bin 0000 0001 0010 0011 0100 0101 0110 0111

Hex 0 1 2 3 4 5 6 7

Bin 1000 1001 1010 1011 1100 1101 1110 1111

Hex 8 9 A B C D E F

Next, group the binary digits into sets of four, starting with the least significant (rightmost)
[Link], look up each group in the table above.
Example: Convert 111001012 to hex
Solution
11100101 = 1110 0101
Looking at the table above 1110 is E and 0101 is 5
Therefore 111001012 = E516

c. Binary to Decimal
There are many methods of converting Binary to decimals. Let’s use the normal
Mathematics method which involves multiplying each digits by the base in an increasing
power starting with the least significant (rightmost) digit.
Example 1: Convert 111100000000 binary to decimal
Solution
111100000002
=(1×210)+(1×29)+(1×28)+(1×27)+(1×26)+(1×25)+(1×24)+(1×23)+(1×22)+(0×21)1+(0×20)
=1024+512+256+128+0+0+0+0+0+0+0
=1920
Therefore 111100000002 =192010

Example 2: Convert 101110011.11012 to decimal


Solution
101110011.11012
=(1×28)+(0×27)+(1×26)+(1×25)+(1×24)+(0×23)+(0×22)+(1×21)1+(1×20)+(1×2-1)+(1×2-2)+(0×2-
3
+(1×2-4)
=1×256+0×128+1×64+1×32+1×16+0×8+0×4+1×2+1×1+ 1×1/4+1×1/8+0×1/16+1×1/32
=256+0+64+32+16+0+0+2+1+1/4+1/8+0+1/32
=467+0.25+0.125+0+0.03125
=371.40625
Therefore (101110011.1101)2 = (371.40625)10

Decimal Number Conversion

a. Decimal to Binary
To convert from Decimal to binary, divide the given decimal number by 2 and write down the
remainder until the decimal number becomes zero
Example 1: convert 1792 decimal to binary
Solution

21920R

2960 0

2480 0

2240 0

2120 0

260 0

230 0

215 0

27 1

23 1

21 1

0 1

Picking the remainder from bottom to top, we have 11110000000


Therefore 192010 = 111100000002
Example 2: Convert 37110 to binary
Solution
To solve this problem, divide 371 by 2 and put the remainder until it becomes zero

2371R

21851

246 1

223 0

211 1

25 1

22 1

21 0

0 1

Picking the number from bottom to top we have 10111001


371 = 101110011.
Next is to work on the number after the decimal point.
Multiply the decimal number part by 2, write down the whole (W) number part and
continue to multiply the decimal result till it becomes 0 (zero)

2W.40625

20 .8125

21 .625

21 .25

20 .5

1 .0

Picking "W" from top to bottom, we have 01101 which is the same as 1101
Merging the two results we have 101110011.1101
Therefore 371.4062510 = 101110011.11012

b. Decimal to Octal
Example 1: Convert 179210 to base 8
Solution

81792R

8224 0
828 0

83 4

0 3

Therefore 179210 = 34008

c. Decimal to Hexadecimal
Example 1: Convert 1792 base 10 to hex
Solution

161792R

16112 0

167 0

0 7

Therefore 179210 = 700hex


It is important to note that, converting from decimal to hexadecimal a table must be used to
obtain the hexadecimal digit if the remainder is greater than decimal 9.

Relationship between Decimal and Hexadecimal

Dec 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F

Example 2: Convert 4780610 to hex


Solution

1647806R Hex R

162987 14E

16186 11B

1611 10A

0 11B

Picking Hex "R" from bottom to top


4780610 = BABE16
Other fun hexadecimal numbers include AD, BE, FAD, FADE, ADD, BED, BEE, BEAD, DEAF, FEE,
ODD, BOD, DEAD, DEED, BABE, CAFE, FED, FEED, FACE, BAD
Octal Number Conversion

a. Octal to Binary
Converting from octal to binary is as easy as converting from binary to octal. Simply look up
each octal digit to obtain the equivalent group of three binary digits.
Octal to Binary Conversion Table

Bin 000 001 010 011 100 101 110 111

Oct 0 1 2 3 4 5 6 7

Example 1: Convert 3458 to binary


Solution
From the conversion table
3 =011
4 = 100
5 = 101
Therefore 3458 = 0111001012 = 111001012

b. Octal to Hexadecimal
When converting from octal to hexadecimal, it is often easier to first convert the octal
number into binary and then from binary into hexadecimal.
Example 1: Convert 3458 to hexadecimal
Solution
Octal = 3 4 5
Binary = 011 100 101
therefore 3458 = 0111001012 = 111001012
Binary 11100101 = 1110 0101
Then, look up the groups in a table to convert them to hexadecimal digits.

Binary to Hexadecimal Table Conversion

Bin 0000 0001 0010 0011 0100 0101 0110 0111

Hex 0 1 2 3 4 5 6 7

Bin 1000 1001 1010 1011 1100 1101 1110 1111

Hex 8 9 A B C D E F

Binary = 1110 0101


Hexadecimal = E 5 = E5hex Therefore, through a two-step conversion process, octal 345 equals
binary 011100101 equals hexadecimal E5.

Octal to Decimal
Example 1: Convert 3458 to decimal
Solution
Using the usual mathematics method of multiplying each digit by increasing power we have
345 octal = (3 x 82 ) + (4 x 8 1) + (5 x 8 0)
= (3 * 64) + (4 * 8) + (5 * 1)
= 229 decimal

Hexadecimal Number Conversion

a. Hexadecimal to Binary
Converting from hexadecimal to binary is as easy as converting from binary to hexadecimal.
Simply look up each hexadecimal digit to obtain the equivalent group of four binary digits.

Binary to Hexadecimal Table Conversion

Bin 0000 0001 0010 0011 0100 0101 0110 0111

Hex 0 1 2 3 4 5 6 7

Bin 1000 1001 1010 1011 1100 1101 1110 1111

Hex 8 9 A B C D E F

Example 1: convert A2DE16 to binary


From the conversion table above, we have that
A = 1010
2 = 0010
D = 1101
E = 1110
Putting the binary number together we have
A2DE16 = 10100010110111102

b. Hexadecimal to Octal
When converting from hexadecimal to octal, it is often easier to first convert the
hexadecimal number into binary and then from binary into octal.
Example 1: Convert A2DE16 to octal
Solution

Binary to Hexadecimal Table Conversion

Bin 0000 0001 0010 0011 0100 0101 0110 0111

Hex 0 1 2 3 4 5 6 7

Bin 1000 1001 1010 1011 1100 1101 1110 1111

Hex: 8 9 A B C D E F
From the above table
A = 1010
2 = 0010
D = 1101
E = 1110
Therefore A2DE16 = 10100010110111102
Next is to convert 10100010110111102 to Octal number system
Add leading zeros or remove leading zeros to group into sets of three binary digits.
10100010110111102 = 1 010 001 011 011 110 = 001 010 001 011 011 110
Then, look up each group in a table:

Binary to Octal Table Conversion

Bin 000 001 010 011 100 101 110 111

Oct 0 1 2 3 4 5 6 7

001 = 1
010 = 2
001 = 1
011 = 3
011 = 3
110 = 6
It implies that 10100010110111102 = 121336 octal
Therefore, through a two-step conversion process, hexadecimal A2DE equals binary
1010001011011110 equals octal 121336.

c. Hexadecimal to Decimal
Converting hexadecimal to decimal can be performed in the conventional mathematical way,
by showing each digit place as an increasing power of 16. Of course, hexadecimal letter
values need to be converted to decimal values before performing the math.

Relationship between Decimal and Hexadecimal

Deci 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F

Example 1:Convert A2DE hexadecimal to decimal


Solution
A2DEhex
= ((A) * 16 3) + (2 * 16 2) + ((D) * 16 1) + ((E) * 16 0)
= (10 * 16 3) + (2 * 16 2) + (13 * 16 1) + (14 * 16 0)
= (10 * 4096) + (2 * 256) + (13 * 16) + (14 * 1)
= 40960 + 512 + 208 + 14
= 41694 decimal
Therefore, A2DE16 = 4169410

Addition in Number Bases

Step 1: Understanding Place Values

o In binary (base 2), place values are powers of 2 (e.g., 1, 2, 4, 8, 16, etc.).

o In octal (base 8), place values are powers of 8 (e.g., 1, 8, 64, etc.).

o In hexadecimal (base 16), place values are powers of 16 (e.g., 1, 16, 256, etc.).

Step 2: Carrying in Different Bases

o Binary Addition: Similar to decimal addition, but carry occurs when the sum exceeds
the base (e.g., 1 + 1 = 10 in binary).

o Example:

 Binary: 1011 + 1101 = 11000 (Carry 1 in base 2).

 Decimal: 23 + 56 = 79 (Carry 1 in base 10).

Examples

 Convert numbers from one base to another and solve the addition problem in that
base.

o Example 1: Add 1011 (binary) + 1101 (binary).

o Example 2: Add 25 (decimal) + 37 (decimal).

o Example 3: Add 17 (hex) + 2A (hex).

Subtraction in Number Bases

Borrowing in Different Bases

o Borrowing works similarly to decimal, but based on the value of the base.

o In binary (base 2), borrowing occurs when subtracting 1 from 0, so you take away
from the next higher place value.

o In hexadecimal (base 16), if you need to subtract a larger digit from a smaller one,
you borrow from the next place value (e.g., 1F - 3 = 1C).

Examples

o Example 1: Subtract 1101 (binary) – 1011 (binary).

o Example 2: Subtract 58 (decimal) – 23 (decimal).

o Example 3: Subtract 2F (hex) – 14 (hex).


Examples for Practice:

1. Binary Addition:
11011+10111=?

2. Decimal Addition:
56+78=?

3. Hexadecimal Addition:
A616+3B16=?

4. Binary Subtraction:
101101−11011=?

5. Hexadecimal Subtraction:
2F16−1B16=?
Data Representation

Definition of Data Representation


Data representation refers to the methods used to internally represent information stored in
a computer. Computers store a lot of different types of information which include: Numbers,
Text, Graphics, and Sound. At least all seem different to us. However, all types of information
stored in a computer are stored internally in the same format: a sequence of 0’s and 1’s.

Methods of Data Representation


1. BITS
BITS stands for Binary digITS. The smallest unit of data on a binary computer
is a single bit. It consist 1(s) and 0(s). Byte

2. BCD
BCD is an acronym that stands for Binary Coded Decimal. BCD is a method of using binary
digits to represent decimal digits 0 through 9. A decimal digit is represented by four binary
digits as shown below.

BCD Table
DecimalBCD
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001

Example 1: Convert 4910 to BCD


Solution
From the table
4 = 0100
9 = 1001
Therefore 49 10 = 01001001BCD
3. EBCDIC
EBCDIC represents Extended Binary Coded Decimal Interchange Code. It is
eight bits or one byte wide. Each byte consists of two nibbles. The first four
define the class of character, while the second nibble defines the specific
character inside that class. A coding scheme developed by IBM for use with
its computers as a standard method of assigning binary (numeric) values to
alphabetic, numeric, punctuation, and transmission-control characters.

4. ASCII
ASCII stands for American Standard Code for Information Interchange. This
coding scheme assigns numeric values to letters, numbers, punctuation
marks, and certain other characters.

ASCII Table
6. UNICODE

Unicode provides a unique number for every character, no matter the

platform, program and language.

Question

1a. What are the advantages of High Level Language over Low level Language
1b. In one sentence differentiate between compiler and Interpreter
2a. All Array are Variables but not all variables are Array. Explain?
2b. Write a simple basic program that will sum array with four elements

2a. Convert 7448 to hexadecimal

3b. Solve for the following: i. 110101 + 10011 ii. 5238 - 3458

Common questions

Powered by AI

The binary to hexadecimal conversion involves grouping binary digits into sets of four, starting from the least significant digit on the right. Each group of four is then matched to a hexadecimal digit using a predefined conversion table. For example, to convert 1110 0101 from binary to hex, the group 1110 is E and 0101 is 5, thus 111001012 is E516 .

In binary addition, the carry occurs when the sum exceeds the base, which is 2, similar to decimal where the base is 10. For instance, in binary, adding 1011 + 1101 results in 11000; the rightmost bits 1 + 1 = 10 (binary, carrying over 1). In decimal, 23 + 56 equals 79; when the sum of 3 + 6 is 9, no carry, but carry 1 from the tens column when 2 + 5 + 1 (carry) = 8 .

To convert a decimal number to octal, divide the decimal number by 8, recording the remainder until the quotient is zero. The octal number is formed by the remainders read in reverse order. A conversion table aids in matching decimal numbers to their octal equivalents, although for direct sequential conversion from calculated remainders, such a table is less critical than in non-sequential digit systems like hexadecimal .

Elements in a BASIC array can be initialized at the time of declaration or later during the program's runtime. If initialized at declaration, initial values are directly assigned to the array elements, while if initialized later, elements start with default values (usually zero) and are assigned new values individually by specifying each index. For example, numbers(0) = 10 initializes the first element of the 'numbers' array to 10 after declaration .

The mathematical method for converting binary to decimal involves multiplying each binary digit by 2 raised to the power of its position, starting with the least significant digit at power zero. This method relies on the weight or place value of each digit increasing by powers of the base 2, reflecting the binary number system's positional nature .

To declare a one-dimensional array in BASIC, you use the DIM statement followed by the array name and the number of elements it can store. The syntax is: DIM arrayName (size), where 'arrayName' is the name of the array, and 'size' is the number of elements, with indices starting from 0 up to size-1. For example, DIM numbers(5) declares an array of 6 elements, from indices 0 to 5 .

Standardized coding schemes like ASCII and Unicode assign numeric values to characters to represent text and symbols in a form computers can process. ASCII uses a 7-bit code for English characters, while Unicode provides a universal character set for multilingual text supporting many languages and symbols, allowing consistent data representation across platforms .

BCD uses binary digits to represent decimal digits separately, providing a simple way to encode numbers in a binary form, while EBCDIC, developed by IBM, uses 8-bit bytes to code alphanumeric data, allowing for more complex and varied data representation. BCD is efficient for pure numeric data, whereas EBCDIC includes extended coding for diverse data types .

Converting hexadecimal to octal typically involves an intermediary step through binary for simplicity. First, convert each hex digit to a 4-bit binary equivalent, then regroup these bits into groups of three for conversion to octal. This intermediary binary conversion simplifies the direct translation between hexadecimal and octal due to the different bases .

Accessing and modifying array elements in BASIC involves using their indices, which start from 0. For example, numbers(0) accesses the first element. To modify, you assign a new value, e.g., numbers(1) = 100 changes the second element to 100. Loops such as FOR...NEXT can iterate over arrays, simplifying access and modifications. For instance, FOR i = 0 TO 4 followed by PRINT numbers(i) prints each element .

You might also like