Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Wednesday, March 9, 2022

Array in QBASIC for class 9 and 10

 

Array in QBASIC for class 9 and 10

 





Introduction:

Arrays are an important concept in programming that allow us to store and manipulate multiple values of the same data type. QBASIC, a popular programming language taught in classes 9 and 10, also supports arrays. Arrays are useful in solving many real-world problems, such as calculating averages, storing student marks, and more. In this blog post, we will discuss how array in qbasic for class 9 and 10 useful for school and SEE students.


What is an Array?

An array is a structure with the help of which a programmer can refer to and perform operations on a group of similar data items. A standard structure for storing data in any programming language is an array. Other variables can hold single entities, such as a number, date, or string, arrays can hold sets of related data. An array has a name and the values stored in it can be accessed by an index number. For example, you can use the variable N$ to store a name. But, if you have 10 persons’ name to store, you most use only array variable.


An array is a collection of similar types of variables under the same name.

An array declared should indicate three things:
  • Type of value to be stored in each element.
  • The name of the array
  • Total number of elements in an array.

 

In QBASIC we can declare the variables two ways: Simple variables and Array variables.

 

Declaration of Arrays


An Array should be declared before the program. DIM statement is used to declare the array followed by name of the array and the maximum number of elements it can hold in parentheses.

 Syntax:

DIM [SHARED] arrayname(subscript) [AS TYPE] [arrayname(subscript) [AS TYPE]]

Where,

SHARED: SHARED keyword is used to share the value of the variable with the procedures (function and sub).

arrayname: It is the name of the array variable

Subscript: It specified the size of the array.

AS TYPE: It defines the type of array variable

 

Example:

DIM age(10)

DIM name$(10)

Note: In above example Age and Nam$ are the name of array variables that holds 10 values. Age (0) and Nam$ (0) are the first person’s age and name, Age (1) and Nam$ (1) are the second person’s age and name, and so forth.

 Array variable can also be declared according to the explicit variable declaration.

DIM Age (10) AS INTEGER

DIM Nam (10) AS STRING

Note: In above case of fixed length string, array variable can be declared according the given structure.

DIM Nam (10) AS STRING*15

Note: In above array variable Name can store 10 different names having the maximum character length of 15 each.

 

OPTION BASE Statement

The OPTION BASE statement defines the lower bound of an array. The smallest subscripted value of an array is lower bound.

Syntax: OPTION BASE n

Where,

n, is a number whose value is either 0 or 1.

Example:

OPTION BASE 1

Note: The OPTION BASE statement must be declared before defining the array variable in the program otherwise the program display the ‘Duplicate Definition’ error in the program.

 

Types of Arrays:

The Array in QBASIC defined according to the number of data items that it holds. Following are the different types of Arrays:

  • Single or One Dimension Array
  • Multi or Two Dimension Array

 

Single / One dimensional Array

An array whose elements are specified by a single subscript is known as one-dimensional array.

Syntax: DIM <arrayname>(subscript)

Where,

arrayname: It means name of the array variable.

subscript: It means the size of the array.

Example:

DIM marks(5)


Numerical Array:

In this type of array numerical data are stored in array.

Example 3:

Program to read five numbers and display them.

REM program to read the data and display them

DIM a(5)

FOR i = 0 TO 4

READ a(i)

PRINT a(i)

NEXT i

DATA 19,17,11,3,-5

END

The data of above program are stores in variable in following ways:

Variable name

a

a(0)

a(1)

a(2)

a(3)

Data stores

19

17

11

3

 

 

Character Arrays:

So far arrays of integers are used. Array of characters or string can also be used.

Declaring character array:

   DIM name$(5)

Example 4: Program to read five names and display them:

            CLS

REM program to read five names and display them

DIM name$(4)

FOR i = 0 TO 4

READ name$(i)

PRINT name$(i)

NEXT i

DATA Naresh, Amir, Ranu, Mohan, Sohan

END

The data of above program are stores in variable in following ways:

Variable name

Name$

na(0)

na(1)

na(2)

na(3)

Data stores

Naresh

Amir

Ranu

Mohan

 

 

1  Program to input any five numbers and display them in reverse order.

CLS

DIM a(4)

FOR i = 0 TO 4

INPUT "enter number="; a(i)

NEXT i

PRINT "Number in original sequence"

FOR i = 0 TO 4

PRINT a(i)

NEXT i

PRINT "number in reverse order"

FOR i = 4 TO 0 STEP -1

PRINT a(i)

NEXT i

END

 

2) Program to input any five number and print their sum.

CLS

DIM n(4)

FOR i = 0 TO 4

INPUT "enter number="; n(x)

NEXT i

s = 0

FOR x = 0 TO 4

s = s + n(i)

NEXT x

PRINT "Sum="; s

END

 

3) Program to input any five numbers display the greatest number.

CLS

DIM n(4)

FOR x = 0 TO 4

INPUT "enter number="; n(x)

NEXT x

max = n(1)

FOR x = 1 TO 4

IF n(x) > max THEN max = n(x)

NEXT x

PRINT "Greatest number="; max

END

 

4) Program to read five names and display them:

            CLS

REM program to read five names and display them

DIM name$(4)

FOR i = 0 TO 4

READ name$(i)

PRINT name$(i)

NEXT i

DATA Naresh, Amir, Ranu, Mohan, Sohan

END

 

5.  To input any five students names and  percentage and print them on screen.

CLS

DIM name$(5), per(5)

FOR p = 1 TO 5

