0% found this document useful (0 votes)
36 views42 pages

Arrays

Uploaded by

akhilkc99999
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)
36 views42 pages

Arrays

Uploaded by

akhilkc99999
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/ 42

ARRAYS

Agenda
• Arrays

• Declaration

• Initialization

• 1D Arrays

• 2D Arrays

• 3D Arrays

• References
Arrays
 An array is a data structure which stores homogeneous (similar)
data items.
 An array variable is used to store more than one data item of same data
type at contiguous memory locations.
 An array is a collection of similar kind of data elements stored in adjacent
memory locations and are referred to by a single array-name.
 Arrays are defined in much the same manner as ordinary variables, except
that each array name must be accompanied by a size specification.
Arrays
 The size and type of arrays cannot be changed after its declaration.
 Consider to store marks of five students. They can be stored using five variables
as follows:
int ml,m2,m3,m4,m5;

• Now, if we want to do the same thing for 100 students in a class then one will
find it difficult to handle 100 variables.
• In such situations it is often convenient to place the data items into an array,
where they will share the same name with a subscript.
• Example:- If you want to store marks of 100 students you can create an array
for it.
int marks[100];
Advantages & Disadvantages of Array
Advantages:
• Code Optimization: Less code to the access the data.
• Easy to traverse data: By using the for loop, we can retrieve the elements of an
array easily.
• Easy to sort data: To sort the elements of array, we need only a few lines of code.
• Random Access: We can access any element randomly using the array.
Disadvantages:
• Fixed Size: Whatever size, we define at the time of declaration of array, we can't
exceed the limit. So, it doesn't grow the size dynamically like LinkedList.
Array Terminologies
 Size:Total number of elements in an array
 Type: Data type of declared array.

 Base: Address of the first element of the array.


 Index: Location or index value of array element.
 Range: Value from lower bound to upper bound.
 Word: Indicates the space required for an element.
Array Declaration
• In C, you have to declare and define array before it can be used.
• Declaration and definition tell the compiler the name of the array, the data
type of the elements, and the size or number of elements.
When array size is given:
• We can declare an array in the c language in the following way:

data-type array_name [size];


• Data-type refers to the type of elements you want to store and Size
specifies the number of elements.
• Eg:- float mark[5];
• Here, float is the data ype, mark is the array name and 5 is the size of the
array.
Array Declaration
• You can access elements of an array by indices.
• Suppose you declared an array 'mark' as float mark[5];
• The first element is mark[0], second element is mark[1] and so on.
• Arrays have 0 as the first index not 1.
• In this example, mark[0] is the first element.
• If the size of an array is n, to access the last element, [n-1] index is used.
• In this example, mark[4] is the last element.
• Suppose the starting address of mark[0] is 2120. Then, the next address,
mark[1], will be 2124, address of mark[2] will be 2128 and so on. Its because
the size of a float is 4 bytes.
Array Declaration
• Suppose you declared an array of 10 elements.
• Lets say, int testArray[10];
• You can use the array members from testArray[0] to testArray[9].
• If you try to access array elements outside of its bound, lets say testArray[12],
the compiler may not show any error.
• However, this may cause unexpected output (undefined behavior).
Array Initialization
 A simple way to initialize array is by index.
 Array index starts from 0 and ends with [size-1]
 Syntax:- datatype array-name[ size ] = {val 1, val 2, .......val n};

Eg:- a[5]= {10,20,30,40,50}


marks[5]={80,60,70,85,75}
Array Initialization
• It is possible to initialize an array during declaration.
Eg:- int mark[5] = {9,4,6,3,5};
• Another method of initialize array during declaration is
Eg:- int mark[ ] ={9,4,6,3,5};
• Here,
mark[0] = 9
mark[1] = 4
mark[2] = 6
mark[3] = 3
mark[4] = 5
• Other examples
int digits[10] = {1,2,3,4,5,6,7,8,9,10};
int digits[ ] = {1,2,3,4,5,6,7,8,9,10};
Array Initialization
 We can initialize the c array at the time of declaration.

 In such case, there is no requirement to define size. So it can also be


written as the following code.
One-dimensional Array (1D)
• Alist of item can be given one variable name using only one subscript and such a variable is called a a single
subscriptedvariableoraonedimensionalarray.

• TheSyntax for an array declaration is:

type variable-name[size];

• Example:- float height[50]; int groupt[10]; char name[10];

• The type specifies the type of the element that will be contained in the array,
such as int, float, or char and the size indicates the maximum number of
elements that can be stored inside the array.
One-dimensional Array (1D)
• Now as we declare a array int number[5];
• Then the computer reserves five storage locations as the size of the
array is 5 as show below.
• The statement, number[5]={35,20,40,57,19}; initializes and stores values
inside the array.

Reserved Space Storing values after Initialization


35
number[0] number[0]
20
40
number[1] number[1]
57
number[2] number[2] 19

