Lecture 01.2
Lecture 01.2
We have already gone through how computers represent information using binary
number system.
To do this, we need to decide on representations for numeric values and for symbolic
ones.
Let us think that, I ask you to retain the number 5 in your mental memory, and then I
ask you to memorize also the number 2 at the same time. You have just stored two
different values (5, 2) in your memory. Now, if I ask you to add 1 to the first number I
said, you should be retaining the numbers 6 (that is 5+1) and 2 in your memory. Now
subtract the second value from the first and you obtain 4 as result.
The whole process that you have just done with your mental memory is a simile of
what a computer can do with two variables. The same process can be expressed in
C/C++ with the following instruction set:
1. a = 5
2. b = 2
3. a = a + 1
4. result = a - b
Variables
1. a = 5
2. b = 2
3. a = a + 1
4. result = a – b
Obviously, this is a very simple example since we have only used two small integer
values, but consider that your computer can store millions of numbers like these at
the same time and conduct sophisticated mathematical operations with them.
So, a variable is a storage location and an associated symbolic name (an identifier)
which contains some known or unknown quantity or information, a value.
The variable name is the usual way to reference the stored value; this separation of
name and content allows the name to be used independently of the exact
information it represents.
The identifier in computer source code can be bound to a value during run time, and
the value of the variable may thus change during the course of program execution.
Compilers have to replace variables' symbolic names with the actual locations of the
data in the memory.
Memory Management
A variable is an area of memory that has been given a name. For example: the
declaration int x; acquires four byte memory area and this area of memory that
has been given the name x.
One can use the name to specify where to store data. For example: x=10; is an
instruction to store the data value 10 in the area of memory named x.
The computer access its own memory not by using variable names but by using a
memory map with each location of memory uniquely defined by a number, called
the address of that memory location.
To access the memory of a variable the program uses the & operator. For Example:
&x returns the address of the variable x.
&x represents the memory area of variable named x.
int x;
x represents the value stored inside the area of variable named x.
Array [1-Dimensional]
Definition & Structure
An array can hold a series of elements of the same type placed in contiguous
memory locations.
Each of these elements can be individually referenced by using an index to a unique
identifier.
In other words, arrays are a convenient way of grouping a lot of values of same type
under a single variable name.
For example, an array to contain 5 integer values of type int called mimo could be
represented like this:
0 1 2 3 4
mimo
|---int---|
where each blank panel represents an element of the array, that in this case are integer
values of type int with size 4 bytes. These elements are numbered/indexed
from 0 to 4 since in arrays the first index is always 0, independently of its length.
Declaration
where type is a valid data type (like int, float …), name is a valid identifier and the
elements field (which is always enclosed in square brackets []), specifies how many of
these elements the array can contain.
Therefore, in order to declare an array called mimo as the one shown in the above
diagram it is as simple as:
The elements field within brackets [] which represents the number of elements the
array is going to hold, must be a constant integer value, since arrays are blocks of
non-dynamic memory whose size must be determined before execution.
Initialization
When declaring a regular array (int mimo[5];) of local scope (within a function,
for example) its elements will not be initialized to any value by default, so their
content will be undetermined until we store some value in them. The elements of
global and static arrays are automatically initialized with their default values, zeros.
When we declare an array, we have the possibility to assign initial values to each one
of its elements by enclosing the values in braces { } separated by comas. For
example: int mimo[5] = { 16, 2, 77, 40, 12071 };
The number of values between braces { } must not be larger than the number of
elements that we declare for the array between square brackets [ ].
Initialization
An array can also be partially initialized. i.e. we assign values to some of the initial
elements. For example: int mimo[5] = { 16, 2};
Here the first 2 values are assigned sequentially. The rest 3 elements are unassigned.
The number in the square brackets [ ]of the array is referred to as the 'index'
(plural: indices) or 'subscript' of the array and it must be an integer number 0 to one
less than the declared number of elements.
We can access the value of any of its elements individually as if it was a normal
variable, thus being able to both read and modify its value. The format is as simple
as: name[index]
For the declaration int mimo[5]; the five (5) elements in mimo is referred in the
program by writing: mimo[0] mimo[1] mimo[2] mimo[3] mimo[4]
Each of the above array elements is an integer and each of them also acts as an
integer variable. That is, whatever operation we can perform with an integer variable
we can do the same with these array elements using same set of rules.
Some Facts
Arrays have a natural partner in programs: the for loop. The for loop provides a
simple way of counting through the numbers of an index in a controlled way. The
for loop can be used to work on an array sequentially at any time during a program.
The second of these tends to be the result on operating systems with no proper
memory protection. Writing over the bounds of an array is a common source of
error. Remember that the array limits run from zero to the size of the array minus
one.
Array Access Demonstration
Can you guess the output if we take 5 as input in the 4th line of the code?
This searching technique is also called Linear Search, because it searches the array
for a given element chronologically or linearly.
Operation on Array - Insertion
1. https://en.wikipedia.org/wiki/Array_data_structure