22.02.2016 Views

C Programming Yellow Book

6019BjHWX

6019BjHWX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Simple Data Processing<br />

Manipulating Data<br />

2.2 Manipulating Data<br />

2.2.1 Variables and Data<br />

2.2.2 Storing Numbers<br />

When you are writing a<br />

specification you should<br />

worry about the precision to<br />

which values are to be held.<br />

Too much accuracy may slow<br />

the machine down - too little<br />

may result in the wrong<br />

values being used.<br />

In this section we are going to take a look at how we can write programs that<br />

manipulate data, how values can be stored, retrieved and generally fiddled with. This<br />

provides us with the ability to perform the data processing part of programs.<br />

In the glazing program above we decided to hold the width and the height of the<br />

windows that we are working on in variables that we described as double. Before we<br />

can go much further in our programming career we need to consider just what this<br />

means, and what other types of data we can store in programs that we write.<br />

Programs operate on data. A programming language must give you a way of storing the<br />

data you are processing, otherwise it is useless. What the data actually means is<br />

something that you as a programmer decide (see the above digression on data).<br />

A variable is a named location where you can store something. You can think of it as a<br />

box of a particular size with a name painted on the box. You chose the name to reflect<br />

what is going to be stored there (we used sensible names like woodLength in the<br />

above program). You also need to choose the type of the variable (particular size and<br />

shape of box) from the range of storage types which C# provides. The type of the<br />

variable is part of the metadata about that variable.<br />

Programs also contain literal values. A literal value is just a value in your program<br />

which you use for some purpose. For each type of variable the C# language has a way<br />

in which literal values of that type are expressed.<br />

When considering numeric values there are two kinds of data:<br />

• Nice chunky individual values, for example the number of sheep in a<br />

field, teeth on a cog, apples in a basket. These are referred to as integers.<br />

• Nasty real world type things, for example the current temperature, the<br />

length of a piece of string, the speed of a car. These are referred to as<br />

reals.<br />

In the first case we can hold the value exactly; you always have an exact number of<br />

these items, they are integral.<br />

In the second case we can never hold what we are looking at exactly. Even if you<br />

measure a piece of string to 100 decimal places it is still not going to give you its<br />

exact length - you could always get the value more accurately. These are real. A<br />

computer is digital, i.e. it operates entirely on patterns of bits which can be regarded<br />

as numbers. Because we know that it works in terms of ons and offs it has problems<br />

holding real values. To handle real values the computer actually stores them to a<br />

limited accuracy, which we hope is adequate (and usually is).<br />

This means that when we want to store something we have to tell the computer<br />

whether it is an integer or a real. We also need to consider the range of possible<br />

values that we need to hold so that we can choose the appropriate type to store the<br />

data.<br />

You tell C# about a variable you want to create by declaring it. The declaration also<br />

identifies the type of the thing we want to store. Think of this as C# creating a box of<br />

a particular size, specifically designed to hold items of the given type. The box is<br />

tagged with some metadata (there is that word again) so that the system knows what<br />

can be put into it and how that box can be used.<br />

C# <strong>Programming</strong> © Rob Miles 2015 22

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!