The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by sanjeev.khattri.dubai, 2019-05-05 05:55:53

VISUAL BASIC

MIDDLE SCHOOL

WORKSHEET 4.4

Write a code in visual basic which will print the following number patterns.
1
22
333
4444
55555

54321
5432
543
54
5

1
23
456
7 8 9 10

#5. ARRAYS

An array is a group of variables that have the same name and data type and are related in some
way. The most commonly used arrays are one-dimensional and two-dimensional.
Arrays has following process:
1 traversing
2 reversing
3 searching. (Linear search)
4 smallest element
5 largest element
6 sorting. ( Linear sort)

Programmers use arrays to store related data in the internal memory of the computer.
You refer to each variable in an array by its name and the variable’s subscript

Names of the variables in a one-dimensional array named states
Examples of declaring an array

Dim cities(3) As String
In most cases, you use an assignment statement to enter data into an existing array
Syntax: arrayname(subscript) = value

Examples

– cities(0) = “Madrid”
– cities(1) = “Paris”

– cities(2) = “Rome”

To refer to a variable in an array, use the array’s name followed by the variable’s subscript
List1.Additem(cities(1)) will print Paris

List1.Additem(cities(2)) will print Rome

We can also have arrays of numbers like
Dim marks[5] as Integer

Dim no[10] as integer
Here 10 variables by the names
no[0]
no[1]
no[2]
…..
no[9]
Note here numbers are starting from 0 to 9
As the index/position of arrays elements start from 0 to total numbers -1
Example
Write a program in visual basic which will store 10 integer numbers and prints their sum
In case of arrays or in this specific example we need to create 10 text boxes “text boxes” but we
will not use 10 different boxes we will copy and paste the 1st textbox and paste it 9 more times to
get 10 text boxes.

We will put a Label and change the caption to “ Enter Array elements”
We will draw a text box on form as shown in figure.

Click on text1 box and select edit copy
Select edit paste
The following message will appear
“You already have a control named “Text1”. Do you want to make it a control array?”
Select “Yes”

Drag the pasted textbox to required position and follow the steps for 9 remaining numbers

Now these text boxes are named as text1(0) text1(1) text1(2) ………………….text1(9)
Dim no[10] as integer
Dim s as integer
s=0
for I=0 to 9

no[i] = val (text1 (i))
next
for i=0 to 9

s=s+no[I]
next
list1.additem ("sum of array is " & s)
Searching technique LINEAR SEARCH
1 INPUT 10 NUMBERS IN ARRAY
2 INPUT A NUMBER TO BE SEARCHED
3 COMPARE THE NUMBER WITH ith NUMBER IN ARRAY
4 PRINT IF THE NUMBER IS FOUND
5 MOVE TO NEXT NUMBER IF NOT FOUND
6 CONTINUE STEP 3 to 5 TILL LAST NUMBER IS REACHED.

Dim no[10] as integer

Dim search.found as integer
for I=0 to 9

no[i] = val (text1 (i)) // here we are using text1(i) as the value of I will go from 0 to 9 so the
// text boxes text1(0), text1(1), text1(2)…………….text1(9) will be used.

next
search=val(text2)
for i=0 to 9

if search=no[I]
found=1

endif
Next
if found=1 then

List1.Additem(search & "found")
else

List1.Additem(search & " not found")
endif

Worksheet 5.1

Q1
Input 5 numbers in an array and print sum of those which are divisible by 7.
Example:
Input: 1,5,7,21,60
Output: should be 7+21 =28
Or
“sum of numbers divisible by 7 is 28”

Q2
Input 10 numbers in an array and print only those which are more then 5

Q3

Input 4 numbers in array and print sum of even numbers and odd numbers separately.
Example:
Input : Enter four numbers: 3,4,7,2
Output: Sum of even numbers : 6

Sum of odd numbers : 10

Q4
Input 5 numbers in an array and print sum of only positive numbers.
Example :
Input: Enter 5 numbers: -1 , 12, - 10 2
Output: Sum of positive numbers is : 14

