Friday, May 06, 2011

ROBOCON CRASH COURSE - Notes


Thursday, April 07, 2011



PC Interfacing - Introducing Variables, Memory concept and Arithmetic



Variables

Variables are identifiers whose value may change during the course of execution of a program. An essential element in this process is having a piece of memory that we can call our own, that we can refer to using a meaningful name and where we can store an item of data. Each individual piece of memory so specified is called a variable.

Each variable will store a particular kind of data, which is fixed when we define the variable in our program. One variable might store whole numbers (that is, integers), in which case it couldn't be used to store numbers with fractional values. The value that each variable contains at any point is determined by the instructions in our program and, of course, its value will usually change many times as the program calculation progresses.

Although you can use variable names that begin with an underscore, for example _this and _that, this is best avoided, because there are potential clashes with standard system variables which have the same form. You should also avoid using names starting with a double underscore for the same reason.
Examples of good variable names are:
  • Price
  • discount
  • pShape
  • Value_
  • COUNT
Declaring Variables

A variable declaration is a program statement which specifies the name of a variable and the sort of data that it can store. For example, the statement,

int value;

declares a variable with the name value that can store integers. The type of data that can be stored in the variable value is specified by the keyword int. Because int is a keyword, you can't use int as a name for one of your variables.

A single declaration can specify the names of several variables but, as we have said, it is generally better to declare variables in individual statements, one per line. We will deviate from this from time to time, but only in the interests of keeping the code reasonably compact.

Initial Values for Variables

When you declare a variable, you can also assign an initial value to it. A variable declaration that assigns an initial value to a variable is called an initialization. To initialize a variable when you declare it, you just need to write an equals sign followed by the initializing value after the variable name. We can write the following statements to give each of the variables an initial value:

int value = 0;
int count = 10;
int number = 5;

In this case, value will have the value 0, count will have the value 10 and number will have the value 5. These three statements are each declarations, definitions and initializations.
There is another way of writing the initial value for a variable in C++ called functional notation. Instead of an equals sign and the value, you can simply write the value in parentheses following the variable name. So we could rewrite the previous declarations as:

int value(0);
int count(10);
int number(5);

If you don't supply an initial value for a variable, then it will usually contain whatever garbage was left in the memory location it occupies by the previous program you ran (there is an exception to this which we shall see later). Wherever possible, you should initialize your variables when you declare them. If your variables start out with known values, it makes it easier to work out what is happening when things go wrong. And if there's one thing you can be sure of, it's that things will go wrong.

Memory Concept

The sort of information that a variable can hold is determined by its data type. All data and variables in your program must be of some defined type. C++ provides you with a range of standard data types, specified by particular keywords. We have already seen the keyword int for defining integer variables. As part of the object-oriented aspects of the language, you can also create your own data types, as we shall see later. For the moment, let's take a look at the elementary numerical data types that C++ provides.


As we have said, integer variables are variables that can only have values that are whole numbers. The number of players in a football team is an integer, at least at the beginning of the game. We already know that you can declare integer variables using the keyword int. These are variables which occupy 4 bytes in memory and can take both positive and negative values.
C++ also provides another integer type, long, which can also be written as long int. In this case, we can write the statement,


long bigNumber = 1000000L, largeValue = 0L;

where we declare the variables bigNumber and largeValue with initial values 1000000 and 0 respectively. The letter L appended to the end of the values specifies that they are long integers. You can also use the small letter l for the same purpose, but it has the disadvantage that it is easily confused with the numeral 1.


Integer variables declared as long occupy 4 bytes and since this is the same as variables declared as int using Visual C++ 6.0, they have the same range of values.


The char data type serves a dual purpose. It specifies a one-byte variable that you can use to store integers, or to store a single ASCII character, which is the American Standard Code for Information Interchange. We can declare a char variable with this statement:


char letter = 'A';

This declares the variable letter and initializes it with the constant 'A'. Note that we specify a value which is a single character between single quotes, rather than the double quotes which we used previously for defining a string of characters to be displayed. A string of characters is a series of values of type char, which are grouped together into a single entity called an array. Because the character 'A' is represented in ASCII by the decimal value 65, we could have written this:


char letter = 65;      // Equivalent to A

