Friday, October 23, 2020

Identifiers, Variables and Datatypes in 'C'

Identifiers:

  All the words that we'll use in our C programs will be either keywords or identifiers.

  Keywords are defined and can't be changed by the user, while identifiers are user defined words and are used to names to entities like variables, arrays, functions, structures etc.


Rules for naming identifiers are:

  1. The name should consist of only alphabets (both upper and lower case), digits and underscore sign( _ ).        
  2. First character should be an alphabet or underscore.
  3. The name should not be a keyword.
  4. Since C is case sensitive, the uppercase and lowercase letters are considered different.
  5. An identifier name may be arbitrarily long.

Variables:

Variable is a name that can be -used to store values. Variables can take different values but one at a time. These values can be changed during execution of the program. A data type is associated with each variable. The data type of the variable decides what values it can take.

The rules for naming variables are same as that for naming identifiers.

 Declaration of Variable:

It is must to declare a variable before it is used in the program. Declaration of a variable specifies it’s name and datatype. The type and range of values that a variable can store depends upon its datatype.

The syntax of declaration of a variable is-

datatype variablename;

Here datatype may be int, float, char, double etc. Some examples of declaration of variables are

int x;

float salary;

char grade;

Here x is a variable of type int, salary is a variable of type float, and grade is a variable of type char.

We can also declare more than one variable in a single declaration. For example- )

int x, y., z, total;

Here x, y, z, total are all variables of type int.

Initialization of Variables:

When a variable is declared it contains undefined value commonly known as garbage value. If we want we can assign some initial value to the variable during the declaration itself, this is called initialization of the variable.

For example

int a = 5;

float x = 8.9, Y = 10.5;

char ch = 'y';

double num = 0.15197e-7;

int l, m, n, total = 0;

Datatypes:



Saturday, October 10, 2020

Structure of a 'C' Program

C program is a collection of one or more functions. Every function is a collection of statements that perform a specific task.

The general structure of C program is:

Structure of a 'C' Program

Comments can be placed anywhere in a program and are enclosed between the delimiters /* at */. Comments are generally used for documentation purposes. Preprocessor directives are processed through preprocessor before the C source code passes through compiler. The commonly used preprocessor directives are #include and #define. #include is used for including header files. #define is used to define symbolic constants and macros.

Every C program has one or more functions. If a program has only one function then it must be main(). Execution of every C program starts with main() function. It has two parts, declaration of local variables and statements. The scope of the local variable is local to that function only. Statements in the main() function are executed one by one. Other functions are the user-defined functions, which also have local variables and C statements. They can be defined before or after main(). It may be possible that some variables have to be used in many functions, so it is necessary to declare them globally. These variables are called global variables.


Friday, October 9, 2020

History & Characteristics of 'C'

History:

    In earlier days, every language was designed for some specific purpose. For example FORTRAN (Formula Translator) was used for scientific and mathematical applications, COBOL (Common Business Oriented Language) were used for business applications. So need of such a language was felt which could withstand most of the purposes. "Necessity is the mother of invention". From here the first step towards C was put forward by Dennis Ritchie.

     The C language was developed in 1970's at Bell laboratories by Dennis Ritchie. Initially it was designed for programming in the operating system called UNIX. After the advent of C, the whole UNIX operating system was rewritten using it. Now almost the entire UNIX operating system and the tools supplied with it including the C compiler itself are written in C.

    The C language is derived from the B language, which was written by Ken Thompson at AT&T Bell Laboratories. The B language was adopted from a language called BCPL (Basic Combined Programming Language), which was developed by Martin Richards at Cambridge University.

    In 1982 a committee was formed by ANSI (American National Standards Institute) to standardize the C language. Finally in 1989, the standard for C language was introduced known as ANSI C. Generally most of the modern compilers conform to this standard.


Characteristics:

It is a middle level language. It has the simplicity of a high level language as well as the power of low level language. This aspect of C makes it suitable for writing both application programs and system programs. Hence it is an excellent, efficient and general-purpose language for most of the application such as mathematical, scientific, business and system software applications.

C is small language, consisting of only 32 English words known as keywords (if, else, for, break etc.). The power of C is augmented by the library functions provided with it. Moreover, the language is extensible since it allows the users to add their own library functions to the library.

C contains control constructs needed to write a structured program hence it is considered a structure programming language. It includes structures for selection (if.. .else, switch), repetition (while, for, do... while) and for loop exit (break).

