The Trick Of The Mind - Representation
Written by Mike James   
Monday, 18 July 2022
Article Index
The Trick Of The Mind - Representation
Strings and Arrays
The Record

The Record Represented


If the array is the representation of the numbered list, what about creating a representation for the record card? A record card is the basic method of manually recording information that you want to look up. For example, an address record card might have a field for a name, another for an address and another for a telephone number. A record isn’t a numbered list because you don’t say “give me field number 3” you say “what’s the telephone number”. You retrieve information from a record not by number but by name of field.

record

How do you represent a record?

The answer is that it is very similar to an array but instead of being organized by a number it is organized by field name. Such an organization should be called a record, but in most computer languages it is called a struct – short for structure. For example:

struct myAddress
	name: “mike”
	address: “1 FORTRAN Avenue”
	Telephone: 1234

defines a struct as a representation of an address record. To get to particular fields the most common convention is to use a dot notation or a qualified name.

 

For example:

myAddress.name

has the value mike and

myAddress.Telephone

has the value 1234.

Records or structs are most often used in programs that are about business. Scientific applications most often use arrays. Today most programming languages have arrays and structs, but in the early days it was often one or the other. For example, FORTRAN had arrays but no structs or records until much later – yet another example of the “two cultures”.

Nesting Data Structures

The array and the struct are the two simplest examples of data structures. We start from the basic idea of a variable and work our way up, representing ever more complex things using nothing but numbers and what we have already defined.

An array is a numbered list of items and a struct is a record of items accessed by field name. However, a field in a struct can be an array and we have already used this fact when we stored a “mike”, a string or character array, in myAddress.name. That is the field myAddress.name is a string, not just a simple value. This is a general principle. Each time a new data structure is invented to represent something in the external world it can usually be combined with existing data structures to do even more.

For example, an array can have elements that are arrays. This is a doubly numbered list or a two-dimensional array. An array can also have elements that are arrays of characters. This is more usually called an array of strings or string array. In this case we really do have something that is more like a numbered list because each element is a piece of text.

A struct can have fields that are arrays and arrays can have elements that are structs. What does this mean? An array of structs looks like a numbered list of records – an address book of sorts. In this case you could find the address of a particular entry by knowing its number in the list.

AddressBook[5].name

would the name field of the fifth address in the list. You can see how things start to look complicated even though the basic idea is very simple.

Things do become more sophisticated as we continue to develop the idea of finding representations for external world things. For example, the array of structs or records easily evolves into the idea of a database – a set of multiple lists of records that are related to one another in some way. A database might have a list of the names and addresses of the members of a club, a list of names and employment records of company employees, or a list of the prices and stock levels of inventory items to give only a few obvious examples.

The Data Determines The Program

At first look it seems that the problem of data representation is isolated from the problem of creating programs – but it isn’t. Data and how you work with it are very much intertwined. For example, if you have an array representing your data then the chances are you will need to examine each element or a subset of elements and this means that you are bound to write a loop. Arrays and loops go together in a very natural way, see the next chapter.

Similarly, though less obviously, other data structures determine what you are most likely to do with them and hence the form of the program that processes them. This idea has been elevated to the status of a programming method – search the Internet for Jackson Structured Programming – but these ideas are mostly out of fashion now.

What is very much in fashion is the idea that the data and its representation is more important than the program. A data representation should be active and incorporate what you most want to do with it. For example, a list of things should include a way of sorting the list and finding a particular entry in the list. Normally you would think of these actions as being part of a more general program, but in this view they should be integral parts of the data. When data and actions become intertwined in this way the result is usually called an “object” and the programming approach is called "object-oriented programming“, see Chapter 12.

Objects are data representations that know how to do things, i.e. perform actions that are appropriate for the data. This is a very sophisticated idea and it is currently the accepted way to program all but the simplest of things. As discussed in Chapter 12, in this world view, data is more important than lists of instructions.

General Representations

As already stated, the basic idea is to find representations of real world things in terms of nothing but numbers. Once we have the idea of mapping characters to character codes we can process text and everything else just seems to follow. There are lots of examples of sophisticated data structures that represent less common things from the real world – hierarchies or trees, for example – but the basic idea is the same.

You might be thinking that this idea of finding representations is unique to programming, but it isn’t. Computers work in binary, but for most of the time you can ignore this. Computers work with symbols that are represented in binary, but you can concentrate on the manipulation of the symbols.

This is much the same as what we do with natural language and natural thinking. We have a limited number of symbols available to use – usually an alphabet and we put these together to make words, and then sentences and then paragraphs and so on. Language is a symbolic representation of the world and often the solution to a problem requires us to find a better, or different, representation of something. If the something is completely new then you might need a completely new way of representing it, usually involving the symbols of mathematics.

The art of building symbolic representations is a key part of problem solving and thinking in general.

Summary

  • Computer memory can only hold so much data. A single byte can store a value up to 255. If you want to store larger values you have to use multiple memory locations.

  • Although computer memory can only store numeric values we can use a code to represent any sort of data we care to use.

  • Text is generally stored using the ASCII code or the more modern Unicode.

  • A single variable can only store a single value or, using an encoding, a single character. To store multiple characters we need a representation of a numbered list – the array.

  • The elements of the array are accessed via their positions in the list – their index value.

  • An array of characters is generally called a string.

  • The record or struct is the representation of the record and each element can be accessed using its field name as part of a fully qualified name.

  • The elements of a data structure can generally be any other sort of data structure. For example, you can use an array of strings as a simple database of names and addresses. Similarly a struct can have fields that are arrays, strings or anything that the language supports.

  • The role of data in programming is more than you might expect as the representation of the data strongly affects the sort of instructions you are going to write to process it.

  • The idea of data representation goes well beyond programming. Symbols that we use in reasoning are all representations of real world data, as is language itself.

Related Articles

Grammar and Torture

The Computer - What's The Big Idea?

The Essence Of Programming

The Trick Of The Mind - Programming & ComputationalThought

Buy Now From Amazon

Trick360

Chapter List

  1. The Trick Of The Mind

  2. Little Languages
       Extract: Little Languages Arithmetic

  3. Big Languages Are Turing Complete

  4. The Strange Incident of The Goto Considered Harmful
       Extract: The Goto Considered Harmful

  5. On Being Variable  

  6. Representation ***NEW!!

  7. The Loop Zoo

  8. Modules, Subroutines, Procedures and Functions

  9. Top-Down Programming

  10. Algorithms

  11. The Scientific Method As Debugging

  12. The Object Of It All

 <ASIN:1871962722>

<ASIN:B09MDL5J1S>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


PyPi Insists On 2FA For Critical Projects
26/07/2022

PyPi, the Python Package Index, which is the official repository of third-party open-source Python projects, has got tough with its requirements for critical projects. The plan is that two-factor auth [ ... ]



wxWidgets 3.2 Released
12/07/2022

There's a new release of wxWidgets, the open source cross-platform C++ framework for writing advanced GUI applications using native controls. The new release comes almost nine years after the beginnin [ ... ]


More News

pythondata

 



 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Monday, 18 July 2022 )