Worksheet 5.2

Q1 Input names and percentage of 5 students in two different array. Print their names along
with their grades on following condition.
Percentage Grades

Between 80 to 100 A
Between 60 to 79 B
Between 40 to 59 C

Example:
Input: Raman 89

Suman 56
Rajan 45
Rohit 61
Suresh 67

Output Raman A
Suman C
Rajan C
Rohit B
Suresh B

Q2 Input country name and capital names of 5 countries in two arrays and Print the country name
and capital name of 1st and last country from the list

Worksheet 5.3
Question1 Input 4 numbers in an array and print the smallest number not.

Example:
Input: 10,34,65,12
Output: Smallest number is 10

Question 2 Input 5 numbers in an array and print the largest number.
Example:
Input 21,3,14,5,65

Output: Largest number is 65

Worksheet 5.4

Question1 Input weights of 5 persons in an array and calculate the average of persons who
has the max. and min weights
Input : 34,45,65,56,76,78,98,67,43,45

Output: Maximum weight is 98
Minimum weight is 34

Average of max and min weights is 66

Question 2
Input names and ages of 5 persons in two different arrays, print the name of the youngest
person along with age.

Example:
Input :

Ram 21
Suman 32
Rohan 22
Roshan 34
Rajesh 12

Output:
The youngest person is Rajesh and his age is 12

Revision : WORKSHEET

Q1 Input weights of 5 persons in an array and calculate the average of persons who has the max.
and min weights
Input : 34,45,65,56,76,78,98,67,43,45
Output: Maximum weight is 98
Minimum weight is 34
Average of max and min weights is 66

Q2 Input the names and ages of 5 persons in two different arrays, Print the name along with age
of the youngest person.

Q3 Input 5 numbers in an array and print sum of those which are divisible by 7.
Example:
Input: 1,5,7,21,60
Output: should be 7+21 =28
Or “sum of numbers divisible by 7 is 28”

Q4 Input 10 numbers in an array and print only those which are more then 5
Q5. Input 4 numbers in array and print sum of even numbers and odd numbers separately.

Example:
Input : Enter four numbers: 3,4,7,2
Output: Sum of even numbers : 6 Sum of odd
numbers : 10

Q6. Input 5 numbers in an array and print sum of only positive numbers.
Example :
Input: Enter 5 numbers: -1 , 12, - 10 2
Output: Sum of positive numbers : 14

Q7 Input names and percentage of 5 students in two different array. Print their names along
with their grades on following condition.
Percentage Grades
Between 80 to 100 A
Between 60 to 79 B
Between 40 to 59 C Example:
Input: Raman 89
Suman 56
Rajan 45
Rohit 61
Suresh 67
Output Raman A
Suman C
Rajan C
Rohit B
Suresh B

Q8 Input country name and capital names of 5 countries in two arrays and Print the country
name and capital name of 1st and last country from the List

Q9 Input 4 numbers in an array and print the smallest number
Example:
Input: 10,34,65,12
Output: Smallest number is 10

Q10 Input 5 numbers in an array and print the largest number.
Example:
Input 21,3,14,5,65
Output: Largest number is 65

#6 STRINGS

In VB, you can use strings as array of characters, however, more common
practice is to use the String keyword to declare a string variable.

Dim fname, lname, fullname, greetings As String
fname = "Rowan"
lname = "Atkinson"
fullname = fname + " " + lname
List1.Additem("Full Name: ", fullname)

Comparing Strings

Dim str1, str2 As String
str1 = Text1
str2 = Text2

If str1 = str2 Then
List1.AddItem (str1 + " and " + str2 + " are equal.")

Else
List1.AddItem (str1 + " and " + str2 + " are not equal.")

End If
End Sub

Checking the chronological order of strings(which comes before or
after in alphabetical order, the order of dictionary)

Dim str1, str2 As String
str1 = Text1
str2 = Text2

If str1 > str2 Then
List1.AddItem (str1 + " comes after " + str2 + " in dictionary")

