Saturday, August 29, 2020

String Functions in Visual Basic:

String functions:

LCase() function:

Return a string that has been passed to it as an augment convert it to lowercase string

LCase(String)

e.g LCase(“PRATIK”) returns “pratik”

UCase() function:

Return a string that have been passed to it as an argument converted to uppercase string

UCase(string)

e.g UCase(“pratik”) returns “PRATIK”

Len( string):

Gives the length of the string which is passed to it as an argument.

e.g Len(“Pratik”) gives 6

LTRIM( string)

Return the string without any leading spaces

e.g Print LTRIM(“  BSC  ”) gives “BSC  ”

RTRIM(string)

Return the string without any trailing spaces 

e.g Print RTRIM(“  BSC  ”) gives “  BSC”

TRIM(string)

Return string without any leading or trailing spaces.

e.g Print TRIM(“  BSC  ”) gives “BSC”

LEFT(String, length)

Return the specified number of characters from the left side of this string.

e.g Print LEFT(“Bajaj College of Science”, 5) gives “Bajaj”

RIGHT(String, length)

Return the specified number of character from the right side of the screen

e.g Print RIGHT (“Bajaj College of Science”, 7) gives “Science”

MID(String, Start,Length)

Return the specified number of characters from the specified position of the string

e.g print MID (“Bajaj College of Science”, 7, 7) gives “College”

CHR(charcode)

Return the character associated with the specified ASCII number.

e.g Print CHR(65) return “A”

ASC(String)

Return the ASCII number for the first letter in the string.

e.g Print ASC(“A”) return 65 

InStr() Function

It is used to find the occurrence of the one string inside other string along with the position of it.

Return a Long value specifying the position of the first occurrence of the one string in another.

Syntax:

InStr(String1, String2)

e,g print Instr(“Pratik” , “a”)  return 2 which is the position of string “a” in the string  Pratik.

StrComp() Function:

Return a(Variant) integer indicating the result of the string comparison.

e.g  StrComp(String1, String2)

Function returns an integer value depending on the result of the comparison as follows

String1 is less than string2

-1

String 1 is equal to string 2

0

String 1 greater than string 2

1

Sting 1 or string to is Null

Null

While comparing the two string the ASCII values of the character in it are compared

e.g      StrComp(“Pratik”, “pratik”) gives  -1

StrComp(“Pratik”, “Pratik”) gives  0

StrComp(“Prashant”, “Pratik”) gives 1  


Special Functions in Visual Basic

InputBox() Function: It displays a prompt in a dialogue box, wait for the user to input text or click button,  and returns a string containing the contents of the text box

Syntax:

Inputbox( prompt[,  title] [,  default] [,  xpos] [, ypos], [helpfile, context])

The input box function syntax has this named argument:

Part

 Description 

Prompt

String expression displayed as the message in dialogue box

Title

Optional.  String expression displayed in the title bar of the dialogue box.  If you omit  title,  the application name is place in title bar

Default

Optional.  String expression displayed in the textbox as the default response.  If no other input is provided

Xpos

The horizontal distance of the left edge of the dialogue box from the left edge of the screen

Ypos

Optional.The vertical distance of the upper edge of the dialogue box from the top of the screen. 

Helpfile

Optional. String expression that identifies the help file to use to provide context sensitive help for the dialogue box. 

Context

Optional.  Expression that is the help contact number assigned to the appropriate help topic by the help author. If context is provided  , help file must also be provided

Private Sub Command1_Click()

Dim x as integer

x = Inputbox(“ Enter in number”,” Number input”, “10”,20,30)

Print x

End sub

When a button is clicked,  the input box is displayed as follows

Inputbox

Msgbox() Function: 

Displays a message in dialogue box, Wait for user to click a button, and return an integer indicating which button the user click.

Syntax:

Msgbox(prompt, [ buttons], [ title] [, helpfile, context])

The details of the augments passed to the Msgbox function are as follows.

Part

 Description

Prompt

Required. String expression displayed as the message in the dialogue box

Buttons

