Desktops and laptops form a minority of the computers in the real world. In the main most computers exist in the innards of items like cars, toasters, microwaves etc. These are embedded systems. All these systems require software programs.
The two principal programming models are:
Procedural - Drives the user. Program controls order of output and input. This control is exerted by the program design.
Event Driven - User is in charge as in a Windows environment. Program does nothing until user clicks an icon.
This course looks at procedural programming first. This is followed later by event driven programming and the introduction of objects.
Software
Binary numbers were originally used to produce Machine Code Programming. Difficult to read and maintain. Modern, easier to read and use languages and tools were developed. These allow:
The production of source code written in a text-editor tailored for writing computer programs.
Translating the program source code into machine readable code by a compiler.
Testing of the program by debugging.
Use of a linker to enable the reuse of other programmers code.
Libraries of code for common tasks.
Facilities for programming for Windows.
This course uses C++ Builder from Borland International Inc.
Builder
The following lines:
#include <vcl.h>
#include <stdio.h>
tell Builder to incorporate library code.
The line - #pragma hdrstop tells builder how to deal with library files.
This line is a comment:
Or at least the slashes are. The dashed line is a visual aid to that comment in this instance.
The line - #pragma argsused prevents warning messages regarding argc, * argv[] from the line that follows it.
The complete code listing is shown below.
#include <vcl.h>
#include <stdio.h>
#pragma hdrstop
#pragma argsused
int main(int argc, char* argv[])
{
puts("Hello World.");
puts("Press enter to close the program.");
getchar();
puts("Not finished yet. Press enter again!");
getchar();
return 0;
}
puts("Hello World."); puts the string to the screen.
getchar holds the program until a key is pressed (get character) in response to the program output.
The above two (puts and getchar) are from a code library called stdio, accessed via the header file by the command #include <stdio.h> .
VAT Program
#include <vcl.h>
#include "MT262io.h";
#pragma hdrstop
#pragma argsused
int main(int argc, char* argv[])
{
float Price;
float VatRate;
float Vat;
Price = 6.20;
VatRate = 0.175;
Vat = Price * VatRate;
Price = Price + Vat;
WriteString("Price including VAT is ");
WriteFloat(Price);
getchar();
return 0;
}
Now we have included the MT262 library. We have established some variables:
Price
VatRate
Vat
Variables have labels called identifiers. Good practice is the use of illustative words, running multiple words together using capitals at the start of each word e.g. VatRate.
These variables will use float numbers and values have been set for the variables.
Calculations are performed and the result returned by the program.
Using Computers
All computers have:
Storage
Input Device(s)
Processor
Output Device(s)
Embedded Systems
Input comes from the user and the appliance. Output can go to user via display or to the appliance. Programming can be procedural or event driven. In a procedural program the program would continually interrogate (aka polling in an embedded system) the sensor. In an event driven model, (termed interrupt driven in embedded systems) the sensor reports to the program and the program reacts (to the event/interruption)
In an embedded system events have to be prioritised.
Non-Embedded Systems
Here we are thinking about the PC/MAC's of the world.
Interaction between the computer user and application programs is via the Operating System (OS). The user may appear to be working with his/her spreadsheet, but it is the OS that controls how the program behaves and what the user sees on his screen.
Although Windows is event driven, there are times when the program is in charge e.g. a dialog box that a user must respond to before the program allows the user to proceed.
In the main programs of the Windows type are event driven. In addition these programs rely on code re-use. Most will have the familiar File - Open menu options. The code does not have to be written afresh for each program. Re-usable code is also in use in procedural programs.
What code does is the specification. How it does it is the implementation. A programmer needs to know this specification before he can re-use it with confidence. C++ separates these two. Comments in the header file should contain the information for the specification. The code file contains the C++ code for the implementation.
Re-usable code and project management promotes the development of small modules with proper specification. These are easier to test and maintain.
Computer Hardware
Computers must have the following features:
Receive information through Input.
keyboard
mouse
scanner
microphone
camera
cable
modem
Display information through Output.
monitor
printer
plotter
loudspeakers (through sound cards)
cable
modem
industrial tools
Storage
ROM
RAM
CD-ROM
Hard Disk
Magnetic Tape
Floppy Disk
Processing Information
CPU
additional microprocessors for specific tasks
Embedded systems may have only one processor
Design and Line Numbering
The following are my observations on the subjects of Design , Numbering and Coding as they appear in the course. Please bear in mind that these are only my own thoughts.
Design
1 The design document requires a data table, for referencing names.
Type
Name
Description
AnsiString
InputString
The string for processing, as input from the keyboard.
The names should be reasonably descriptive, not too short, not too long. Note the use of Capital letters rather than underscore (Input_String)
2 The design must be followed exactly by the code .
For instance, at a minor level, if the design shows a particular string to be used as a prompt say, then the same string must be used in the code. More seriously if a decision is made in the design to use a while loop then the code should not change this to a do/while loop.
Since you are both designer and coder in this instance, you may have a good idea as to how to change the design for better as you are coding and running(often things become clearer, particularly after one debug session). But if this is the case then you should go back, rethink the design to incorporate the change, so that once more the code follows the design exactly!
Numbering
Most console designs could start with three basic steps;
Input / Process input / Output. However a numbering of lines that follows this pattern (1 / 2 / 3) rather restricts our options for including finer detail. I prefer to start with the following considerations;
Drawing up a variable/data table points to a first step of declaring and initialising all variables
The next step will usually involve the input of data
Then think of any obvious loops/conditions/ selects that will occur at a basic level in the design. Now give the start and end of each of these a single number, together with a number for the process that occurs between them and which will in turn be expanded.
A first stab at design and the accompanying numbering could look something like -
1 Initialise all variables (from the data table)
2 Input data
3 loop while (condition)
4 Process data
5 loopend
6 Output result.
The process of 4 above will expand as needs be, giving something along the lines
3 loop while...
4.1 something...
4.2 something...
4.3 if (condition)
4.4 process
4.5 ifend
4.6 etc
...
5 loopend
.... and here the process 4.4 could split further ?
4.3 if
4.4.1 something
4.4.2 something
4.4.3 something
etc
....
4.5 ifend
So each construct start is numbered X.X.n say, the body of the process is then X.X.(n+1) and the construct end is numbered X.X.(n+2), if you see what I mean? Notice that indentation is occuring because each increase in numbers is pushing the statements outwards.
So adding lines of code is easy, and you dont have rewrite the design. Suppose you want to insert a few more lines between lines 4.1 and 4.2. These are just numbered 4.1.1, 4.1.2 and so on, and can be written beside the design, or at the bottom of the page.
Coding
The most important thing here is as mentioned above, the code should follow the design and its psuedo code exactly.
Other points to note are;
Use indentation, preferably the same as that from the design. It is much simpler to follow pairs of curly brackets { } that enclose blocks of code if the opening and closing brackets are on the same indented column on the page and not in line with any nested blocks etc.
It is never a bad thing to place comment in your code. At least open with a block comment as to what the main function does.
/****************
This program does the following............
.........................
****************/
( Note that a number of lines of comment are placed between the TAGS /* and */. A single line
of comment can be placed after the single TAG // )
A few good comments at various points in the code make life considerably easier. But only to point out the non-obvious, such as
result = (x*y)/(z*100) // this formula calculates the percentage .....
and not something such as
WriteString("Have a good day") // this prints out the string "Have a good day"
The { } of a block of code can also be commented to keep track of pairs. eg
while ( ............)
{
something
something
}//while end
Starting a new project:
1. Open a new console application in Builder:
- Select File ... New
- Double-click the "Console Wizard" icon
- The "Window Type" should be "Console"
- The "Execution Type" should be "EXE"
- Include Visual Components Library must be ticked.
2. Save the project in a suitable place with a suitable file name:
- Select File .... Save Project As
3. Add any libraries to the project (especially the MT262io one!):
- Select Project .... Add to Project
- You many need to change the file type from *.cpp to *.lib
- Select MT262io.lib
- Repeat above steps for any other relevant libraries
4. Add the appropriate header files:
- type in "#include "MT262io.h" just before the line "#pragma hrdstop"
- repeat for any other relevant header files
5. Add the statement required to hold the screen output:
- type in "getchar();" on the line before before "return 0;"
Katherine Childs
Top
Statement missing:
Just insert a semicolon (;) at end of line before the highlighted statement and that should do the trick!
"Undefined..."
Either...
1. Misspelled identifier: check upper and lower case letters... C++ is case-sensitive, so be careful!
or...
2. Missing header file: ensure that any #include and USE...(...) statements are specified
Top
Linker error "undefined"...
File not correctly added to project. Use Project ... Add to project to try this again
Error: "Runtime error 216 at ...." when closing Builder
Go to the Builder shortcut on the desktop
Right-click and select Properties
In the "Target" box put "-NS" at the end of the line and click on "OK"
Top
Changing the colour of the Run Window...
Include the following in your main program coding:
textattr(0xF0);
clrscr();
This will result in black font used against a white background.
Karen M. Baker
Common syntax errors...
Typos - and especially misspelled identifiers... C++ is case sensitive... so watch out!
Typing '='when you mean '=='... remember, '=' means "assigned to" and '==' means "is equal to" (at the design stage this is shown as "=" which adds to the confusion... doesn't it!!)
Missing variable declarations... if you use a variable in your code - remember to declare it
Missing out round brackets... all conditions (if, while and the like) need 'em!
Missing out those pesky semi-colons!
Putting braces (those curly {} brackets to us common folk) in the wrong place... or missing 'em out!