PF-CE Lab03_1D Arrays
PF-CE Lab03_1D Arrays
(CL1002)
LABORATORY MANUAL
BS-CE, Spring 2025
LAB 03
Arrays
______________________________________
Tools needed:
1. PC running Windows Operating System (OS)
2. DevC++ (installed)
Turn on the PC and log into it using your student account information.
Note:
The information and concepts discussed in this
lab manual are in sufficient detail but are
still not exhaustive. It is assumed that all
these concepts are already taught to you in your
PF theory class.
1. Arrays
When we need to store a lot of values of the same type, instead of declaring many
variables, it is convenient to be able to use a single name but access different values by
indexing. Such data structures are called arrays.
Declaration:
An array is declared as follows:
datatype nameofarray[sizeofarray];
For example:
Indexing:
Individual values within an array are called elements. An array element is accessed
through indexing. Index values start from zero and the maximum index value can be one
less than the size of the array. For example, for an array of size ten, legitimate index
values vary from 0 - 9. The index is provided within square brackets, as follows:
int age[10];
cin >> age[0]; //get input into first index of the array
Indices may not be constants. Any expression that evaluates to an integer value can be
used as an index. For example, consider the code below:
Initialization:
An array can be initialized right when it is declared, as follows:
Note that ellipsis (...) is not part of the syntax. Instead, it means that we need to supply
enough values to initialize the required number of elements in an array as needed.
Number of values being initialized can be less than the size of the array. The rest of the
values get initialized to zero.
A detailed memory-map of the array age[ ] (first example above) is as given below:
Note byte address differs by 4 as each element in the array is an int which takes 4 bytes
to store the value.
When an array is not initialized, the elements can take any values. It is customary to call
such values as garbage values, since we do not know for sure what they are. We can
denote a value with a ? if we do not know what value it is at a specific point in the
execution of the code.
For example, after the declaration of an array as follows:
The values provided to initialize the array must conform to the datatype of the array.
Address of an array:
The array name is treated as its starting (or base) address. Note that an array is
contiguous in memory, i.e., it is allocated all the memory in a single chunk. An array that
is declared to hold 10 int values will be allocated 4x10 = 40 bytes. The array name
serves as the starting address. Subsequent array elements are stored in increasing
memory addresses.
int age[10];
cout << age;
This will display the address of the array (first element) in hex; not the value of any
element in the array. This is in contrast with a regular (non-array) variable, where printing
it gets its value; and using ‘&’ before the variable name gets its address,
int arr[10] = {25, -10, 3, 4, 99, 100, 74, -890, 0, 1}, val;
cout << "Enter an integer value to search: "; cin >> val;
if(index >= 0)
cout << "Value found at " << index << " index.";
else
cout << "Value not found.";
cout << endl;
The algorithm used above for searching a value in an array is known as Linear Search.
Task # 1: Take an array of ten elements. Write code to find the frequency of occurrence of
an element in an array entered by user. e.g.
1 5 4 1 1 3 2 5 1 5
Occurrence of 1: 4
Task # 2: Write a program to declare an array of odd number of elements. Split it into middle
and store the elements in two different arrays. e.g.
Initial array:
22 58 19 87 15 72 9 1 37
After splitting:
22 58 19 87 15
72 9 1 37
Task # 4: Store an array of integers indicated by constant variable SIZE and write code that
checks if the array is stored in ascending order or not. Output should be the original array
and the result.