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

Arrays Notes

Uploaded by

kiranc2503
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Arrays Notes

Uploaded by

kiranc2503
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 11- Arrays I PUC, Surana Ind.

PU College

Chapter-11
ARRAYS
➢ Introduction
• In everyday life we commonly group similar objects into units.
• In computer languages we also need to group together data items of the same type.
• The most basic mechanism that accomplishes this in C++ is the array.
• Arrays can hold a few data items or tens of thousands.
• The data items grouped in an array can be simple types such as int or float, or they can be user-
defined types such as structures and objects.
• Arrays exist in almost every computer language. Array in C++ are similar to those in other
languages and identical to those in C.

➢ Array Fundamentals:
• An array is collection of elements where all the elements are same data type under the same
name.
• The elements are numbered as 0, 1, 2….n-1.
• These numbers called as indices or subscripts.
• These numbers are used to locate the positions of elements within the array.
• If a is the name of the array, the elements can be directly accessed as a[0], a[1], a[2],……, a[n-1].
o a[0] is the name of the first element present at position 0.
o a[1] is the name of the second element present at position 1 and so on.
o In general, a[i] is the element present at position i.
• An array can be represented as
a 10 20 30 40 50
0 1 2 3 4
• In the above array, a is the name of the array,
o a[0] contains the element 10
o a[1] contains the element 20
o a[2] contains the element 30
o a[3] contains the element 40
o a[4] contains the element 50.
• The diagram represents a region of the memory, because the array elements are always stored in
contiguous sequence.

1|Page
Chapter 11- Arrays I PUC, Surana Ind. PU College

• The method of numbering the ith element with index i-1 is called as zero- based indexing.
• That is, the element is always same as the number of “steps“ from the initial element a[0] to that
element. For example, the element a[3] is 3 steps from the element a[0].
• In C++, the subscripts always start with 0. For example, if b is the name of the array b[0], b[1],
b[2] ......... are the elements and 0, 1, 2,… ..... are the subscripts.

➢ Types of Arrays:
• There are 3 types of arrays.
i. One-dimensional array
ii. Two-dimensional array
iii. Multi-dimensional array

✓ One –dimensional array


• It is an array in which each element is accessed by using only one subscript.
• The only one subscript represents the position of the element in array.

✓ Two-dimensional array
• It is an array in which each element is accessed using 2-subscripts.
• The subscripts represent the position of the element in the array.

✓ Multi-dimensional array
• A multi-dimensional array is an array of n-dimensions.
• In other words, an array of arrays is called a multi dimensional array.
• A one dimensional array of one dimensional array is called a two dimensional array; a one
dimensional array to two dimensional array is called three dimensional array and so on.

➢ One-Dimensional Array:
• It is an array in which each element is accessed by using only one subscript.
• The only one subscript represents the position of the element in array.
• Like other variable, the array should also be defined before it is used to store elements.
• An array can be defined by specifying the data type of the elements followed by name of the array
and the number of elements that can be stored in the array.
• The number of elements that can be stored in the array is called the size of the array.
• The size of the array must be a constant or an expression that evaluates to a constant, and should
also be an integer.
• The size should be enclosed within square brackets.

2|Page
Chapter 11- Arrays I PUC, Surana Ind. PU College

➢ Declaration of One-dimensional array:


• Syntax: data_type array_name[size];
• Example: int regno[10];
• This example defines an array marks that can store 50 elements and all the elements are of int data
type.

➢ Initialization of one-dimensional arrays:


• You can give values to each array element when array is first defined.
• Example 1: int a[5] = { 9, -5, 6, 2, 8};
• In the above example, value 9 is stored in a[0], value -5 is stored in a[1], value 6 is stored in a[2],
value 2 is stored in a[3] and value 8 is store in a[4].
a 9 -5 6 2 8
0 1 2 3 4
• If there is less number of elements specified than the size of the array, the remaining elements are
filled with 0 by the compiler.
• Example 2: float w[5] = {1.5, 2.8, 2.5};
• In the above example, value 1.5 is stored in w[0], value 2.8 is stored in w[1], value 2.5 is stored in
w[2], The elements w[3] and w[4] are initialized with 0. Thus, the initialization, the default value
is 0.
w 1.5 2.8 2.5 0 0
0 1 2 3 4
• If the array does not include an initialization, then the array elements may contain the unexpected
values called “garbage values”.
• When an array has explicit initialization, its size can be omitted from the declaration.
• Example: int a[5] = { 9, -5, 6, 2, 8}; is equivalent to int a[ ] = { 9, -5, 6, 2, 8};

➢ Accessing the elements:


• Consider the declaration, int a[5];
• We can read 5 elements into the array through the input device by writing the following program
fragment.
cout<<”Enter the elements:”;
for(i=0; i<5; i++)
cin>>a[i];
• To print the 5 elements of the above array, we write the following program fragment.
cout<<”The elements are:”;
3|Page
Chapter 11- Arrays I PUC, Surana Ind. PU College

for(i=0; i<5; i++)


cout<<a[i]<<”\t”;
Sample Program 1: To read the elements into the array and printing the elements:
#include<iostream.h>
#include<conio.h>
void main( )
{
int a[50], i, n;
clrscr( );
cout<<”Enter the size of the array:”; Output:
cin>>n; Enter the size of the array: 5
cout<<”Enter the elements:”;
for(i=0; i<n; i++) Enter the elements:
cin>>a[i]; 10 20 30 40 50
The elements are:
cout<<”The elements are:”;
10 20 30 40 50
for(i=0; i<5; i++)
cout<<a[i]<<”\t”;
getch( );
}

➢ Memory representation of one-dimensional arrays:


• The elements of one-dimensional arrays are stored in continuous memory locations.
• Example: Consider the declaration, char A[5];
• The element a[0] is allocated at a particular memory location, the element a[1] is allocated at next
memory location and so forth. Since the array is of the type char, each element requires 1-byte.
1000 1001 1002 1003 1004
A

A[0] A[1] A[2] A[3] A[4]


• Example: Consider the declaration, int a[3];
• The element a[0] is allocated at a particular memory location, the element a[1] is allocated at next
memory location and so forth. Since the array is of the type char, each element requires 2-byte.
1000 1001 1002 1003 1004 1005
A

A[0] A[1] A[2]


• The amount of storage space required to hold an array is directly related to its type and size of the
array. The total size can be calculated using the relation.
Total Size = sizeof(type) * size_of_array;

4|Page
Chapter 11- Arrays I PUC, Surana Ind. PU College

• Example:
o The total size of the char array required is 1 * 5 = 5 bytes.
o The total size of the int array required is 2 * 3 = 6 bytes.

Practical Program 17: To find the sum and average of n number of the array:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{
int a[50], i, n, sum;
float avg;
clrscr( );
cout<<"How many elements?"<<endl;
cin>>n;
cout<<"Enter the elements:";
for(i=0; i<n; i++) Output:
cin>>a[i]; How many elements? 5
sum = 0; Enter the elements:
for(i=0; i<n; i++)
sum = sum + a[i]; 10 20 30 40 50
avg=(float) sum / n; Sum = 150
cout<<"Sum = "<<sum<<endl; Average = 30
cout<<"Average = "<<avg<<endl;
getch( );
}

➢ Two -Dimensional arrays:


• It is an array in which each element is accessed using 2-subsripts.The subscripts represents
position of the elements in the array.
• The elements of two dimensional arrays are represented as rows and columns. To identify any
element, we should know its row-number and column-number.

Column-number
0 1 2 3 …. n-1
0
Row-Number

1 a[2] [3]
2 * 2 → Row Number
3 → Column Number
….
m-1

5|Page
Chapter 11- Arrays I PUC, Surana Ind. PU College
➢ Declaration of Two -Dimensional array:
• A Two-dimensional array can be defined by specifying the data type of the elements followed by
name of the array followed by the number of rows (i.e. row-size) and number of columns (i.e.
column-size)in the array.
• The rows-size and column-size of the array must be constant are expressions that evaluates to an
integer.
• The rows-size and column-size should be separately enclosed within square brackets.
• Syntax: data_type array_name[row size] [column_size];

➢ Initialization of Two-Dimensional arrays:


• Similar to the one-dimensional array, we can initialize a two-dimensional array either by
initializing it in its declaration or by using assignment statement.
• Example: int a[2][3] = { 1,2,3,4,5,6};
• Where a is a two-dimensional array which contains 2 rows and 3 columns and these assignments
would be Column - 3
a[0][0] = 1 a[0][1] = 2 a[0][2] = 3 0 1 2

Row- 2
a[1][0] = 4 a[1][1] = 5 a[1][2] = 6 0 1 2 3
• Note: If the values are missing in an initialize, they are 1 4 5 6
automatically set to 0.
• Example: int b[2][3] = { Column - 3
{ 1, 2 } 0 1 2
{3}
Row- 2

}; 0 1 2 0
• It will initialize the first two elements to the first row, the next 1 3 0 0
element to the second row. The remaining elements are automatically set 0.
b[0][0] = 1 b[0][1] = 2 b[0][2] = 0
b[1][0] = 3 b[1][1] = 0 a[1][2] = 0

➢ Accessing the elements:


• Consider the declaration int a[2][3];
• We can read 6 elements into the array, we need to use nested for loops.
• One loop processes the rows and another loop processes the columns.
• If outer loop is for rows and inner loop is for columns, then for each row index, all the columns are
processed and then the same process is repeated for next row index.
• To understand this, let us consider the following program fragment.
cout<<”enter the elements:”;
6|Page
Chapter 11- Arrays I PUC, Surana Ind. PU College
for (i=0; i<2; i++) //outer loop to control the rows
for (j=0; j<3; j++) //inner loop to the control columns
cin>>a[i][j];
• To print the above 6 elements as 2-rows and 3-columns, we write the following program fragment.
cout<<”the elements are:”<<endl;
for(i=0; i<2; i++)
{
for (j=0; j<3; j++)
cout<<a[i][j]<<”\t”; //each element is printed using 5-columns
cout<<endl;
}
Sample Program 2: To read the elements into the two-dimensional array and printing the
elements:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int a[5][5], i, j, m, n;
clrscr( ); Output:
cout<<"Enter the order:"<<endl; Enter the order: 2 3
cin>>m>>n;
Enter the elements:
cout<<"Enter the elements:"<<endl;
for(i=0; i<m; i++) 1 2 1
for(j=0; j<n; j++) 1 0 1
cin>>a[i][j];
cout<<”The array elements are:”<<endl; The elements are:
for(i=0; i<m; i++)
1 2 1
{
for(j=0; j<n; j++) 1 0 1
cout<<a[i][j]<<”\t”;
cout<<endl;
}
getch( );
}
➢ Multi -Dimensional arrays:
• A Multi-Dimensional array is an array of n-dimensions.
• An array of arrays is called a multi-dimensional array.
• Syntax: Data_type Array_name[s1][s2][s3]…..[sn];
where s1 is the size of the ith dimension.

CHAPTER 11 – ARRAYS BLUE PRINT


MCQ(1 M) VSA(1M) SA(2M) SA (3 M) LA (5 M) Total
01 Question 01 Question - 01 Question 01 Question 10 Marks

**************

7|Page

You might also like