0% found this document useful (0 votes)
2 views

PF-CE Lab03_1D Arrays

This laboratory manual for Programming Fundamentals (CL1002) focuses on arrays, detailing their declaration, indexing, initialization, and memory allocation. It includes practical tasks for students to implement, such as finding the frequency of elements, splitting arrays, and calculating averages. The manual emphasizes the importance of managing array bounds to avoid memory corruption.

Uploaded by

almautulamreeka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PF-CE Lab03_1D Arrays

This laboratory manual for Programming Fundamentals (CL1002) focuses on arrays, detailing their declaration, indexing, initialization, and memory allocation. It includes practical tasks for students to implement, such as finding the frequency of elements, splitting arrays, and calculating averages. The manual emphasizes the importance of managing array bounds to avoid memory corruption.

Uploaded by

almautulamreeka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Programming Fundamentals

(CL1002)

LABORATORY MANUAL
BS-CE, Spring 2025

LAB 03
Arrays

________________________________________ __________ ____

STUDENT NAME ROLL NO SEC

______________________________________

LAB ENGINEER'S SIGNATURE & DATE


Maryam Zafar

MARKS AWARDED: /10


_____________________________________________________________________
NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES), ISLAMABAD
Prepared by: Engr. Aamer Munir, Muhammad Hammad Version: 1.1
Date last
Last edited by: Engr. Azhar Rauf, Engr. Aamer Munir Sep 27, 2018
edited:
Date last
Verified by: Engr. Azhar Rauf Jan 10, 2025
edited:
Repetition and Flow Control Structures LAB 07
Lab Learning Objectives:
In this lab session you will learn about 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];

Note the square brackets [ ] are part of the syntax.

For example:

int age[10]; //to store ten integer values


float marks[100]; //to store marks of up to one hundred students

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:

int age[10]; //to store ten age values


for(int i = 0; i < 10; i++)
cin >> age[i]; //get input into ith index of the array

PF LAB NUCES, ISLAMABAD Page 2 of 7


Repetition and Flow Control Structures LAB 07
Similarly, a loop can be used to output (display) the values in an array. Also note the
expression used as index:

int age[5]; //to store five age values


for(int i = 1; i <= 5; i++)
cout << age[i-1] << endl; //display value at (i-1)th index

Initialization:
An array can be initialized right when it is declared, as follows:

datatype nameofarray[sizeofarray] = {value1, value2, ...};

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.

int age[5] = {19,20,20,21,23}; //all values initialized


int age[5] = {12,19,20}; //first 3 values initialized, rest to 0

A detailed memory-map of the array age[ ] (first example above) is as given below:

Element Index Byte Memory Access


# address contents
(4 bytes block)
1 0 0x00FF2200 19 age[0
]
2 1 0x00FF2204 20 age[1
]
3 2 0x00FF2208 20 age[2
]
4 3 0x00FF220C 21 age[3
]
5 4 0x00FF2210 23 age[4
]

Note byte address differs by 4 as each element in the array is an int which takes 4 bytes
to store the value.

In the second case, the map would look like this:

Element Index Byte Memory Access


# address contents
(4 bytes block)
1 0 0x00FF2214 12 age[0
]
2 1 0x00FF2218 19 age[1
]

PF LAB NUCES, ISLAMABAD Page 3 of 7


Repetition and Flow Control Structures LAB 07
3 2 0x00FF221C 20 age[2
]
4 3 0x00FF2220 0 age[3
]
5 4 0x00FF2224 0 age[4
]

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:

int age[5]; //uninitialized array

the memory map would be:

Element Index Byte Memory Access


# address contents
(4 bytes block)
1 0 0x00FF2214 ? age[0
]
2 1 0x00FF2218 ? age[1
]
3 2 0x00FF221C ? age[2
]
4 3 0x00FF2220 ? age[3
]
5 4 0x00FF2224 ? age[4
]

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,

Caution: Array bounds

PF LAB NUCES, ISLAMABAD Page 4 of 7


Repetition and Flow Control Structures LAB 07
In C++, the array bounds must be taken care of by the programmer. Changing values of
elements at indices that are not within limits (0 to one less than the size-of-array) would
overwrite (i.e., change) the adjacent memory bytes and may corrupt other variables!
Even in the case of read, we should be careful not to exceed the array bounds,
otherwise we are liable to read other variables’ data, which could be a privacy breach.

PF LAB NUCES, ISLAMABAD Page 5 of 7


Repetition and Flow Control Structures LAB 07
Example 1: For an array of integer values, the program below asks the user to enter an
integer value and then checks whether that value exists in the array or not. If it does, the
index at which the value is stored in the array is printed.

int arr[10] = {25, -10, 3, 4, 99, 100, 74, -890, 0, 1}, val;
cout << "Enter an integer value to search: "; cin >> val;

int index = -1;


for(int i = 0; i < 10; i++)
{
if(arr[i] == val)
index = i;
}

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.

PF LAB NUCES, ISLAMABAD Page 6 of 7


Repetition and Flow Control Structures LAB 07

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 # 3: Based on Example 1 above


I) Print the average value of the elements of the array arr.
II) Print the average value of the even elements of the array arr, the average of odd
values, and the average of the average of even and average of odd elements.
III) Print the smallest value of the array arr.

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.

PF LAB NUCES, ISLAMABAD Page 7 of 7

You might also like