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

Array in C-C++

The document provides an overview of arrays in C, explaining their definition, advantages, disadvantages, and how to declare and initialize them. It covers one-dimensional and multi-dimensional arrays, including examples of sorting and finding the largest elements. Additionally, it discusses the syntax for declaring two-dimensional and three-dimensional arrays along with their initialization methods.
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)
4 views

Array in C-C++

The document provides an overview of arrays in C, explaining their definition, advantages, disadvantages, and how to declare and initialize them. It covers one-dimensional and multi-dimensional arrays, including examples of sorting and finding the largest elements. Additionally, it discusses the syntax for declaring two-dimensional and three-dimensional arrays along with their initialization methods.
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/ 18

ARRAYS IN C

Arrays
 It is a group of variables of similar data types
referred by a single element.
 Its elements are stored in a contiguous memory
location.
 The size of the array should be mentioned while
declaring it.
 Array elements are always counted from zero
(0) onward.
 Array elements can be accessed using the
position of the element in the array.
 The array can have one or more dimensions.
Array

elements can be accessed randomly using indices


of an array. They can be used to store the
collection of primitive data types such as int,
float, double, char, etc of any particular type. To
add to it, an array in C/C++ can store derived
data types such as structures, pointers etc. Given
below is the picture representation of an array.
Why do we need arrays?
 We can use normal variables (v1, v2,
v3, ..) when we have a small number of
objects, but if we want to store a large
number of instances, it becomes difficult
to manage them with normal variables.
The idea of an array is to represent
many instances in one variable.
Advantages & Disadvantages

Advantages:-
 Random access of elements using the array index.
 Use of fewer lines of code as it creates a single array of multiple elements.
 Easy access to all the elements.
 Traversal through the array becomes easy using a single loop.
 Sorting becomes easy as it can be accomplished by writing fewer lines of
code.
Disadvantages:-
 Size Limit: We can store only the fixed size of elements in the array.
It doesn’t grow its size at runtime.
 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.
 Insertion and deletion of elements can be costly since the elements
are needed to be managed in accordance with the new memory
allocation.
Array declaration in C/C++:There are
various ways in which we can declare an array. It can be done by specifying its type and size, initializing it or both.
Declaration & Initialization of Array
Print marks of four students
#include <stdio.h>
int main()
{
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75; //traversal of array
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}//end of for loop
return 0;
}
C Array Example: Sorting an array
#include<stdio.h>
void main ()
{
int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n",a[i]);
}
}
Program to print the largest and second largest element of the
array.

#include<stdio.h> {
void main () if(arr[i]>largest)
{ {
sec_largest = larg
int arr[100],i,n,largest,sec_lar
gest; est;
largest = arr[i];
printf("Enter the size of the arr
}
ay?");
else if (arr[i]>sec_la
scanf("%d",&n); rgest && arr[i]!=largest)
printf("Enter the elements of t {
he array?"); sec_largest=arr[i];
for(i = 0; i<n; i++)
{ }
scanf("%d",&arr[i]); }
}
printf("largest = %d, se
cond largest = %d",larges
largest = arr[0];
t,sec_largest);
sec_largest = arr[1];
for(i=0;i<n;i++) }
Multidimensional Arrays in C / C++

 A multi-dimensional array can be termed as an


array of arrays that stores homogeneous data in
tabular form. Data in multidimensional arrays
are stored in row-major order.
 The general form of declaring N-
dimensional arrays is:
 data_type array_name[size1][size2]....
[sizeN];data_type: Type of data to be stored in
the array.
 array_name: Name of the array
 size1, size2,… ,sizeN: Sizes of the dimension
Two-Dimensional Array
 Two – dimensional array is the simplest form of a
multidimensional array. We can see a two –
dimensional array as an array of one-dimensional
array for easier understanding.
 The basic form of declaring a two-dimensional array
of size x, y:
Syntax:
 data_type array_name[x][y]; Here, data_type is
the type of data to be stored.
 We can declare a two-dimensional integer array say
‘x’ of size 10,20 as:
 int x[10][20];
 Elements in two-dimensional arrays are
commonly referred to 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 the column number ranges
from 0 to (y-1). A two – dimensional
array ‘x’ with 3 rows and 3 columns is
shown below:
 Initializing Two – Dimensional Arrays: There are
various ways in which a Two-Dimensional array can be
initialized.
 First Method:
 int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}The
above array has 3 rows and 4 columns. The elements
in the braces from left to right are stored in the table
also from left to right. The elements will be filled in the
array in order, the first 4 elements from the left in the
first row, the next 4 elements in the second row, and
so on.
 Second Method:
 int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
Three-Dimensional Array
 Initializing Three-Dimensional Array: Initialization in a Three-
Dimensional array is the same as that of Two-dimensional arrays.
The difference is as the number of dimensions increases so the
number of nested braces will also increase.
 Method 1:
 int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23};Method 2(Better):
 int x[2][3][4] = { { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} } };Accessing
elements in Three-Dimensional Arrays: Accessing elements
in Three-Dimensional Arrays is also similar to that of Two-
Dimensional Arrays. The difference is we have to use three loops
instead of two loops for one additional dimension in Three-
dimensional Arrays.
 THANK YOU

You might also like