The programs written in C are portable i.e. programs written for one type of computer or operating system can be run on another type of computer or operating system.

Tuesday, September 22, 2020

Transforming Infix Expression into Postfix Expressions:

 Let Q be an arithmetic expression written in infix notation. This algorithm transforms the infix expression into postfix expressions.


POLISH(Q, P)

Support Q is an arithmetic expression written in infix notation.  This algorithm finds the equivalent postfix expression P.

  1. Push “(” Onto STACK, and add “)”  to the end of Q.

  2. Scan Q from left to right and repeat step 3 to 6 for each element of Q until the stack is empty:

  3.  If an operand is encountered,  add it to P.

  4.  If left parenthesis is encountered,  push it onto  STACK.

  5.  If an operator is encountered, then:

    1. Repeatedly pop from stack and add to P  each operator (on the top of the STACK) which has the same precedence or higher precedence than the operator

    2.  Add operator to STACK

[End of If structure]

  1.  If a right  parenthesis encounter, then :

    1.  Repeatedly pop from the stack and add it to P each operator (on the top of the stack) until a left parenthesis is encountered.

    2.  Remove the left [Do not add the left parenthesis to P].

  [End of if  structure]

  [End of step 2 loop]

  1. Exit






Towers of Hanoi:

Suppose 3 pegs, labelled A, B and C, are given, and suppose on peg A there are placed a finite number of N of disks with decreasing size.  The objective of the game is to move the disk from Peg A to Peg C using Peg B as an auxiliary.

The rules of the game are as follows:

  1. Only one disk may be moved at a time, specifically,  only top disks on any peg may be moved to any other peg.
  2. At no time a larger disks placed on smaller disks.

 

Initial Setup of Towers of Hanoi with n=6

 Sometimes we will write X->Y  to denote the instruction “ Move top disk from peg X to Peg Y”  where X and Y may be any of the three pegs.

The solution of the Tower of Hanoi problem for n = 3 shown in figure.

n=3 :               Move top disc from peg A to Peg C

Move top disc from peg A to Peg B

Move top disc from peg C to Peg B

Move top disc from peg A to Peg C

Move top disc from peg B to Peg A

Move top disc from peg B to Peg C

Move top disc from peg A to Peg C



In other words,

n=3,   

A->C,              A->B,              C->B,              A->C,             B->A,                         B->C,              A->C

We also give the solution to the Tower of Hanoi problem for n=1 and n=2.

n=1                 A->C

n=2                 A->B,              A->C,              B->C

 

Rather than finding a separate solution for each n,   we use the technique of recursion to develop a general solution.  first we observed that the solution of the Tower of Hanoi problem for n >1  disk may be reduced to the following some sub problem.

  1. Move the top n-1 disks from peg A to peg B
  2. Move the top n disks from peg A to peg C : A->C
  3. Move the top n-1 disks from peg B to peg 
Let us now introduce a general solution

                               TOWER (N, BEG, AUX, END)

To denote a procedure which moves the top n disks from the initial peg BEG to final peg END using the AUX as an auxiliary.  When n = 1 we have a following solution:

TOWER (1, BEG, AUX, END) Consist of the single instruction BEG->END

When n>1, the solution may be reduced to the solution of the following three sub problems:

TOWER(N-1, BEG, END, AUX)

  1. TOWER(1, BEG, AUX, END) or BEG->END
  2. TOWER(N-1, AUX, BEG, END)
  3. TOWER(N-1, AUX, BEG, END)

TOWER (4, A, B, C)





Procedure: TOWER (N, BEG, END, AUX)

This procedure gives a recursive solution of the Tower of Hanoi problem for N disks.

 1.         If N=1, then :

    1. Write BEG->END
    2. Return.

[End of if Structure]

2.            [Move N-1 disks from peg BEG to peg AUX]    

Call TOWER (N-1, BEG, END, AUX)

3.            Write: BEG-> END

4.            [Move N-1 disks from peg AUX to peg END]

Call TOWER (N-1, AUX, BEG, END)

5.            Return.


Tuesday, September 15, 2020

Creating Menus in VB

Menu bar is one of the foundation controls in any window based application.  The list of menus is a display in the menu bar.  The menu options are display when the individual menu is clicked.

The menu editor is used to create the menu on the form.

To display the menu editor:

Right click on the form and select the option Menu Editor from the menu displayed.