Optional.  Numeric expression that is the sum of values specifying the number and types buttons to display,  the Icon style to use, the identify of the default button,  and the modality of the message box. If omitted the default value of the button 0.

Title

Optional.  String expression displayed in the title bar of the dialogue box.  If you omit title the application name is place in the title bar.

Helpfile

Optional.  String expression that identifies a help file to use to provide context sensitive help for the dialogue box.  If help file is provided context must also be provided

Context

Optional.  numeric expression that is to help contact number assigned to the appropriate help topic by the help author.  if context is provided,  helpfile must also be provided 

The Msgbox() function return a value depending upon the button clicked,

Constant

 Value

 Description

VbOK

1

Ok

VbCancel

2

Cancel

VbAbort

3

Abort

VbRetry

4

Retry

VbIgnore

5

Ignore

VbYes

6

Yes

VbNo

7

No

The message box is always displayed at the centre of the screen.

Private Sub Command1_Click()

Dim x as integer

x = Msgbox(“ Are you sure”,  vbYesNoCancel+vbQuestion, ” confirmation”)

End sub

Msgbox


Functions in Visual Basic:

The function is capable of returning a value to the calling program what is procedure can't do

Steps to create a function:

Tool -> Add Procedure -> Name ->Type(Function) -> Scope (Private/Public)

Syntax for the function definition is:

Public Function  Function_name()  as return data_type

……….

Statements

………..

End function

Public Function Fact (num as integer) As Long

Dim f, i as Long

f=1

For i = 1 to num

f=f*i

Next i

Fact=f

End Function

Private Sub Command1_Click()

Dim x as Long

x=Fact (5)

Msgbox  x

End Sub

A function can return a value in the following format

function_name =return_value

Calling a function:

Usually, you can call a function procedure you have written yourself the same way call intrinsic Visual Basic function like Abs that is by using its name in an expression

MsgboxFact(4)

It is also possible to call a function just like you would call a sub procedure. Following statement both call the same function.

Call Fact(4)

Or 

Fact 6

When you call a function this way, Visual Basic throw away the return value


Friday, August 28, 2020

Queues, Deques and Priority Queues:

Queue is a linear list of elements in which the deletion can take place only at one end, called the FRONT and the insertion can takes place only at the other end called the REAR.  The term FRONT and REAR are used in describing a linear list only when it is implemented as a queue.

Queues are also called as First in First out (FIFO) list, since the first element in a queue will be the first element out of the queue.  Another word, the order in which elements entered a queue is the order in which they leave.

Queues abound in everyday life. The automobile waiting to pass through an interaction for a queue, in which the first car in line is the first car through. The people waiting in the line at a bank from a queue, where the first person in line is the first person to be waited on and so on.

An important example of queue in computer science occurs in a time sharing system, in which the program with the same priority from a queue while waiting to be executed.

Fig. Queues

Representation of queues:

Queue may be represented in a computer in various ways usually by means of one way list or linear array.  Each of our queues will be maintained by a linear array QUEUE and to pointer variable: FRONT, contain the location of the front element of the queue, and REAR, containing the location of the rear element of the queue. 

The condition FRONT=NULL will indicate that the queue is empty.

The element will be deleted from the queue and the way new element will be added to the queue .Observe that whenever an element is deleted from the queue, the value of FRONT is incremented by 1.  This can be implemented by the assignment.

 

FRONT: =FRONT+1

Similarly, whenever an element is added to the queue,  the value of rear is incremented by 1.

REAR: =REAR+1

This means that after n insertion, the rear element of the queue will occupy QUEUE [N], or, another word;  eventually the Q will occupy the last part of the array.

Suppose we want to insert an element ITEM into a queue at the time the queue does occupy in last part of the array, i.e when REAR=N.  One way to do this is to simply move the entire Q to the beginning of the array, by changing front and rear accordingly, and then inserting item as above. This procedure may be very expensive the procedure we adopt is to assume that the Q is circular. That is Q[1] comes after Q[N] in the array. With this assumption, we insert item into the queue by assigning ITEM to QUEUE [1].  Specifically, instead of increasing REAR to N+1, we reset REAR=1 and then assign


                QUEUE[REAR]:=ITEM


