Copyright 2001 (c) Logic Code Generator Ltd; You must not take information from this web page and reuse without the owner's permission!

COMPUTER PROGRAMMING AND PROGRAM PROBLEM SOLUTION

What is a Computer Program Problem?

 

WHAT IS A COMPUTER?

Definition
A concise definition of a computer is as follows:
Any machine or device that will accept an input amount of data, process it under the control and direction of a program and then output the process result to some other device or medium. Hence, a computer is made out of the basic elements illustrated here.

Illustration
Graphical illustration of a computer

Classification
Modern Electronic Computer systems can be classified in terms of
(a) Type
(b) Size
(c) Generation

DIGITAL COMPUTERS AND THE STORED PROGRAM CONCEPT

Digital computers operates on the principles of the binary number system and are therefore controlled by digital signals. An example digital signal is illustrated in figure 1-3. Here are some of the basic elements that constitute a micro-computer system

A microcomputer system is made out of three basic functional elements:
1. Input and Output (I/O) ports
2. A Central Processing Unit (CPU) and
3. Main Memory

Each component of a microcomputer system can in turn be broken down into smaller sub-system components. For example, the CPU (or microprocessor) consists of the following sub-system components:
(1) A set of registers
(2) A control unit
(3) An arithmetic and logic unit (ALU).

STRUCTURE OF A MICROCOMPUTER SYSTEM
Components of a micro-computer system

WHAT IS DATA?
The operations performed by a computer require data. Data values are represented as digital signals like that illustrated in figure 1-3. Data is the representation of facts, concepts, or instructions in a structured and logical manner suitable for processing, communication, and interpretation by humans or some programmed device. Examples of data are: The balance of a bank account, The score a student receive on a test, or The list of all male students doing a particular subject at a college. The purpose of a computer is to accept and process data to produce useful information for human use. In the context of a digital computer system, we define data as Information in a form that can be used by a computer program.

An Example Digital Signal
Example Digital signal

Therefore, we see that the signal represented in figure 1-3 could be that of the ASCII code for a keyboard character. It could even be that of a coded machine instruction been transmitted through the system or it could be the representation of some other data format been used by a program.

WHAT IS A COMPUTER PROGRAM?

A computer program is a predetermine sequence of instructions that specify the operations to be done by a computer on data values. An instruction is a sequence of Binary Digits (bits) that can be used to control the internal electronics of a digital computer system. Therefore, instructions are machine codes that specify what operation is to be performed by the electronics of the computer system.

The "Stored Programming Concept" requires that both the data values and the instructions that controls operations on them during the run of a program to be represented in a digital format and be stored in the Internal memory of the computer system.

WHAT IS A PROGRAMMING LANGUAGE?

On a digital computer, programs at machine level consist of a series of binary words that are directly interpreted by the central processing unit (CPU). These specific sequences of instructions are designed to solve a particular problem on a selected device. Programming is the process of selecting the correct sequence of machine instructions to be used for solving a specific problem. The binary coded instructions used on a selected electronic device (computer system) are determine by the electronic configuration of the device. However, there are fundamental concepts and principles that govern the way programs are written and how they are executed. Writing machine programs require that you have knowledge of the electronic configuration of the machine. In addition, a program written for a given machine is not portable to other machines. Therefore, writing machine programs is best left to people who like to do things using 1's and 0's. The example below illustrates a machine level computer program.

Assembly language is machine language written using mnemonics. A mnemonic is a word or combination of letters that suggest the meaning of the instruction it represents. Like BASIC, an assembly language consists of a set of commands that can be written into instructions that tells the computer what to do. However, each mnemonic word in an Assembly Language instruction set refers directly to the digital components of the computer system. Therefore, we see that Assembly Language mainly provides a convenient abstraction of the binary interpretation of a particular machine instruction set and the representation of data values on the system. Assembly Language does not adequately abstract the structure of a system.

From this we see that a programming language forms an interface between the programmer and the hardware system of the computer. There are levels of abstraction between the hardware system and the programming language. These abstractions are logical and are built on consistent rules that manifest themselves at the software interface as syntax and semantic rules in particular programming languages. Good programmers have a comprehensive knowledge of the way digital computer systems works and is able to appreciate the syntax and semantic rules in well defined programming languages. Understanding of a computer system includes basic knowledge of the way the hardware system works at machine or assembly language level.

HIGH LEVEL PROGRAMMING LANGUAGES

A digital computer system is design to execute instructions coded in a digital format. The set of instructions that the computer system is design to execute is called the machine instruction set for the system. Programming a computer system at machine level requires that the programmer is familiar with the instruction set of the machine and the physical architecture of the system. Therefore, as you can appreciate, people who are not expert in digital system are easily put off at this level.

Therefore, we had to invent a system whereby a person who is not an expert at this level can program such a digital device that will solve a problem for the programmer with the solution approach been in the programmer's problem domain as opposed to that of the implementation machine domain.

