• Home
  • /
  • Blog
  • /
  • 1-D and 2-D Arrays Simplified For Kids

1-D and 2-D Arrays Simplified For Kids

Arrays Simplified For Kids

This post is also available in: हिन्दी (Hindi) العربية (Arabic)

What is an Array?

Arrays are around you every day whether you realize it or not. An array can be a group of biscuits in a packet, eggs or soft drink bottles kept in a crate, etc.

Arrays Simplified For Kids
Bottle Crates

Let’s see what does array in computer science means. An array means a set of objects or numbers arranged in an order, often in rows and columns. Arrays often make counting and calculating easier.

The rows are the horizontal lines that go from left to right. The columns are the vertical lines that go from top to bottom. Things that are arranged in an array create a rectangular or a square shape.

Arrays Simplified For Kids
Array of Numbers

In this article “Arrays Simplified for Kids”, we will discuss the use of arrays in computer science and their significance.

Array in Computer Science

In computer science, an array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. It is the simplest type of linear data structure. An array can hold several values of the same type (integer, float, string, etc.). Accessing elements within the array is very fast. 

An array is normally of fixed size. After the size of the array is defined at the start, it may not possible to increase the size of the array without creating a new larger array and copying all values into the new array. The array makes the process of searching and sorting easier and efficient.

Types of Arrays Based on Dimensions

An array can be classified as one dimensional (1-D) array or multi-dimensional (n-D) array. In multi-dimensional arrays, the most common is two dimensional (2-D) array.

One Dimensional Array

Conceptually you can think of a one-dimensional array as a row, where elements are stored one after another. One dimensional array uses one index to access its elements.

Arrays Simplified For Kids
One-Dimensional Array

Accessing an Element in One Dimensional Array

The elements of an array can be accessed by specifying an array name followed by subscript or index inside square brackets (i.e., []). Array subscript or index starts at 0, i.e., the first element of the one-dimensional array named ‘a’ is denoted by a[0]. If the size of an array is 10, the last element is at index 9 and is accessed as a[9]. In general, in the case of an array of size ‘n’, the last element is a[n – 1]. The first valid subscript (i.e., 0) is known as the lower bound, while the last valid subscript is known as the upper bound. (Any positive integer including 0 is a valid subscript).

Declaring One-Dimensional Array in C++: The general form to declare a one-dimensional array in C++ is

data_type array_name[array_size];

To declare an array with the name ‘a’ containing elements of integer type and of size 10 is

int a[10];

Initializing One-Dimensional Array in C++: You can assign the values to an array at the time of declaration. Such a process is called initializing an array at declaration.

The general form to initialize values to a one-dimensional array in C++ is

data_type array_name[array_size] = {comma_separated_element_list};

An example of declaring and initializing values to the array name ‘a’ of integer type data, containing 10 elements is

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Accessing Elements from One-Dimensional Array in C++

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int a[5] = {1, 2, 3, 4, 5};
	int i;
	for(i=0; i<5; i++)
	{
		cout<<"a["<<i<<"] = "<<a[i]<<"\n";
	}
}

Here is one more example of accepting 10 values in a one-dimensional array and displaying their average.

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int a[10];
	int i;
	int sum=0, avg=0;
	cout<<"Enter 10 array elements: ";
	for(i=0; i<10; i++)
	{
		cin>>a[i];
		sum = sum + a[i];
	}
	cout<<"\nThe array elements are: \n";
	for(i=0; i<10; i++)
	{
		cout<<a[i]<<"  ";
	}
	cout<<"\n\nSum of all elements is: "<<sum;
	avg = sum/10;
	cout<<"\nAnd average is: "<<avg;
}

Two Dimensional Array

Conceptually you can think of a two-dimensional array as a matrix of a fixed number of rows and columns. A two-dimensional array uses two indexes to access its elements.

Arrays Simplified For Kids
Two Dimensional Array

Accessing an Element in Two Dimensional Array: The elements of an array can be accessed by specifying an array name followed by two subscripts or indexes inside square brackets (i.e., []). Array subscript or index starts at 0 (also known as lower bound) and moves up to (n – 1) (also known as upper bound).

If ‘x’ is the array name, then x[i][j], fetches the element present at i th row and j th column. For example, x[2][1] means element at 2nd row and 1st column. (Remember, the row and column indices start from 0. Hence, x[2][1] actually means 3rd row and 2nd column).

Declaring Two-Dimensional Array in C++: The general form to declare a two-dimensional array in C++ is

data_type array_name[array_row_size][array_col_size];

To declare an array with the name ‘a’ containing elements of integer type and of row size 2 and column size 5  is

int a[2][5];

Initializing Two-Dimensional Array in C++: You can assign the values to an array at the time of declaration. Such a process is called initializing an array at declaration.

The general form to initialize values to a two-dimensional array in C++ is

data_type array_name[array_row_size][array_col_size] = {{comma_separated_element_list}};

An example of declaring and initializing values to the array name ‘a’ of integer type data, containing 10 elements in 2 rows and 5 columns is

int a[2][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};

Accessing Elements from Two-Dimensional Array in C++

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int a[2][5] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10} };
	int i, j;
	for(i=0; i<2; i++)
	{
		for(j=0; j<5; j++)
		{
			cout<<"a["<<i<<"]["<<j<<"] = "<<a[i][j]<<"\n";
		}
	}
}

Here is one more example of accepting 10 values in a two-dimensional array and displaying their average row-wise, column-wise.

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int arr[2][5];
	int i, j;
	int sum=0, avg_r=0, avg_c=0, avg=0;
	
	cout<<"Enter 2*5 array elements: ";
	for(i=0; i<2; i++)
	{
		for(j=0; j<5; j++)
		{
			cin>>arr[i][j];
		}
	}
	cout<<"\nThe array elements are: \n";
	for(i=0; i<2; i++)
	{
		for(j=0; j<5; j++)
		{
			cout<<arr[i][j]<<"   ";
		}
		cout<<"\n";
	}
	for(i=0;i<2;i++)
	{
		sum=0;
		for(j=0;j<5;j++)
		{
			sum=sum+a[i][j];
		}
		avg_r=sum/5;
		cout<<”\n Row-wise total for “ << i+1 << “ row is “ <<avg_r;
}
	for(j=0;i<5;j++)
	{
		sum=0;
		for(i=0;i<2;i++)
		{
			sum=sum+a[i][j];
		}
		avg_c=sum/5;
		cout<<”\n Column-wise total for “ << i+1 << “ row is “ <<avg_r;
}
sum=0;
for(i=0;i<2;i++)
	{
		for(j=0;j<5;j++)
		{
			sum=sum+a[i][j];
		}
	}
	avg=sum/10;
	cout<<”\n Average = “ <<avg;
}
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>