Similarly, if front= N and an element of queue is deleted, we reset FRONT=1 instead of increasing FRONT to N+1.

Suppose that our queue contain only one element i.e 

            

                FRONT=REAR!=NULL

 And suppose that the element is deleted.  then we assign

FRONT=NULL and REAR= NULL  to indicate that the queue is empty.



Algorithm to insert an Item into a Queue:

QINSERT (QUEUE, N, FRONT, REAR, ITEM)

This procedure inserts an element ITEM into a queue.

1.  [Queue already filled?]

     If FRONT=1 and REAR=N, or FRONT=REAR+1, then:

            Write: OVERFLOW, and Return.

2. [Find new value of REAR]

       If FRONT: =NULL, then:[Queue initially empty]

            Set FRONT: =1 and REAR: =1

Else if REAR: =N, then:

            Set REAR: =1

Else

            Set REAR: = REAR+1.

[End of if structure]

3. Set QUEUE[REAR]:=ITEM [This inserts new element]

4.  Return.

Algorithm to delete an Item from a Queue:

QDELETE (QUEUE, N, FRONT, REAR, ITEM)

This procedure deletes an element ITEM from a queue.

1.  [Queue already empty?]

If FRONT=NULL, then:

            Write: UNDERFLOW, and Return.

2. Set ITEM := QUEUE[FRONT]

3.[Find new value of FRONT]

       If FRONT: =REAR, then:[Queue has only one element]

            Set FRONT: =NULL and REAR: =NULL

Else if FRONT: =N, then:

            Set FRONT: =1

Else

            Set FRONT: = FRONT+1.

[End of if structure]

4. Return


DEQUES:

A deque is a linear list in which the elements can be added or removed at either end but not in the middle.  The term deque is a contraction of the name double ended queue.

Deque  is maintained by a circular array DEQUE  with pointers left and right,  which points to the two ends of the deque.  We assume that the elements extend from the left end to the right end in the array.  The term circular comes from the fact that we assume that DEQUE[1]  comes after DEQUE[N]  in the array.  Picture shows the deque with N=8  memory location.  The condition LEFT=NULL will be used to indicate that deque is empty.

There are two variations of the deque- namely, an input restricted deque and output restricted deque - which are intermediate between deque and queue . Specifically, an input restricted deque is a deque, which allow insertion at only one end off the list but allow deletion at both the end of the list; and an output restricted deque is a deque which allows deletion at only one end of the list but allow insertion at both the ends of the list.

DEQUES

Priority Queues:

A priority queue is a collection of elements such that each element has been assign a priority and such that the order in which the element are deleted and processed comes from the following rules:


  1. An element of higher priority is processed Before any element of lower priority
  2. Two elements with the same priority are processed according to the order in which they were added to the queue.

A prototype of a priority queue is a time sharing system:  programs of higher priority or precede first and program with the same priority form standard queue. 

One way list representation of Priority Queue: 

One-way to maintain a priority queue in memory is by means of one way list,  as follows.

  1. Each node in the list will contain three item of information:  and information field INFO, a Priority number PRN and link number LINK.
  2. A node X proceeds in node Y in the list: 
    1. When x has higher priority than y
    2. When both have same priority but X was added to the list before y. This means that the order in the one-way list corresponds to the order of the priority queue
One-way List Representation of Priority Queue

Array representation of Priority Queue:

Another way to maintain a priority queue in memory is to use a separate queue for each level of priority.  Each such you will appear in its own circular array and must have its own pair of pointer, front and rear.  In fact, If each queue is allocated the same amount of space, if two dimensional array queue can be used instead of the linear array.

Figure shows this representation of priority queue.  Observe that FRONT [K] and REAR [K] contents, respectively, different and real elements of row K of QUEUE, the row that maintain the queue of elements with priority number k.

Array Representation of Priority Queue