In a high level programming language instructions are written using statements and expressions similar to those used in every day natural languages. Instructions written in a low-level language are written using machine code or mnemonics. Low level languages are machine oriented. Because of the limitation of low level programming languages, high level languages were developed. High level languages are:
(a) Similar to English with the usage of words and symbols
(b) More user friendly than low level languages
(ac) Problem oriented rather than machine oriented
(d) Shorter than their machine equivalent.
A single statement in a high level language can be translated into many machine instructions

A SYSTEMATIC APPROACH TO PROGRAMMING
Click the following link to get more information about the systematic approach to programming and the Program Development Life Cycle. Click HERE

WHY USE PROGRAM FLOWCHARTS?

This section gives a concise introduction to the concepts and principles of flowcharting as a means for illustrating program algorithms to be executed on digital computer systems. This on-line documentation is not intended to be a detailed practical guide. Reference sources that gives more practical learning material including programming language concepts and design techniques are given at the link to additional learning resources. I will give you a link that gives you some experimental results from students learning programming with a visual design system as opposed to purely using a text base programming language.

The difference with UML is that it is base on high level concepts that are not well defined and does not deal with issues that have been bugging software engineers and computer programming for a long time now. Typical issues UML fail to deal with are the issue of Complexity, Maintainability and Re-usability. LogicCoder effectively deals with these issues by developing a standard for the design of program algorithms with the use of program flowchart. These are simple straight forward easy to understand principles. They are not targeted at the highly academic but they can be used by the highly creative person to build sound, large, complex and consistent systems.

Visual programming system that automates lots of programming tasks for the development of complex software systems is evolving and is becoming the predominant choice in all areas of program/software development.

SAMPLE PROGRAM PROBLEMS AND THEIR SOLUTION FLOWCHART

Problem specification
The sample program used in this chapter prepares a report on a checking account. The report is an output listing of checks drawn on a personal account in the month of September. Each line of the report list information on each check drawn during the month as follows. The amount drawn on each check, the check number, the check date and the person to whom the check is made payable. At the end of the report listing, the total number of checks and the total amount drawn on all checks for the month are printed on the output report.

The Problem Flowchart Solution
Sample program logic design

With LogicCoder, you can quickly and easily modify the control logic so that each record in the program is included as part of an internal data list. In addition, you can change the Input/Output symbols that "Read a Record" to appropriate Processing symbol so that the system implement the control loop as a for-next loop. You can also put these symbols into the Ignore state and then put an appropriate control for statement in the loop decision.

ANSI C source code generated by LogicCoder
void main(void)
{
r = 0; TotalChecks = 0;TotalAmount = 0;
printf("\n\nChecking Account report\t Date: %s", __DATE__);
scanf("%s,%f,%s",FileRec[r].Name, &FileRec[r].Amount, FileRec[r].Payee);
while(strcmp(NAME, "END OF FILE"))
{
TotalChecks++;
TotalAmount += CheckFile[r].Amount;
printf("\n\t%s\t%.2f\t%s", FileRec[r].Name, FileRec[r].Amount, FileRec[r].Payee);
scanf("%s,%f,%s",FileRec[r].Name, &FileRec[r].Amount, FileRec[r].Payee);
}
printf("\n\n\tTotal number of checks %d", Totalchecks);
printf("\nTotalAmount of all checks: %s", TotalAmount);
}

 

ANOTHER SAMPLE PROGRAM SPECIFICATION

The sample program in this chapter is to prepare a park camping fee report. The logic to be used by the sample program consists of a loop with a nested case structure. This nested case structure determines alternative ways for calculating campers' fees base on three tested conditions. The report created by the program contains report and column headings. Each line of the report list the camper's Name, the camper's Residency, the number of Days the camper is staying and the camping Fee. The camping fee is determined as follows. If the camper is a resident, the cost for the first seven days is 7.95 per day, after which the fee is calculated at 5.95 per day for the days in excess of 7. If the camper is a non-resident, the fee is calculated at 9.95 for the full camping period. If there is an error in the residency code field of the camper's record, i.e. this field neither contain a "R" (resident) or a "N" (non-resident), then the value; "UNKNOWN" is to be printed in the residency column on the output report. The input data file and a sample of the output report is given in figure 5-12

With LogicCoder, you can easily transfer the implementation of your program logic design from a given source language to another with very little effort. You do this by paying more attention to the control logic of your program and the meaning of command statements in the source language. No more need for "hang-ups" on programming lnguage paradigms.

The Program Flowchart


OUTPUT SPECIFICATION
The output consist of an edited report that list the values in the input data file as illustrated below in the sample output. You can also use LogicCoder to enter a list of data values to be used in a program for testing the program. For instance, if you Demeter the list of data values below, LogicCoder will insert them for you at the end of the source code with line numbers and string values properly delimited.



Sample Code Generated by LogicCoder in BASIC

500 REM ****** PROCESSING ******
510 REM
520 PRINT TAB(11) "DESERT PARK"
530 PRINT " "
540 PRINT " NAME RESIDENCY DAYS FEES "
550 PRINT " "
560 REM
570 READ N$, C$, D
580 REM
590 IF N$ = "END OF FILE" THEN 850
600 IF C$ = "R" THEN 700
610 IF C$ = "N" THEN 650
620 PRINT USING F1$; N$, U$
630 GOTO 810
640 REM
650 LET A = D * C3
660 LET T2 = T2 + A
670 PRINT USING F1$; N$, O$, D, A
680 GOTO 800
690 REM
700 IF D > D1 THEN 740
710 LET A = D * C1
720 GOTO 770
730 REM
740 LET A = (D1*C1) + ((D-D1)*C2)
750 GOTO 770
760 REM
770 LET T2 = T2 + A
780 PRINT USING F1$; N$, R$, D, A
790 GOTO 810
800 REM
810 LET T1 = T1 + 1
820 READ N$, C$, D
830 GOTO 590
840 REM
850 PRINT " "
860 PRINT USING F2$; T1
870 PRINT USING F3$; T2
880 END

More advance versions of LogicCoder allow you to select to generate BASIC programs with the use of standard control structures as opposed to that that uses the traditional line numbering as illustrated here. Many high level programming languages implement these standard control structures with the use of standard control statements such as do-while, while, if, if-then and so on. Never the less, as we have seen, with LogicCoder there is no need to think about these control statements when writing the source code for a program.

ANOTHER PROGRAM SPECIFICATION

You are to design a program and then write the source code. The program is to encode the sorted sequence for a set of data values from two pre-sorted lists that are to be merged in-place. In addition, the permutation sequence should encode the order in which values are to be selected from the two list been merged in a stable manner.

BACKGROUND INFORMATION
A merge or sort algorithm is said to be in-place whenever it does merging or sorting with the use of constant amount of extra memory. In this case, the amount of memory required to implement the algorithm does not depend on the number of records in the input list(s). In addition, a merge algorithm is stable if it always leave sequences of equal data values in the same order at the end of merging. For example, if we have the set of indexed values in the A list and the B list as follows.

Illustration of stable merge

If the merge algorithm is such that it keeps the indices of a sequence of equal values from a given list in sorted order. I.e. they are kept in the same order as they were before the merge as illustrated in the output example for the above input, the algorithm is said to be stable. Otherwise, the algorithm is said to be unstable. Output when the merge operation is stable is as illustrated by C in figure 3-15. The encoding algorithm should encode the sequence in which the values in C are selected as follows: 10011100110100.

Note that, the merge operation that produces the output list C selects values from A in preference to that from B whenever a tie occurs. We define stability in the general sense to mean that values from A are always given preference whenever a tie occurs between values in A and B and that the indices of equal set of values are kept in sorted order at the end of the merge operation.

INPUT TEST DATA
The input test data to be used in the program consist of two pre-sorted lists, each consist of a set of integer values listed below.

Test data for a merge operation
During the merge if there is a tie between a value from the A list and the B list, the value from the A list is given preference to that from the B list.

OUTPUT
Output from the encoding operation should be a bit-pattern stored in a variable name of the appropriate type. This bit pattern encodes the sequence in which values are to be selected from A and B to create a stable merge. During the merge operation, we use a 1 to encode a value selected from the A list and a 0 to encode a value selected from the B list. We count bit position in the encoded data starting with the LSB. For example, to stable merge the two pre-sorted sequence A and B in figure 3-15, we create the new output list C by selecting values in A and B as explained above. Therefore, the output C would be encoded by the following bit pattern: 111110010011100100110000

THE PROGRAM LOGIC DESIGN
The overall control logic to be used in the program design is relatively simple and straightforward as illustrated by the flowchart in figure 3-16. However there is a need for the use of several bit-manipulating functions in order to meet the overall program objective.

For the purpose of doing a visual checks on the results of our program, we define a function that will print the bit pattern for a value stored as an unsigned long int. We call this function Print_bits( ). In this function we select each bit in the value to be printed by shifting it to the LSB position and then masking out the remaining bits. This function is defined in the source code of figure 3-17.

Merge encoding algorithm
Figure 3-20
The source code that implements the program logic illustrated by the flowchart in figure 3-16.
Further information on this program problem is found in the text book "STRUCTURED PROGRAMMING WITH ANSI C" Page 15 - 17

THE OUTPUT SOURCE PROGRAM
void main(void)
{
unsigned int Bit_counter = i = j = 0;
while((i < 12)&&(j < 12))
{
if(Array1[i] <= Array2[j])
{
Coded_bits = Coded_bits |Set_bit(Coded_bits, Bit_counter, 1);
i++;
}
else
{
Coded_bits = Coded_bits |Set_bit(Coded_bits, Bit_counter, 0);
j++;
}
Bit_counter++;
}
if(j == 12)
Set_bit(Coded_bits, Bit_counter, 1);
Print_bits(Coded_bits); }

Most information listed on this web page are taken from the following text books
(1) STRUCTURED PROGRAMMING WITH BASIC, Vol. 1, ISBN 0-9544270-1-7; Price $44.00 USD
(2) STRUCTURED PROGRAMMING WITH ANSI C; ISBN 0-9544270-0-9; Price $56.00 USD
You receive a free tutorial version of LogicCoder for ANSI C and BASIC with any purchase.

Copy right © May 2002
Logic Code Generator Ltd
131a Shenley Road
Borehamwood
Hertfordshire WD6 1AH
Tel: +44(0)203 538 6133
E-mail: info@