to produce the same result as the previous statement. The range of integers that can be stored in a variable of type char is from -128 to 127.


We can also use hexadecimal constants to initialize char variables (and other integer types). A hexadecimal number is written using the standard representation for hexadecimal digits: 0 to 9, and A to F (or a to f) for digits with values from 10 to 15. It's also preceded by 0x (or 0X) to distinguish it from a decimal value. Thus, to get exactly the same result again, we could rewrite the last statement as follows:


char letter = 0x41;    // Equivalent to A

Variables of the integral types char, int, short or long, which we have just discussed, contain signed values by default. That is, they can store both positive and negative values. This is because the default type modifier for these types is the modifier signed. So, wherever we wrote char, int, or long, we could have written signed char, signed int, or signed long respectively.
If you are sure that you don't need to store negative values in a variable (for example, if you were recording the number of miles you drive in a week), then you can specify a variable as unsigned:


unsigned long mileage = 0UL;

Here, the minimum value that can be stored in the variable mileage is zero, and the maximum value is 4,294,967,295 (that's 232-1). Compare this to the range of -2,147,483,648 to 2,147,483,647 for a signed long. The bit which is used in a signed variable to determine the sign, is used in an unsigned variable as part of the numeric value instead. Consequently, an unsigned variable has a larger range of positive values, but it can't take a negative value. Note how a U (or u) is appended to unsigned constants. In the above example, we also have appended L to indicate that the constant is long. You can use either upper or lower case for U and L and the sequence is unimportant, but it's a good idea to adopt a consistent way of specifying such values.

 Arithmetic

The basic arithmetic operators we have at our disposal are addition, subtraction, multiplication and division, represented by the symbols +, -, * and / respectively. These operate generally as you would expect, with the exception of division, which has a slight aberration when working with integer variables or constants, as we'll see. You can write statements like this:


netPay = hours * rate - deductions;

Here, the product of hours and rate will be calculated, then deductions subtracted from the value produced. The multiply and divide operators are executed before addition and subtraction. We will discuss the order of execution more fully later in this chapter. The overall result of the expression will be stored in the variable netPay.


The minus sign used in the last statement applies to two operands - it subtracts one from another. This is called a binary operation because two values are involved. The minus sign can also be used with one operand to change the sign of its value, in which case it is called a unary minus. You could write this:


int A = 0; 
int B = -5;
A = -B;                        // Changes the sign of the operand

Here, A will be assigned the value +5, because the unary minus changes the sign of the value of the operand B.


Note that an assignment is not the equivalent of the equations you saw in high school algebra. It specifies an action to be carried out rather than a statement of fact. The statement,


A = A + 1;

means, 'add 1 to the current value stored in A and then store the result back in A'. As a normal algebraic statement it wouldn't make sense.







PC Interfacing - Application Borland C++ Builder



Creating project for welcome application

Now we will try to create a program that displays Welcome message on the screen.
Right now you should have C++Builder running and you should be looking at a blank form.
By default, the form is named Form1.  To the left of the form, the Object Inspector shows the properties for the form. Click on the title bar of the Object Inspector. The Caption property is highlighted. Type WELCOME ! to change the form’s caption.

Now click the Run button on the speedbar. (You could also press F9 or choose Run | Run from the main menu.) C++Builder begins to build the program. After a brief wait, the compiler status box disappears, the form is displayed, and the caption shows WELCOME!. In this case, the running program looks almost identical to the blank form. You may have noticed when the program was displayed because it is displayed in the exact location of the form in the Form Editor. You’ve just written your first C++ Windows program with C++Builder. It can be moved by dragging the title bar, it can be sized, it can be minimized, it can be maximized, and it can be closed by clicking the Close button.

If you still have the Hello World program running, close it by clicking the Close button in the upper-right corner of the window. The Form Editor is displayed again, and you are ready to modify the form (and, as a result, the program). To make the program more viable, we’re going to add text to the center of the window itself. To do this, we’ll add a text label to the form. First, click on the Standard tab of the Component Palette. The third component button on the palette has an A on it.  Click the label button and then click anywhere on the form. A label component is placed on the form.

Click on the title bar of the Object Inspector or on the Caption property and type WELCOME !. Now the label on the form shows Hello World!. As long as we’re at it, let’s change the size of the label’s text as well. Double-click on the Font property. The property will expand to show the additional font attributes below it. Locate the Size property under Font and change the font size to 24 . As soon as you press the Enter key or click on the form, the label instantly changes to the new size. To move a component, simply click on it and drag it to the position you want it to occupy. Once you have the label where you want it, you’re ready to recompile and run the program. Click the Run button again. C++Builder compiles the program again and, after a moment , the program runs. Now you see WELCOME! displayed in the center of the form .


Title Bar

The main section of the title bar displays the C++ Builder 5 name of the application, and the name of the program that is running. A C++ Builder program is called a project. When Bcb starts, it creates a starting project immediately, and it names the starting project, Project1. If or when you add other projects, they take subsequent names such as Project2, Project3, etc. This main section of the title bar is also used to move, minimize, maximize the top section of the IDE, or to close Bcb. On the right section of the title bar, there are four system buttons with the following roles a) Minimize window b) Maximize window c) Restores window d) Close window