INPUT "enter name and marks"; name$(i), per(i)

NEXT p

PRINT :

PRINT "Name", "percentage"

FOR p = 1 TO 5

PRINT name$(i), per(i)

NEXT p

END

 

Bubble Sort:

It is quicker and easier method to sort data when the numbers of items are less. The Bubble sorting involves the following steps.

  • Make compare the first element of an array with rest of the elements. If it is larger than other, swap the element position.
  • Compare second element of an array with all elements having index greater than 1.
  • Compare next element with remaining elements. Continue this process until you reach to the end of array. The last sequence is the sorted sequence of elements.

 

Some solved problems using Bubble Sort

Number Sorting:

1) Program to input any 10  numbers and display them by sorting in ascending order.

CLS 1

DIM num(10)

FOR i = 1 TO 10

INPUT num(i)

NEXT i

FOR j = 1 TO 10

FOR k = 1 TO 10 - j

IF num(k) > num(k + 1) THEN SWAP num(k), num(k + 1)

NEXT k

NEXT j

PRINT "*********Sorted Data***********"

FOR z = 1 TO 10

PRINT num(z)

NEXT z

END

 

2) To input any ten number and display them by sorting in descending order.

CLS 1

DIM num(10)

FOR i = 1 TO 10

INPUT num(i)

NEXT i

FOR j = 1 TO 10

FOR k = 1 TO 10 - j

IF num(k) < num(k + 1) THEN SWAP num(k), num(k + 1)

NEXT k

NEXT j

PRINT "======Sorted Data======"

FOR z = 1 TO 10

PRINT num(z)

NEXT z

END

 

String Sorting:

It is possible to sort a string with the same method as we have done for number sorting.  String sorting is possible comparing by first character of the string with first character of another string. If the first characters of the both string s are same, then the second character will be compared.

 

1) To input any five names and print them in ascending order by sorting.

CLS

DIM n(5) AS STRING

FOR i = 1 TO 5

INPUT n(i)

NEXT

FOR i = 1 TO 5

FOR j = 1 TO 5 - 1

IF n(j) > n(j + 1) THEN

t$ = n(j)

n(j) = n(j + 1)

n(j + 1) = t$

END IF

NEXT j

NEXT i

CLS

PRINT “=====Sorted Name=====”

FOR x = 1 TO 5

PRINT n(x)

NEXT

END

 

2) To input any five names and print them in descending order by sorting.

CLS

DIM n(5) AS STRING

FOR i = 1 TO 5

INPUT n(i)

NEXT

FOR i = 1 TO 5

FOR j = 1 TO 5 - 1

IF n(j) < n(j + 1) THEN

t$ = n(j)

n(j) = n(j + 1)

n(j + 1) = t$

END IF

NEXT j

NEXT i

CLS

FOR x = 1 TO 5

PRINT n(x)

NEXT

END

 

You may also Read:

Arguments in QBasic


String Sorting:

It is possible to sort a string with the same method as we have done for number sorting.  String sorting is possible comparing by first character of the string with first character of another string. If the first characters of the both strings are similar, then the second character will be compared.

 

1) To input any 5 names and display them in ascending order by sorting.


CLS

DIM n(5) AS STRING

FOR i = 1 TO 5

INPUT n(i)

NEXT

FOR i = 1 TO 5

FOR j = 1 TO 5 - 1

IF n(j) > n(j + 1) THEN

t$ = n(j)

n(j) = n(j + 1)

n(j + 1) = t$

END IF

NEXT j

NEXT i

CLS

PRINT “=====Sorted Name=====”

FOR x = 1 TO 5

PRINT n(x)

NEXT

END

 

2) To input any 5 names and display them in descending order by sorting.

         CLS

DIM n(5) AS STRING

FOR i = 1 TO 5

INPUT n(i)

NEXT

FOR i = 1 TO 5

FOR j = 1 TO 5 - 1

IF n(j) < n(j + 1) THEN

t$ = n(j)

n(j) = n(j + 1)

n(j + 1) = t$

END IF

NEXT j

NEXT i

CLS

FOR x = 1 TO 5

PRINT n(x)

NEXT

END

 

Searching using Array:


The method of finding a data element in the array is called searching. Different search techniques are used for finding the data elements in the array. The sequential search is mostly used for searching the data items. In a sequential search data element is compared with the value being searched for and stops when the match is found or the end of the array is encountered.


Example:

CLS
REM program to search the input name and display with address.
DIM n(5) AS STRING
DIM a(5) AS STRING
FOR i = 1 TO 5
READ n(i), a(i)
NEXT
flag = 0
CLS
INPUT "Enter name which you want to search"; s$
PRINT "Name", "Address"
FOR i = 1 TO 5
IF UCASE$(s$) = UCASE$(n(i)) THEN
flag = 1
PRINT n(i), a(i)
END IF
NEXT
IF flag = 0 THEN PRINT " Try again, record not found"
DATA Ram,Dhangadhi, Radha, Taranagar, Sunil, Hasanpur
DATA OM, Tikapur, Sumit, Lamki
END


Conclusion:

In conclusion, understanding arrays is an essential skill for any programmer. QBASIC, being a popular programming language, supports arrays, and understanding how to use them can help in solving many real-world problems. With the knowledge of how to declare, initialize, access, and iterate over arrays in QBASIC, students in classes 9 and 10 can write more efficient and powerful programs that can handle large amounts of data. By mastering arrays, students can take their programming skills to the next level and create more complex and sophisticated programs.


You can also Read:




 

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages