Lab Manual 02 - Arrays (1D, 2D)
Lab Manual 02 - Arrays (1D, 2D)
Lahore
Lab Manual: Data Campus
Structures and Algorithm
Lab- 02 Manual
Lab Instructor: Sameer Ahmed
Department of Software Engineering
Email: [email protected]
Lab 02:
Arrays
1. Objective:
Learn how to declare, initialize and use one-dimensional arrays.
2. Scope:
The student should know the following:
1. Syntax of array declaration
2. assigning and processing and elements
3. Useful Concepts:
An array is a collection of two or more adjacent memory cells, called array elements that are
associated with a particular symbolic name. Arrays are very useful construct to store related
values together instead of declaring several variables for each value.
To set up an array in memory, we must declare both the name of the array and the number of
cells associated with it.
The following declaration will instruct the compiler to allocate eight memory cells with the
name x; these memory cells will be adjacent to each other. Each element may contain a value of
type double. double x[8];
x
0 1 2 3 4 5 6 7
As you can see; the name of the array x points to the first element of the array. We can define a
constant for array size and use it whenever we declare an array.
Lab Manual: Data Structures and Algorithm
To access the data stored in an array, we reference each individual element by specifying the
array name and identifying the element desired.
x[0] the value of the first element
x[3] the value of the fourth element
x[7] the value of the eighth element
NOTE: The indices of the elements start from zero, not from one.
Array initialization:
We can initialize an array directly by specifying each cell value individually as follows:
double values [] = {12.5,17.9,23.5,-2.5,115.75,-55.3};
Hence here we do not need to specify the size of the array or how many elements this array
should have. This number can be deduced from the initialization list.
We can also use for loop which is the common way to deal with arrays in general. The following
code initializes array square to squares of the indices:
for (i=0;i<8;++i)
square[i] = i *
i;
0 1 4 9 16 25 36 49
Lab Manual: Data Structures and Algorithm
Array Processing:
Elements of the array are dealt as normal variables, the only difference here is to specify
the index desired. These statements are examples of using array elements:
1D dynamic array
#include <iostream>
Lab Manual: Data Structures and Algorithm
int main() {
int size;
std::cout << "Enter the size of the array: ";
std::cin >> size;
// Input elements
std::cout << "Enter " << size << " elements: ";
for(int i = 0; i < size; i++) {
std::cin >> arr[i];
}
// Display elements
std::cout << "Array elements: ";
for(int i = 0; i < size; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
Searching in 1D Arrays
#include <iostream>
int main() {
int size, key;
std::cout << "Enter the size of the array: ";
std::cin >> size;
std::cout << "Enter " << size << " elements: ";
for(int i = 0; i < size; i++) {
std::cin >> arr[i];
}
return 0;
}
3. Useful Concepts:
[1]
[2]
Or
b[0][0] = 51; b[0][1] = 52; b[0][2] = 53; b[1][0] =
54; b[1][1] = 55; b[1][2] = 56;
or
int b[][3] = {{51, 52, 53}, {54, 55, 56}};
• The output of two-dimensional arrays should be in the form of rows and columns for
readability. Nested for loops are used to print the rows and columns in row and column
order.
• By increasing the index value of the array the elements stored at that index value are printed
on the output screen.
int array[10][10];
passFunc(array)
;
// Function Definition
2D dynamic Arrays
#include <iostream>
int main() {
int rows, cols;
std::cout << "Enter the number of rows and columns: ";
std::cin >> rows >> cols;
// Input elements
Lab Manual: Data Structures and Algorithm
// Display elements
std::cout << "2D Array elements:\n";
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
std::cout << arr[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
int main() {
int rows, cols, key;
std::cout << "Enter the number of rows and columns: ";
std::cin >> rows >> cols;
Lab Manual: Data Structures and Algorithm
return 0;
}
4. Task
Example: Write a program that contains an array of five elements, take five values from user,
assign the value to each element of array and display the all array elements.
int main() {
int arr[5];
}
return 0;
4: Write a program that takes an integer value from user and search it in array, If found then
display message ‘Number Found’ else ‘Number Not Found. 5: Write a Program that finds
maximum number in an array
6: Write a program that declares an array of 5 numbers. Use a loop to read 5 real numbers from
user and fill the array. Then print the following on screen:
64 36 56 47 40 54 61 60 58 64 54
48 59 45 63 54 50 49 51 60 58 59
Lab Manual: Data Structures and Algorithm
Initialize an array with above grades and find the following things about the above data.
4. Task