0% found this document useful (0 votes)
9 views11 pages

Arrays in C

Arrays in C++ are collections of similar data type elements that allow users to store and manipulate multiple values in a single variable. They can be declared with a fixed size and initialized in various ways, and accessing or modifying elements is done using index numbers. C++ also supports multidimensional arrays and provides sorting techniques like bubble sort, along with the use of library functions for sorting.

Uploaded by

eric.e.aghedo
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)
9 views11 pages

Arrays in C

Arrays in C++ are collections of similar data type elements that allow users to store and manipulate multiple values in a single variable. They can be declared with a fixed size and initialized in various ways, and accessing or modifying elements is done using index numbers. C++ also supports multidimensional arrays and provides sorting techniques like bubble sort, along with the use of library functions for sorting.

Uploaded by

eric.e.aghedo
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/ 11

Arrays in C++

Arrays in C++ are a collection of similar data type elements. They are one of the most versatile and
powerful data structures in C++
An array in C++ programming language is a powerful data structure that allows users to store and
manipulate a collection of elements, all of the same data type in a single variable. Simply, it is a
collection of elements of the same data type.
For example, Suppose a class has 27 students, and we need to store all their grades. Instead of
creating 27 separate variables, we can simply create an array:
double grade[27];
Here, grade is an array that can hold a maximum of 27 elements of double type.

In C++, the size and type of arrays cannot be changed after its declaration
Array Declaration in C++
dataType arrayName[arraySize];
For example, int x[6];
int - type of element to be stored
x - name of the array
6 - size of the array

Array Initialization in C++


There are various ways to do this:
i. Initialize at the time of declaration using {}.
int a[5] = {1, 2, 3, 4, 5};
ii. Initialize an array without specifying its size at declaration time.
int a[] = {1, 2, 3, 4, 5};
Though we haven't specified the size, the compiler understands the size as 5 due to the initialization
of 5 elements.
iii. Initialization by using the index of an element
int marks[5];
marks[0]=80; //the index of an array starts with 0.
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;

What if the elements initialized are less than the size of the array declared?
Example
// store only 3 elements in the array
int a[5] = {1, 2, 3};
Here an array a of size 5 is declared. We have initialized it with 3 elements only. In this case, the
compiler assigns random values to the remaining places. Many a time, this random value is 0.

Accessing Array Elements in C++


To access the array elements, use the index number of the required element. The array index starts
with 0. The index of the last element is n-1.
Remember that when you access an unavailable element in an array, the program may result in an
unexpected output (undefined behavior). Sometimes you might get an error and some other times
your program may run correctly.

Changing the Elements of an Array in C++


To change the value of a specific element, refer to the index number:
Taking Inputs from User and Store Them in an Array.
write a Program to take values of the array from the user and print the array

Here, we have used a for loop to iterate from i = 0 to i = 4. In each iteration, we took input from the
user and stored it in numbers[i].
Then, we used another for loop to print all the array elements.
Array Sorting in C++
Sorting in C++ is a concept in which the elements of an array are rearranged in a logical order. This
order can be from lowest to highest or highest to lowest. Sorting an unsorted array helps to solve
many problems such as searching for the minimum or maximum element, etc.
Arranging things in a sorted manner makes it easier to analyze and search for a particular element
among the collection of elements.
For example, In the below example, we are sorting or arranging the elements of an unsorted array in
ascending order, i.e., from lowest to highest.

Types of Sorting Techniques

There are various types of sorting techniques in C++. We will be learning the most popular ones in
this article.
 Bubble sort
 Selection sort
 Insertion sort
 Quick sort
Bubble Sort
Bubble sort is one of the most straightforward sorting algorithms. In this sorting technique, we begin
by comparing the first two elements of the array and checking if the first element is greater than the
second element; if it is, we will swap those elements and move forward to the next element.
If the first element is not greater than the second, then we don’t need to swap it. And this process will
keep on repeating till the end of the array

Exercise
Write a C++ Program to sort this array arr[8]={12,3,1,5,18,10,7,35}; using the Bubble sort algorithm
As we can see in the above example, we have an unsorted array containing elements
12, 3, 1, 5, 18, 10, 7 and 35. Two for loops are used to sort this array, the first for loop is iterating
from 0 to 8, and the second is repeating from i+1 to 8. When i is at 0, then j will be at 0+1, i.e., index
1, so the comparison will occur between the element at index1 and element 0.
Inside the for loops, there is an if statement which states that if arr[j] is less than arr[i], then swapping
will occur, i.e., if the element at index1 is less than the index 0 element, then the swapping will take
place. Similarly, one by one, each element will be compared with the next element, and it will
proceed till the end.
Sorting Using C++ Library
We can also sort using the C++ library. To use that library function, we must include the
#include<algorithm> header file.
The below function compares every element within the range.
The syntax of the function is sort; then, there will be a starting iterator and the ending iterator within
the brackets.

sort(starting iterator, ending iterator);

There are two types of array in C++, which are:


1. Single-dimensional array: It is a collection of elements of the same data typestored in a
contiguous block of memory.

2. Multi-dimensional array: It is an array that contains one or more arrays as its elements. We
will see this in the next section multidimensional arrays in C++

Multidimensional Array in C++


A multidimensional array is an array with more than one dimension. It means that it can grow in
different directions i.e. instead of changing the length only, it can also change in width, depth or
more. It is the homogeneous collection of items where each element is accessed using multiple
indices. Or an array of an array.
Syntax
Here is the given syntax for a Multidimensional array in C++:
data_type array_name[s1][s2]...[sn];
where s1, s2,…, sn is the size of each dimension.
Example of two dimensional array
int two_d[2][4];

Example of Three dimensional array


int three_d[2][4][8];
example: int x[3][4];
Here, x is a two-dimensional array. It can hold a maximum of 12 elements.
We can think of this array as a table with 3 rows and each row has 4 columns as shown below
Multidimensional Array Initialization
Like a normal array, we can initialize a multidimensional array in more than one way.
1. Initialization of two-dimensional array.
int test[2][3] = {2, 4, 5, 9, 0, 19};
The above method is not preferred. A better way to initialize this array with the same array
elements is given below:
int test[2][3] = { {2, 4, 5}, {9, 0, 19}};
This array has 2 rows and 3 columns, which is why we have two rows of elements with 3
elements each.

2. Initialization of three-dimensional array


int test[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23, 2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4,
9};

This is not a good way of initializing a three-dimensional array.


A better way to initialize this array is:
int test[2][3][4] = {
{ {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
{ {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} }
};
Example 1: Two Dimensional Array: Write a C++ Program to display all elements of an initialised
two dimensional array.

Example 2: Write a C++ Progrram to inputs for 2 dimensional array.

You might also like