Menu Bar

Under the title bar, there is a range of words located on a gray bar; this is called the menu.  To use a menu, you click one of the words and the menu expands. Click File. There are four main types of menus you will encounter. When clicked, the behavior of a menu that stands alone depends on the actions prior to clicking it. Under the File menu, examples include Save, Close All or Exit. For example, if you click Close All, Bcb will find whether the project had been saved already. If it were, the project would be closed; otherwise, you would be asked whether you want to save it.

A menu that is disabled is not accessible at the moment. This kind of menu depends on another action or the availability of something else. A menu with three dots means an action is required in order to apply its setting(s). Usually, this menu would call a dialog box where the user would have to make a decision. A menu with an arrow holds submenu. To use such a menu, position the mouse on it to display its submenu.

Notice that on the main menu (and any menu), there is one letter underlined on each word. Examples are F in File, E in Edit, etc. The underlined letter is called an access key. It allows you to access the same menu item using the keyboard. In order to use an access key, the menu should have focus first. The menu is given focus by pressing either the Alt or the F10 keys. To see an example, press Alt.

Notice that one of the items on the menu, namely File, has its border raised. This means the menu has focus.

Press p and notice that the Project menu is expanded.
When the menu has focus and you want to dismiss it, press Esc.

Notice that the Project menu has collapsed but the menu still has focus. Press f then press o. Notice that the Open dialog displays.

On most or all dialog boxes that have either an OK, Open, or Save buttons, when you press Enter, the OK, Open, or Save button is activated. On the other hand, most of those dialog boxes also have a Cancel button. You can dismiss those dialogs by clicking Cancel or pressing Esc.

On some menu items, there is a combination of keys we call a shortcut. This key or this combination allows you to perform the same action on that menu using the keyboard. If the shortcut is made of one key only, you can just press it. If the shortcut is made of two keys, press and hold the first one, while you are holding the first, press the second key once and release the first key. Some shortcuts are a combination of three keys. To apply an example, press and hold Ctrl, then press S, and release Ctrl. Notice that the Save As dialog box opens. To dismiss it, press Esc.

Toolbar

A toolbar is an object made of buttons. These buttons provide the same features you would get from the menu, only faster. Under the menu, the IDE is equipped with a lot of toolbars. For example, to create a new project, you could click File _ New… on the main menu, but a toolbar equipped with the New button allows you to proceed a little faster.

By default, Bcb displays or starts with 6 toolbars. Every toolbar has a name. One way you can find out the name of a toolbar is to click and hold the mouse on its gripper bar and drag it away from its position


Saving and closing solution

A program in Borland C++ Builder is called a project. As an application, it is saved in a few steps. To save a project, on the Standard toolbar, click the Save All button.





PC Interfacing - Introduction Computers and Programming


Machine Language

The machine language (also known as machine code or native code) is a system of instructions for a specific processor or a data processing system which can be run without compilation process.
In contrast to assembly language or high level language , it is very hard for us to understand the   code , It can be read only by experts and usually they will try to understand machine code with the help of special programs which is called machine language monitors . 
 The machine code is usually generated from assembler or compiler from other programming languages.  Machine language can be programmed directly which means no assembler for the target processor is needed.