Else
List1.AddItem (str1 + " comes before " + str2 + " in dictionary")

End If

Checking each character of a string

Example: Input a string and check how many times the letter

appears in it.

Dim word, alpha As String
Dim count, length As Integer

count = 0
word = Text1
length = Len(word)
For i = 1 To length

alpha = Mid(word, i, 1)
If alpha = "a" Then
count = count + 1
End If
Next i
List1.AddItem ("There are " & count & " a's in " & word)

In the above program we used a string function mid(word,i,1) Justification
This function picks 1 letter from the ith position from word
Look at the dry run table of the above loop Picks 1 letter from 1st
Assuming the word = “computer” position
I alpha=mid(word,i,1)
1c Picks 1 letter from 2nd
position
2o
Picks 1 letter from 3rd
3m position

4p Picks 1 letter from 4th
position
5u
Picks 1 letter from 5th
6t position

(=7 e Picks 1 letter from 6th
position
8r
Picks 1 letter from 7th
position

Picks 1 letter from 8th
position

alpha=mid(word, position,count)

How many letters

From which position

We have two more functions in string
alpha=Left(word,3)  com (prints 3 letters from left side)
alpha=right(word,3)  com (prints 3 letters from right side)

WORKSHEET 6.1

Question1 Input a word and count and print how many vowels it has
Question2 Input a word and count how many consonants it has.
Question3 Input a word and count how many spaces it has.
Question4 Input a sentence and count how many words it has

ENCRYPTION DECRYPTION

Encryption is a way of converting a word/sentence in coded message following a certain rule.
Decryption is a way of converting the coded message to normal form following a certain rule.

Every characters or symbols on keyboard has a code called asci code. Example

To convert a character to asci code
Dim code as Integer

code=asc(alpha) //alpha stores a alphabet

To convert a asci code to character(alphabet)

alpha=chr(code) //alpha stores a alphabet

Example:
Input a word and encrypt it by changing the alphabet position by two steps ahead in dictionary
order. Like ac

ef
abc  cef

In this example we have to be careful about the letter ‘y’ as there is no letter after ‘z’ so it should
become ‘a’ (following cyclic order)

Dim word, alpha, ans As String

Dim length, code As Integer

word = Text1

length = Len(word)

for i = 1 To length

alpha = mid(word, i, 1)

code = Asc(alpha)

If code = 121 Then //if code is for ‘y’

code = 97 // make it ‘a’

elseif code = 122 then // if code is for ‘z’

code = 98 // make it ‘b’

else

code = code + 2 //change all other alphabet code to next 2 letters

end If

alpha = chr(code)

ans = ans + alpha

next i

List1.AddItem ("Coded word is " & ans)

Write a program which will decode the above word

The solution will be just opposite of above code now we have to subtract 2 to every letters

asci code, we need to keep track of ‘a’ and ‘b’ so ‘a’ will become ‘y’ and ‘b’ will become ‘z’

Dim word, alpha, ans As String

Dim length, code As Integer

word = Text1

length = Len(word)

for i = 1 To length

alpha = mid(word, i, 1)

code = Asc(alpha)

If code = 97 Then //if code is for ‘a’

code = 121 // make it ‘y’

elseif code = 98 then // if code is for ‘b’

code = 122 // make it ‘z’

else

code = code - 2 //change all other alphabet code to previous 2 letters

end If

alpha = chr(code)

ans = ans + alpha

next i

List1.AddItem ("Coded word is " & ans)

WORKSHEET 6.1

Question1 Input a word and encrypt it by changing each alphabet to next 3rd alphabet in
Question2 alphabetical order.
Question3 Input the coded word and decrypt it by changing each alphabet to previous 3rd
Question4 alphabet in alphabetical order.
Input a word and encrypt it by changing each alphabet to next 5th alphabet in
alphabetical order.
Input the coded word and decrypt it by changing each alphabet to previous 5th
alphabet in alphabetical order.


Click to View FlipBook Version