Arrays
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.
type variable-name[size];
• 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.
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);
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
Or
int table[2][3] = {
{1,2,3},
{4,5,6} };
#include<stdio.h>
#include<conio.h> max=a[0][0];
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