Normally if we want to program a processor we need an assembler to translate our program from a text file assembly program into binary machine instructions.
For the execution and translation of machine code on unsuitable processors, we can use emulators .

Assembly Language

An assembly language is a special programming language , which is written in a human readable form for a specific processor. Each computer architecture has its own assembly language.
program in assembly language is also known as assembler code. It has a special compiler or also known as assembler. It converts the assembly language directly to executable machine language. The process to convert back the machine code to human-readable assembly code is called disassembly . However after this process some information such as identifiers and comments could not be recovered, as this information already lost during compiling process and it makes us difficult to understand the program.
When we program a computer in assembly language, the full range of computer hardware and chip program can be directly exploited. It is because assembly language programs works very well since they are often much smaller and faster than higher level programs that have a similar degree of complexity.  Nowadays assembly language is rarely used, unless the programs are very critical (for example, the programming for device drivers of graphics cards ) or completely new technologies whereby the high-level language libraries are still not exist. In principle, nowadays most machines use high-level programming.  Other disadvantage of assembly code is there are higher error rate (due to the complexity.


High Level Language

A high-level programming is a programming language , which allows the writing of a computer program in an abstract language (it is understandable for humans).
The first computers were using programs in machine code instruction. This is merely a sequence of numbers. The processors will interpret the sequence of commands. These commands consist of simple instructions such as arithmetic, memory access, etc. The first innovation was the invention of assembly languages ​​. It is not abstract, but the command is represented in text form.

In the end of 1950 , computer was so powerful that translation software programs could significantly facilitate the input. Fortran , Algol , and Lisp was the first generation of high level languages:
§  Fortran - FOR mula TRAN slation = formula translation
§  ALGOL - ALGO rithmic L anguage = language algorithms
§  LISP - LIS t P rocessing for list processing
These first generation of higher-level languages ​​contain abstract elements such as conditional statements ("if X is true, then do y") and loops ("while x, leads from y"). This make the program are more readable.

Most "modern" programming languages ​​( BASIC , C , C + + , C # , Pascal - known by the IDE Borland Delphi and Lazarus IDE - and Java ) are the languages ​​of the third generation .

Comparison between assembly language and high – level language
High-level programming
Assembly language
Syntax often adapted to human ways of thinking
Space-saving, highly compressed syntax
Mostly machine-independent
Only on a particular type of processor running
Loss of speed through abstraction (trend)
Machine-oriented commands increase the speed
Abstract, machine-independent data types (integer, float, ...)
Data types of the processor ( byte , word , long word)
Several control structures ( if , while ,...)
Jump instructions, macros
Data structures (field record)
Only simple types
Extensive semantic analysis is possible
Only basic semantic analysis is possible
Example:
  A: = 2; FOR I: = 1 TO 20 LOOP A: = A * I; END LOOP; PRINT (A);
Example:
      . ST ST START: MOV R1, # 2 MOV R2, # 1 M1: CMP R2, # 20 BGT M2 MUL R1, R2, R2 JMP INI M1 M2. JSR PRINT END

Borland C++ Builder

C++Builder is Borland’s hot new rapid application development (RAD) product for writing C++ applications.
With C++Builder you can write C++ Windows programs more quickly and more easily than was ever possible before. You can create Win32 console applications or Win32 GUI (graphical user interface) programs.
When creating Win32 GUI applications with C++Builder, you have all the power of C++ wrapped up in a RAD environment. What this means is that you can create the user interface to a program (the user interface means the menus, dialog boxes, main window, and so on) using drag-and drop techniques for true rapid application development.
You can also drop OCX controls on forms to create specialized programs such as Web browsers in a matter of minutes.
C++Builder gives you all of this, but you don’t sacrifice program execution speed because you still have the power that the C++ language offers you.

Object Oriented programming (OOP)

OOP is a new technique in software development. By emphasizing software reusability in program coding, OOP has the potential of increasing programmer productivity while reducing the cost of software maintenance. It does this by treating data as objects capable of manipulating themselves and gives great importance to relationships between objects.
C++ is one of the widely-used OOP languages today. C++ provides classes for declaring objects. In fact, before it was called C++ this programming language was called C with classes.

Wednesday, January 12, 2011