number[3] number[3]
Initialization of one
dimensional array
• After an array is declared, its elements must be initialized.
• In C programming an array can be initialized at either of
the following stages:
 At compile time
 At run time
Compile Time Initialization
• The general form of initialization of array is:
• type array-name[size] ={list of values};
•The values in the list are separated by commas.
• For example: int number[3] = {0,5,4};
•This statement will declare the variable 'number' as an array of size 3 and will assign
the values to each elements.
•If the number of values in the list is less than the number of array elements, then,
only that many elements will be initialized.
•Remaining elements will be set to '0' automatically.
•If we have more initializers than the size, the compiler will produce an error.
Example Array Program 1
Example Array Program 2
Run time initialization
• An array can also be explicitly initialized at run time.
• For example consider the following segment of a c program.
for(i=0;i<10;i++)
{
scanf(“%d”, &x[i]);
}
• In the run time initialization of the arrays, looping statements are
almost compulsory.

• Looping statements are used to initialize the values of the arrays one by
one using assignment operator or through the keyboard by the user.
Example Array Program 3
Program:

#include<stdio.h>
void main()
{ Output:
int array[5]; Enter 5 elements in the array:
printf(“Enter 5 elements in the array: \n”); 23 45 32 25 45
for(i=0;i<5;i++) Elements in the array are:
{
Element stored at a[0]:23
scanf(“%d”, &array[i]);
} Element stored at a[1]:45
printf(“Elements in the array are: \n”); Element stored at a[2]:32
for(i=0;i<5;i++) Element stored at a[3]:25
{ Element stored at a[4]:45
printf(“Element stored at a[%d]=%d \n”, i, array[i]);
}
Example of One Dimensional Array Program
4
/*Write a program to get n numbers and find out sum and average of numbers*/

#include<stdio.h>

void main()
{
int a[10],i,n; //array of size 10
float avg,sum=0;
printf(“Give the values of n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Give number\n”);
scanf(“%d”,&a[i]);
sum=sum+a[i];
}
avg=sum/n;
printf(“Array elements are :\n);

printf(“\nSum=%f Average= %f\n”,sum, avg);


}
Two-dimensional Arrays
(2D)
• The simplest form of multidimensional array is two dimensional array.
• The two dimensional array in C language is represented in the form of rows and
columns, also known as matrix.
• It is also known as array of arrays or list of arrays.
• A two dimensional array is a list of one dimensional arrays.
• The two dimensional, three dimensional or other dimensional arrays are also known as
multidimensional arrays.
• Syntax of declaration of a two dimensional array
• type array-name[row_size][col_size];

Illustration of a 3 X 3 Array
Two-dimensional Arrays
(2D)
• A two-dimensional array can be considered as a table which will have m
number of rows and n number of columns.
• It can be declared as int a[m][n];
• A two-dimensional array a, which contains three rows and four columns
i.e., a[3][4], can be shown as follows.
Column 0 Column 1 Column 2 Column 3
a[0][0] a[0][1] a[0][2] a[0][3]
Row 0
a[1][0] a[1][1] a[1][2] a[1][3]
Row 1
a[2][0] a[2][1] a[2][2] a[2][3]
Row 2
Initializing Two-Dimensional Arrays

• Multidimensional arrays may be initialized by specifying bracketed values


for each row.
• Following is an array with 3 rows and each row has 4 columns.
int a[3][4] = {
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}
};
• The nested braces, which indicate the intended row, are optional.
• The following initialization is equivalent to the previous example
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Initializing Two-Dimensional Arrays
int table[2][3] = { 1,2,3,4,5,6 };

Or

int table[2][3] = {
{1,2,3},
{4,5,6} };

It means that element:


