Arrays
Index
➢Introducing Arrays.
➢Declaration of a Array. Variables,
Creating Arrays.
➢The Length of Arrays.
➢Initializing Arrays.
➢Multidimensional Arrays.
Introducing Arrays
➢ Array is a fixed size sequenced collection of
elements of same data type
int a[10];
Num reference a[0]
a[1]
a[2]
a[3]
An Array of 10 Elements
a[4] of type int.
a[5]
a[6]
a[7]
a[8]
a[9]
Declaring Array Variables
➢ Syntax:
Data type array name[SIZE];
SIZE must be an integer constant
Example:
int a[10]
int list[5];
char name[30];
float marks[5];
Creating Arrays
➢ Data type array-name[size];
Example:
int num[10];
num[0]references the first element in the
array.
num[9]references the last element in the
array.
The Length of Arrays
➢ Once an array is created, its size is fixed. It
cannot be changed.
For Example,
int arr[10];
You can not insert any number to arr[11]
location because it is not initialized.
Initializing Arrays
➢ Declaring, creating, initializing in one
step:
int myArray[5] = {1, 2, 3, 4, 5};
int studentAge[4];
then
studentAge[0] = 14;
studentAge[1] = 13;
studentAge[2] = 15;
studentAge[3] = 16;
Array declaration, initialization and accessing Example
Integer array example:
Array declaration syntax: int age [5];
data_type arr_name [arr_size]; int age[5]={0, 1, 2, 3, 4};
age[0]; /*0 is accessed*/
age[1]; /*1 is accessed*/
age[2]; /*2 is accessed*/
Array initialization syntax:
data_type arr_name [arr_size]={value1, value2,
Character array example:
value3,….};
char str[10];
char str[10]={‘H’,‘a’,‘i’};
(or)
char str[0] = ‘H’;
char str[1] = ‘a’;
Array accessing syntax: char str[2] = ‘i;
str[0]; /*H is accessed*/
arr_name[index];
str[1]; /*a is accessed*/
str[2]; /*i is accessed*/
#include<stdio.h>
int main()
{
int i;
int arr[5] = {10,20,30,40,50}; // declaring and Initializing array in C
//To initialize all array elements to 0, use int arr[5]={0};
/* Above array can be initialized as below also
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50; */
for (i=0;i<5;i++)
{
// Accessing each variable
printf("value of arr[%d] is %d \n", i, arr[i]);
}
}
#include<stdio.h> Program to print largest and smallest
int main() numbers of an array
{
int a[50],i,n,large,small;
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("\nThe smallest element is %d",small);
return 0;
}
• Multidimensional Arrays
Two dimensional array is nothing but array of array.
Array declaration, initialization and accessing Example
Array declaration syntax:
data_type arr_name
[num_of_rows][num_of_column];
Integer array example:
int arr[2][2];
Array initialization syntax: int arr[2][2] = {1,2, 3, 4};
data_type arr_name[2][2] = {{0,0},{0,1}, arr [0] [0] = 1;
{1,0},{1,1}}; arr [0] ]1] = 2;
arr [1][0] = 3;
arr [1] [1] = 4;
Array accessing syntax:
arr_name[index];
int matrix[10] [10];
for (i=0; i<10; i++)
for (j=0; j<10; j++)
{
matrix[i][j] = i * j;
}
float mat[5] [5];
Multidimensional Array
Illustration
0 1 2 3 4 0 1 2 3 4
0 1 2
0 0
0 1 2 3
1 1
1 4 5 6
2 2 7
2 7 8 9
3 3
3 10 11 12
4 4
int matrix[5] [5]; int[][] array ={
matrix[2] [1] = 7
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}};
Initializing of
Multidimensional Arrays
To declare, create and initialize a
multidimensional array.
For example,
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
This is equivalent to the following statements:
array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;
array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;
array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;
array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
#include<stdio.h>
int main()
{
int i,j;
int arr[2][2] = {10,20,30,40}; // declaring and Initializing array
/* Above array can be initialized as below also
arr[0][0] = 10; // Initializing array
arr[0][1] = 20;
arr[1][0] = 30;
arr[1][1] = 40; */
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{ // Accessing variables
printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
}
}
}
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of
matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;
}
#include<stdio.h> r=i+ 1;
#include<conio.h> for(j=1 ;j<=COLS;j++)
{
#define ROWS 10 c=j;
#define COLS 10 pro [i][j]=r*c;
printf("%4d" ,pro[i] [j]);
void main()
}
{ printf("\n ");
int r,c,pro[ROWS] [COLS]; }
int i,j; getch();
}
clrscr();
printf(" Print the Table from 2 to 10\n\n\n");
printf(" ");
for(j=1 ;j<=COLS;j++)
printf("%4d" ,j);
printf( "\n");
printf(" --------------------------------------- \n ");
for(i=0;i<ROWS;i++ )
{
#include<stdio.h>
int main() {
int a[30], b[30], i, no;
printf("\nEnter no of elements :");
scanf("%d", &no);
//Accepting values into Array
printf("\nEnter the values :");
for (i = 0; i < no; i++) {
scanf("%d", &a[i]);
}
/* Copying data from array 'a' to array 'b */
for (i = 0; i < no; i++) {
b[i] = a[i];
}
//Printing of all elements of array
printf("The copied array is :");
for (i = 0; i < no; i++)
printf("\n b[%d] = %d", i, b[i]);
return (0);
}