Menu Editor Dialogue Box:

 


 Allow you to create custom menu for your application and to define their properties.

Dialogue Box Options:

Caption:  Allow you to enter the menu or command name that you want to appears on your menu bar or in a menu.

Name: Allow you to enter a control name for the menu item. A control name is an identifier used only to access the menu item in code; it doesn't appear in a menu.

Shortcut:

 Allow you to select a shortcut key for each command. i.e.  the combination with Ctrl  key.

E.g.  Shortcut to save command is Ctrl+S 

Checked:

Allow you to have a check marks appear initially at the left of a menu item.  It is generally used to indicate whether a toggle option is turned on or off.

Enabled:

Allow you to select whether you want the menu item to respond to event, or clear if you want the item to be unavailable and appears dimmed.

Visible:

 Allow you to have the menu item appears on the menu.

Right arrow:

Moves the selected menu down one level each time you click it.  You can create up to four level of submenu.

Left Arrow:

Move the selected menu up one level each time you click it. You can create up to four level of submenu.

Down Arrow:

Move the selected menu item down one position within the same menu label each time you check it.

Menu List:

A list box that displays a hierarchical list of menu items. Submenu items are intended to indicate their hierarchical position or level.

Next:

 Moves selection to the next line

Insert:

 Insert a line in the list box above the currently selected line

 Delete:

Delete the currently selected line

Ok:

Closes the Menu Editor and applies all changes to the last for you selected.

Cancel:

Closes the Menu Editor and cancels all changes.

Step to create menus as a displayed on the following form:

 


  

  1. Right click on the form and select the option Menu Editor from the menu displayed
  2. Enter the caption and name of the main menu ‘Abcd’.
  3. Click on the next button.
  4. Enter the name and caption of the menu item ‘a’
  5. Since ‘a’  is a menu option and not the main menu,  it is to be preceded with dots hence click on the right arrow button(->) to place the dots ahead of it.
  6. Click on the Next button
  7. Enter the name and caption of the menu item  ’b’
  8. Click on the Next button
  9. Enter the name and caption for the item ‘x’.  Since ‘x’ is a submenu of the menu item ‘b’ and hence it is to be preceded with double the number of dots.  Click on the right arrow button(->)
  10.  Click on the Next button.
  11.  Enter the name and caption of menu ‘y’
  12. Click on next button.
  13. Enter the name and the caption of menu ‘c’
  14. To remove the additional dots head of the menu ‘c’ click on the left arrow button (<-).

Popup Menus:

A menu which is displayed, when the control click with the right mouse button is called as popup menu.

A popup menu is a floating menu that is display over a form, independent of the menu bar.  The items displayed on the popup menu depend on where the pointer was located when the right mouse button was pressed; therefore, popup menus are also called as context menus.

Syntax to display the popup menu is as follows:

object.popupMenu  menuname

Program to display the pop-up menu when the command is right  clicked.

Create a menu in the menu editor.  Set the visible property of the menu false in the menu editor

Use the popup menu method in the mouse down event of the control to display the menu  when the control is right  clicked.

Privates Sub Button1_ MouseDown( Button as integer,  Shift as integer,  X as Single , Y as Single)

If Button =2 Then

Popupmenu  abcd

End if

End sub



Tuesday, September 1, 2020

Modules in VB:

Form Module: The form that you can see on the screen is a representation of the properties that define its appearance and behavior of a component place on it.  For each form in an application there is a related form module that contains its code.

Each form module contains event procedure which is a section of the code where you place the instruction that will execute in response to specific event.  Form can contain controls.  For each control on the form there is a corresponding state of event procedure in the form module.  In addition to the event procedure, form module can contain the general procedure that is executed in response to a call from any event procedure.

To add a new form to the project, from the project menu select the option “Add Form”, selected type of the form and click on open button.

Standard Module:

This standard module can be used to declare the global data and the global methods which can be accessed anywhere in the project or an application.

Code that isn't related to a specific form control can be place in a different type of module, standard module (.BAS)

To add a standard module into the project, from the project menu select an option “Add Module”, and click on “open” button

Class Module:

A class module is used to create object that can be called from procedure within your application.  Where as a standard module contain only code, a class module contain both code and data.

The members in the class module can only be accessed through its object.  One class can be defining in one class module.

To add a new Class module to the project, from the project menu select the option “Add Class Module”, select the type of the class and click on open button.

By default, your project contains a single form module.  User can add additional form, class and standard module, as needed.