table [ 0][0] = 1;
table [ 0][1] = 2;
table [ 0][2] = 3;
table [ 1][0] = 4;
table [ 1][1] = 5;
table [ 1][2] = 6;
Accessing Two-Dimensional Array
Elements
• Every element in the array is identified by an element name of the form a[i]
[j], where ‘a’ is the name of the array, and ‘i’ and ‘j’ are the subscripts that uniquely
identify each elements in ‘a’.
• Thus, an element in a two-dimensional array is accessed by using the
subscripts. i.e., row index and column index of the array.
• For Example:- int val = a[2][3];
• The above statement will take the 4 th element from the 3 rd row of the array.
Two dimensional array example
Program1
Two-Dimensional Arrays
program 2
#include<stdio.h> Output:
int main()
{ a[0][0]: 0
int a[5][2]={{0,0}, a[0][1]: 0
{1,2},{2,4},{3,6}, a[1][0]: 1
{4,8}}; a[1][1]: 2
int i,j;
a[2][0]: 2
for(i=0;i<5;i+
+) a[2][1]: 4
{ a[3][0]: 3
for(j=0;j<2;j++) a[3][1]: 6
{ a[4][0]: 4
printf(“a[%d] a[4][1]: 8
[%d] = %d\
Two-Dimensional Arrays program 3
/*Write a program to read 3*3 matrix and find maximum
number*/

#include<stdio.h>
#include<conio.h> max=a[0][0];

void main() for(i=0;i<3;i++)


{ {
int a[3][3],i,j,max; for(j=o;j<3;j++)
clrscr(); {
printf(“Enter the if(max<a[i][j])
elements of the matrix\ max=a[i][j];
n”); }
}
for(i=0;i<3;i++)
{ printf(“Max number is = %d”,
for(j=0;j<3;j++) max);
{ getch();
printf(“a[%d][%d]= }
“,i,j);
scanf(“%d”,&a[i]
[j]);
}
printf(“\n”);
}
OUTPUT

Enter matrix row


wise
a[0][0]=3
a[0][1]=6
a[0][2]=7
a[1][0]=4
a[1][1]=5
a[1][2]=8
a[2][0]=0
a[2][1]=9
a[2][2]=2

Max number is = 9
Transpose of a Matrix Program 4
#include<stdio.h> printf(“Transpose of the matrix\n”);
for(i=0;i<c;i++)
main() { for (j=0;j<r;j++)
{ printf(“%d\
int mat[10][10],temp[10][10],r,c,i,j; t”,temp[i][j]);
printf(“Enter the rows and col of the matrix:\n”); printf(“\n”);
scanf(“%d%d”,&r,&c); }
printf(“Enter the elements of matrix”); }
OUTPUT
for(i=0;i<r;i++)
Enter the rows and col of the matrix
{
33
for (j=0;j<c;j++)
Enter the elements of the matrix
scanf(“%d”,&mat[i][j]);
} 2 3 5
for(i=0;i<r;i++) 7 8 9
{ for (j=0;j<c;j++) 6 3 1
temp[j][i]=mat[i][j];
} Transpose of the matrix
2 7 6
3 8 3
5 9 1
Sum of 2 matrices Program 5
#include<stdio.h>
{
int main() printf("\n a[%d][%d]:",i+1,j+1);
{ scanf("%d",&a[i][j]);
int r,c,a[50][50],b[50] }
[50],sum[50][50],i,j;
printf("\nEnter elements of matrix B");
printf("\t\n Multi-dimensional array");
for(i=0;i<r;++i)
printf("\n Enter the no.of rows:");
scanf("%d",&r); for(j=0;j<c;++j)
printf("\n Enter the no. of cols:"); {
scanf("%d",&c);. printf("\n b[%d][%d]:",i+1,j+1);
printf("\nEnter elements of matrix A"); scanf("%d",&b[i][j]);
for(i=0;i<r;++i) }
for(j=0;j<c;++j)
printf("\n Addition of matrix");
for(i=0;i<r; i++)
{
for(j=0;j<c;j++)
sum[i][j]=a[i][j]+b[i][j];
}
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("\t
%d",sum[i][j]);
}
return 0;
}
OUTPUT
Enter elements of matrix B
Multidimensional array a[1][1]:4
Enter the no. of rows:2 a[1][2]:8
Enter the no. of cols:2 a[2][1]:2
a[2][2]:11
Enter elements of matrix A
a[1][1]:1
Addition of matrix
a[1][2]:4
5 12
a[2][1]:6
8 14
a[2][2]:3
Matrix multiplication in C Program 6
• Matrix multiplication in C language to calculate the product of two matrices
(two-dimensional arrays).
• A user inputs the orders and elements of the matrices.
• If the multiplication isn't possible, an error message is displayed.
• No. of columns in matrix A should be equal to No. of rows in Matrix B
Program:
#include <stdio.h>
int main()
{
int m, n, p, q, i, j, k;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &first[i][j]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d", &second[i][j]);
for (i = 0; i < m; i++) printf("Product of the matrices:\n");
{ for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) for (j = 0; j < q; j++)
{ printf("%d\t", multiply[i][j]);
multiply[i][j]=0; printf("\n");
for (k = 0; k < p; k++) }
{ }
multiply[i][j]+ = first[i][k]*second[k][j]; return 0;
} }
}
printf(“\n”);
}
OUTPUT
Multi-Dimensional Arrays

• C programming language supports multidimensional Arrays.


• Multi dimensional arrays have more than one subscript variables.
• If n>=2, then we call that array as multi-dimensional array.
• Multi dimensional array is also called as matrix.
• Multi dimensional arrays are called array of arrays.
• For example, suppose we want to store 5 students marks information for
two different exams carried out in the term for 4 different subjects, this data
can be stored by using 3-dimensional array like, int marks[5][2][4];
Declaration of Multidimensional
Array
• A multidimensional array is declared using the following syntax
• Syntax: - data_type array_name[d1][d2][d3][d4]....[dn];
• Above statement will declare an array on N dimensions of name
array_name, where each element of array is of type data_type.
• The maximum number of elements that can be stored in a multi-
dimensional array array_name is size1 X size2 X size3....sizeN.
• For example:- char cube[50][60][30];
THANK YOU

You might also like