Arrays
10/19/2024
Example for arrays
• Suppose we have to store the roll numbers of
the 100 students the we have to declare 100
variables named as roll1, roll2, roll3,…….,
roll100 which is very difficult job.
• Concept of C Programming Arrays is
introduced in C which gives the capability to
store the 100 roll numbers in the contiguous
memory which has 100 blocks and which can
be accessed by single variable name.
• Array is the Collection of Elements
• Array is collection of the Elements of the same
data type.
• All Elements are stored in the Contiguous
memory(all the elements in an array are
always stored next to each other)
• The below array is declared as int a[5];
• a[0] = 4; a[1] = 5; a[2] = 33; a[3] = 13; a[4] = 1;
• Individual data items can be accessed by the
name of the array and an integer enclosed in
square bracket called subscript variable /
index
C Array Declaration
• Array has to be declared before using it in C
Program. Array is nothing but the collection of
elements of similar data types.
• <data_type> array_name[size1][size2].....[sizen];
• What does C Array Declaration tells to Compiler ?
– Type of the Array
– Name of the Array
– Number of Dimension
– Number of Elements in Each Dimension
C Initializing 1-D Array
• Method 1 : Array Size Specified Directly
• int num[5] = {2,8,7,6,0};
• Method 2 : Size Specified Indirectly
• int num[] = {2,8,7,6,0};
Explanation :
• Compiler Counts the Number Of Elements Written
Inside Pair of Braces and Determines the Size of An
Array.
• After counting the number of elements inside the
braces, The size of array is considered as 5 during
complete execution.
• This type of Initialization Scheme is also Called as
“Compile Time Initialization“
Sample Program
#include <stdio.h>
main()
{
int num[] = {2,8,7,6,0};
int i;
for(i=0;i<5;i++)
{
printf("\nArray Element num[%d] : %d",i+1,num[i]);
}
}
Output :
Array Element num[1] : 2
Array Element num[2] : 8
Array Element num[3] : 7
Array Element num[4] : 6
Array Element num[5] : 0
Sample Program 2 : reading from
keyboard
#include <stdio.h>
main()
{
int num[5],i;
for(i=0;i<5;i++)
{
printf(“Enter element %d”, i+1);
scanf(“%d”, &num[i]);
}
for(i=0;i<5;i++)
{
printf("\nArray Element num[%d] : %d", i+1, num[i]);
}
}
C Multidimensional Array
• Array having more than one subscript variable is called
multidimensional array.
• Two Dimensional Array requires Two Subscript Variables.
• Two Dimensional Array stores the values in the form of
matrix.
• One Subscript Variable denotes the “Row” of a matrix.
Another Subscript Variable denotes the “Column” of a matrix.
Example:
int a[3][4];
for(i=0;i<row,i++)
for(j=0;j<col,j++)
{
printf("%d",a[i][j]);
}
Meaning of Two Dimensional Array :
Matrix is having 3 rows ( i takes value from 0 to 2 )
Matrix is having 4 Columns ( j takes value from 0 to 3 )
C Initializing 2D Array
Method 1 : Initializing all Elements rowwise
#include<stdio.h>
main()
{
int i, j;
int a[3][2] = { { 1, 4 }, { 5, 2 }, { 6, 5 }};
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d \t ", a[i][j]);
}
printf("\n");
}
}
Method 2 : Combine and Initializing 2D Array
#include <stdio.h>
main()
{
int i, j;
int a[3][2] = { 1, 4, 5, 2, 6, 5 };
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d \t ", a[i][j]);
}
printf("\n");
}
}
Method 3 : Some Elements could be initialized
int a[3][2] = { { 1 }, { 5 , 2 }, { 6 } };
Output :
10
52
60
/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 }
/* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 }
/* Invalid declaration – must specify second dimension*/
int abc[][] = {1, 2, 3 ,4 }
/* Invalid because of the same reason mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }
Accessing Two-Dimensional Array Elements
• An element in a two-dimensional array is accessed by using
the subscripts, i.e., row index and column index of the
array.
#include <stdio.h>
main () {
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 2; j++ )
{
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
}
Example of Multidimensional Arrays
#include <stdio.h>
main()
{
float a[2][2], b[2][2], c[2][2];
int i,j;
printf("Enter the elements of 1st matrix\n");
for(i=0;i<2;++i) {
for(j=0;j<2;++j)
{
printf("Enter a%d%d: ",i+1,j+1);
scanf("%f",&a[i][j]);
}
}
printf("Enter the elements of 2nd matrix\n");
for(i=0;i<2;++i) {
for(j=0;j<2;++j)
{
printf("Enter b%d%d: ",i+1,j+1);
scanf("%f",&b[i][j]);
}
}
for(i=0;i<2;++i) {
for(j=0;j<2;++j)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\nSum Of Matrix:");
for(i=0;i<2;++i) {
for(j=0;j<2;++j)
{
printf("%f\t",c[i][j]);
}
printf("\n");
}
}
Memory Representation
• 1-D arrays are Stored in contiguous memory
location(all the elements in an array are
always stored next to each other).
• Each element is of the type int (that is, 2 bytes
long), the array marks occupies ten contiguous
bytes in memory and these bytes are reserved
in the memory at the compile-time.
• 2-D arrays are Stored in contiguous memory location row wise.
• Consider 3×3 Array is stored in Contiguous memory location which starts
from 4000 .
• Array element a[0][0] will be stored at address 4000 again a[0][1] will be
stored to next memory location i.e Elements stored row-wise
• After Elements of First Row are stored in appropriate memory location ,
elements of next row get their corresponding memory locations.
Assignment
1. Write a C program to create an array of size 10 and
print the elements stored in the array. (For 1D as
well as 2D)
2. Write a C program to find out the average of all
numbers using 1D array.
3. Write a C program to find largest number in a 1D
array.
4. Write a C program to find the sum of all elements of
each row in a 2D array.
5. Write a C program to print the diagonal elements of
a 2D array.
Assignment
6. Write a program in C to find the transpose of a 2D
array. (To find the transpose, the rows become
columns and the columns become rows of the
resultant matrix.)