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.

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”
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

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. Representation

6. The Loop Zoo
Extract The Loop Zoo

7. Modules, Subroutines, Procedures and Functions
Extract Modular Programming

8. Algorithms
Extract: Binary Search
Extract: Recursion ***NEW!!

9. The Object Of It All
Extract Why Objects

<ASIN:1871962722>

<ASIN:B09MDL5J1S>

 The Ongoing State Of JavaScript26/06/2024The latest State of JavaScript Survey has just been published, bringing us a wealth of details about what parts of the JavaScript ecosystem and used, loved and wanted by developers. + Full Story Rust's Rapid Rise on TIOBE Index10/07/2024Rust is making spectacular progress up the TIOBE index and JavaScript is also on the up and experiencing a personal best. Kotlin is maintaining its inclusion in the top 20 and the gap at the very top  [ ... ] + Full Story More News