0% found this document useful (0 votes)
12 views14 pages

3 Arrays

An array is a collection of elements of the same type stored in contiguous memory locations. Arrays allow fixed number of elements and random access of elements. Two dimensional arrays organize data into rows and columns. Character arrays can represent strings by terminating with a null character.

Uploaded by

viveknegi200345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

3 Arrays

An array is a collection of elements of the same type stored in contiguous memory locations. Arrays allow fixed number of elements and random access of elements. Two dimensional arrays organize data into rows and columns. Character arrays can represent strings by terminating with a null character.

Uploaded by

viveknegi200345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Array

• Array is a collection of same type of elements.


• Arrays a kind of data structure that can store a fixed-size sequential
collection of elements of the same type.
• All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
• The starting index of an array is always 0 (zero) by default
Advantages of an Array:
1) Elements of the array can be randomly accessed since we can calculate
the address of each element of the array with the given base address and
the size of the data element.
2) Use of less line of code as it creates a single array of multiple elements.
3) Easy access to all the elements.
4) Traversal through the array becomes easy using a single loop.
5) Sorting becomes easy as it can be accomplished by writing less line of
code.
Disadvantages of an Array:
1.Allows a fixed number of elements to be entered which is decided at the
time of declaration. Unlike a linked list, an array in C is not dynamic.
2.Insertion and deletion of elements can be costly since the elements are
needed to be managed in accordance with the new memory allocation.

1,2,3,5,4, 6,7, 8, 9 = elements


0 1 2 3 4 5 6 7 8 = index
Declaration of array:
• Syntax: data_type array_name[array_size];
data_type: it specifies the data type of array like int, float, double, char etc
array_name: it denotes the name of array use to represent it
array_size: it denotes the size of the array. The size must be an integer literal
or a constant variable. The compiler uses the size to determine how much
space to allocate (i.e. how many bytes).
• Example: int marks[5];
int marks[];
it is array mark of type integer and it can store 5 integer values.
Initialization of array:
• 1st method: The simplest way to initialize an array is by using the index of each
element.
Example: marks[0]=80; //initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
• 2nd method: int marks[5]={80,60,70,85,75};
int marks[ ] = {80, 60, 70, 85,75};
Here, we haven't specified the size. However, the compiler knows its size is 5 as we
are initializing it with 5 elements.
Accessing the array:
An element is accessed by indexing the array name. This is done by placing
the index of the element within square brackets after the name of the array.
Example: marks[2];
int mark[5] = {19, 10, 8, 17, 9};
// change 4th element to 9
mark[3] = 9;

// take input from the user and store the value at third position
cin >> mark[2];

// take input from the user insert at ith position


cin >> mark[i-1];

// print first element of the array


cout << mark[0];

// print ith element of the array


cout << mark[i-1];
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
cout << "Enter 5 numbers: " << endl;
for (int i = 0; i < 5; ++i) // store input from user to array
Output
{
Enter 5 numbers:
cin >> numbers[i];
11
} 12
13
cout << "The numbers are: ";
14
for (int n = 0; n < 5; ++n) // print array elements 15
{
The numbers are:
cout << numbers[n] << " "; 11
12
}
13
return 0; 14
15
}
Two dimensional array (2D array)
• The two-dimensional array can be defined as an array of arrays. The 2D array is
organized as matrices which can be represented as the collection of rows and columns.
• The basic form of declaring a two-dimensional array of size x, y
• Syntax: data_type array_name[x][y];
• Example: int x[10][20];
• Elements in two-dimensional arrays are commonly referred by x[i][j] where i is the row
number and ‘j’ is the column number.
• A two – dimensional array can be seen as a table with ‘x’ rows and ‘y’ columns where
the row number ranges from 0 to (x-1) and column number ranges from 0 to (y-1). A
two – dimensional array ‘x’ with 3 rows and 3 columns is shown below:
• Array representation

• Array initialization:
int x[3][3]={{1,2,3},{2,3,4},{3,4,5}};
• Accessing Elements of Two-Dimensional Arrays: Elements in Two-
Dimensional arrays are accessed using the row indexes and column indexes.
Example:
x[2][1];
// C++ Program to display all elements of an initialize two dimensional array
#include <iostream>
using namespace std;
int main()
{ test[0][0] = 2
test[0][1] = -5
int test[3][2] = {{2, -5}, {4, 0}, {9, 1}}; test[1][0] = 4
for (int i = 0; i < 3; ++i) // use of nested for loop access rows of the array test[1][1] = 0
test[2][0] = 9
{ test[2][1] = 1

for (int j = 0; j < 2; ++j) // access columns of the array


{
cout << "test[" << i << "][" << j << "] = " << test[i][j] << endl;
}
}
return 0;
}
Character array and Strings
• Declaration of character array:
char array_name[size];
Example: char city[7];
• Initialization :
Example: char city[7] = “PATIALA”;
char city[7] = {‘P’,’A’,’T’,’I’,’A’,’L’,’A’};
Strings
• Strings are defined as an array of characters. The difference between a
character array and a string is the string is terminated with a special
character ‘\0’.
• Declaration: char str_name[size];
char str_name[];
• Initialization: char str[] = “HOME”;
• Example:
String input and output
#include <iostream>
using namespace std;

int main()
{
char str[100];
cout << "Enter a string: ";
cin >> str;
cout << "You entered: " << str << endl;
cout << "\nEnter another string: ";
cin >> str;
cout << "You entered: "<<str<<endl;
return 0;
}

You might also like