BIT1C013
INTRODUCTION TO PROGRAMMING
Chapter 10
Arrays
Learning Objectives
At the end of the session, student should be able to:
▪ Describe with the concept array and its application
▪ Understand the declaration of one- and two-dimensional array
▪ Write the program for one- and two-dimensional array
Arrays
▪ An array is a collection of elements of the same type stored in
contiguous memory locations. It provides a way to store multiple
values of the same data type under a single variable name.
▪ Declaration and Initialization
▪ Arrays are declared using square brackets ([]).
▪ Element − Each item stored in an array is called an element.
▪ Index − Each location of an element in an array has a numerical
index, which is used to identify the element.
Syntax
▪ The general syntax for declaring an array is:
data_type array_name[array_size];
Example: size is the number of elements we want
to store in the array
datatype variable_name [size];
Once we declare the 1D Array, it will look
like as shown in the picture below:
int a[5];
Example:
string cars[4] = {“Volvo”, “BMW”, “Ford”, “Mazda”};
cars Volvo BMW Ford Mazda
0 1 2 3
#include <iostream> Output:
#include <string>
using namespace std; Volvo
int main()
{
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
return 0;
}
#include <iostream>
Example using namespace std;
int main()
{
// Declaration and initialization of an integer array
int numbers[5] = {1, 2, 3, 4, 5};
// Accessing and printing array elements
cout << "Array elements: ";
for (int i = 0; i < 5; i++)
{
cout << numbers[i] << " ";
}
cout << endl;
// Modifying an array element
numbers[2] = 10;
// Accessing and printing modified array elements
cout << "Modified array elements: ";
for (int i = 0; i < 5; i++)
{ Output:
cout << numbers[i] << " "; Array elements: 1 2 3 4 5
} Modified array elements: 1 2 10 4 5
cout << endl;
return 0;
}
Initialize Array
▪ There are several ways to initialize an array in C++.
▪ Here are some common methods:
▪ Initializing with values at declaration.
int numbers[5] = {1, 2, 3, 4, 5};
▪ Partial initialization at declaration.
int numbers[5] = {1, 2, 3};
Initialize Array
▪ Omitting the size during initialization.
int numbers[] = {1, 2, 3, 4, 5};
▪ Initializing with a loop.
int numbers[5];
for (int i = 0; i < 5; i++)
{
numbers[i] = i + 1;
}
Initialize Array
▪ Initializing an array with zero.
int numbers[5] = {0};
Multidimensional Arrays
▪ Multidimensional arrays in C++ are arrays of arrays. They allow you to
store data in multiple dimensions, such as rows and columns, creating
a grid-like structure.
▪ To declare a multidimensional array, you specify the size of each
dimension within the square brackets ([]).
▪ Here is an example of a 2-dimensional array:
int matrix[3][3];
▪ The above is a 2-dimensional integer array named matrix with 3
rows and 3 columns. This creates a grid with 3 rows and 3 columns,
resulting in a total of 9 elements.
Arrays as Parameters
▪ In C++, arrays can be passed as parameters to functions.
▪ When an array is passed as a parameter, it is actually passed by
reference.
▪ This means that any modifications made to the array within the
function will affect the original array outside the function.
Example #include <iostream>
using namespace std;
// Function that takes an array as a parameter and modifies its elements
void modifyArray(int arr[],
int size)
{
for (int i = 0; i < size; i++)
{
arr[i] *= 2; // Multiply each element by 2
}
}
int main()
{
int numbers[] = {1, 2, 3, 4, 5};
cout << "Original array elements: ";
for (int i = 0; i < 5; i++)
{
cout << numbers[i] << " "; Output:
}
Original array elements: 1 2 3 4 5
cout << endl;
modifyArray(numbers, 5); Modified array elements: 2 4 6 8 10
cout << "Modified array elements: ";
for (int i = 0; i < 5; i++)
{
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
Exercises
1. Write a program in array to find the sum of n numbers.
2. Write a program that can take inputs from user and store them in an array and display it.
#include <iostream>
Answer using namespace std;
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int numbers[n];
cout << "Enter " << n << " numbers: ";
for (int i = 0; i < n; i++) {
cin >> numbers[i];
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += numbers[i];
}
cout << "The sum of the " << n << " numbers is: " << sum << endl;
return 0;
}
#include <iostream>
Answer using namespace std;
int main()
{
int n;
cout << "Enter the number of elements: ";
cin >> n;
int numbers[n];
cout << "Enter " << n << " numbers: ";
for (int i = 0; i < n; i++)
{
cin >> numbers[i];
}
cout << "Array elements: ";
for (int i = 0; i < n; i++)
{
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
*** The End ***