1 - Page Developed By: Thilak Reddy

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

ARRAYS

Array Definition
An array can be defined as ordered list of same type of data elements. These elements may be
data type int, float, char or double. All these elements are stored in consecutive memory location.
An array is described by a single name or identifier. And each element in an array is referenced
by subscript (an index) enclosed in a pair of square brackets. This subscript indicates the position of an
individual data item in an array. The index must be positive integer.
Characteristic features of an array
1. Array is a data structure storing a group of elements, all of which are of the same data type.
2. All the elements of an array share the same name, and they are distinguished from one another
with the help of an index.
3. Random access to every element using a numeric index of an array.
Classification of array
Array are classified into two types:
1. One dimensional arrays
2. Multi dimensional array
Declaration of an array
An array must be declared before it appears in a c program. This is done using an appropriate data
type. At the same time the size of an array must be specified .This allow the compiler to decide on how
much memory has to be reserved for the given array.
Syntax of an array
data_type arrayname[size];

data type : any basic data type or a user defined data type
array name: is the name of an array
size: number of elements of type data_type. And the size must be an integer constant specified within
a pair of square brackets.

1. One dimensional array:


This is a linear list of a fixed number of data items of the same type. All these data items are
accessed using the same name and a single subscript. It is similar to row or a column matrix. It is also
called a single dimensional array or a one subscripted variable.
Example
int list[10];
char name[20];
float xyz[5];
double p[100];
Rules for subscripts
1. Each subscript must be an unsigned positive integer constant or an expression
2. Subscript of subscript is not allowed
3. The maximum subscript appearing in a program for a subscripted variable should not exceed
the declared one.
4. The subscript values ranges from 0 to one less than maximum size. If the size of an array is 10,
the first subscript is 0, the second subscript is 1 and so on the last subscript is 9.
Total size of array:-
The total amount of memory that can be allocated to a one-dimensional array is computed as,
size*[sizeof(data_type)].
Syntax for calculating total size of array:

1|Page Developed By: Thilak Reddy


Total_size=size*[sizeof (data_type)];

size is a number of elements of a one dimensional array


sizeof() is an unary operator to find the size in bytes
data_type basic data type or a user-defined data type

Example:-
#include <stdio.h>
void main()
{
int number[10];
printf("The total size of array is : %d\n",sizeof(number));
}

The total size of a one-dimensional array of 10 integers is 40 bytes. Because the size of an integer data is
4 bytes.

Initializing a one-dimensional array:-

We can initialize an array in two ways.

1. Static array initialization

static constant variable that allows you to define a constant value inside a class body.

Example

#include<stdio.h>
void main()

{
int age[5]={2,4,34,3,4};

printf("age %d",age[3]);
}

Arrays can be initialized at declaration time in this source code as:

int age[5]={2,4,34,3,4};

It is not necessary to define the size of arrays during initialization.

int age[]={2,4,34,3,4};

In this case, the compiler determines the size of array by calculating the number of elements of an
array.

2|Page Developed By: Thilak Reddy


2. Dynamic array initialization
A dynamic array lets you keep the number of elements in the array unspecified at the
declaration time. You can define the number of elements it holds during run time. Moreover,
once declared, the number of elements can be altered at a later point of time too.
Example
#include <stdio.h>
int main()
{
int marks[10],i,n,sum=0;
printf("Enter number of students: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("Enter marks of student%d: ",i+1);
scanf("%d",&marks[i]);
}
for(i=0;i<n;++i){

printf("%d\n",marks[i]);

Examples of one dimensional array:-


1. C program to find the sum marks of n students using arrays

#include <stdio.h>
int main(){
int i,n,sum=0;
printf("Enter number of students: ");
scanf("%d",&n);
int marks[n];
for(i=0;i<n;++i){
printf("Enter marks of student%d: ",i+1);
scanf("%d",&marks[i]);
sum+=marks[i];
}
printf("Sum= %d",sum);
return 0;
}
Output

Enter number of students: 3


Enter marks of student1: 12
Enter marks of student2: 31
Enter marks of student3: 2
sum=45

3|Page Developed By: Thilak Reddy


2. Source Code to Display Largest Element of an array
#include <stdio.h>
int main(){
int i,n;
printf("Enter size of an array ");
scanf("%d",&n);
int arr[n];

for(i=0;i<n;++i) /* Stores number entered by user. */


{
printf("Enter Number %d: ",i+1);
scanf("%d",&arr[i]);
}
for(i=1;i<n;++i) /* Loop to store largest number to arr[0] */
{
if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/
arr[0]=arr[i];
}
printf("Largest element = %d",arr[0]);
return 0;
}
3. C Program to Sort the Array in an Ascending Order

#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];

number[i] = number[j];

number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)

4|Page Developed By: Thilak Reddy


printf("%d\n", number[i]);
}

4. Write a C program to accept two integer arays and find the sum of the corresponding
elements of these two arrays.

#include<stdio.h>
void main()
{
int i , n;
printf(" Enter the size of Array A and B\n");
scanf("%d" , &n);
int a[n];
int b[n];
int sum[n];
printf(" Enter the elements of Array A\n");
for(i=0 ; i<n ; i++)
{
scanf("%d" , &a[i]);
}
printf(" Enter the elements of Array B\n");
for(i=0 ; i<n ; i++)
{
scanf("%d" , &b[i]);
}
for(i=0 ; i<n ; i++)
{
sum[i] = a[i] + b[i];
}
printf(" Sum of elements of A and B\n");
for(i=0 ; i<n ; i++)
{
printf("%d\n" , sum[i] );
}
}
5. This Program computes the Sum of Even and Odd Numbers stored in an Array of N

#include<stdio.h>
void main()
{
int i , even , odd , n;
printf("Enter the size of Array\n");
scanf("%d" , &n);
int num[n];
even = 0;
odd = 0;
printf(" Enter Array elements\n");
for(i=0 ; i<n ; i++)

5|Page Developed By: Thilak Reddy


{
scanf("%d" , &num[i]);
}
for(i=0 ; i<n ; i++)
{
if((num[i] %2) ==0)
{
even = even+num[i];
}
else
{
odd =odd+num[i];
}
}
printf(" Sum of even Numbers = %d\n" , even);
printf(" Sum of odd Numbers = %d\n" , odd);
}

2. Multidimensional Arrays:-
This multi dimensional arrays are classified into two dimensional, three dimensional and so on n-
dimensional array. The dimensionality of an array is determined by the number of subscript present in
the given array. If there is one subscript, then it is called a one dimensional array. If there are two
subscript, it is called a two-dimensional array and so on. The dimensionality is determined by the
number of pairs of square brackets placed after the array name
Example
1. array1[] (one dimensional array)
2. array2[][] ( Two dimensional array)
3. array3[][][] (Three dimensional array)
Two dimensional array
It is an ordered table of homogeneous elements. It is generally, referred to as a matrix of some rows
and some columns. It is also called two subscripted variable.
Syntax
Datatype arrayname [rows][columns];
Example
int marks[5][3];

This first example specifies that the marks is two dimensional array of 5 rows and 3 columns. Row
may represent students and the columns represent tests.

Student Tests Test 1 Test 2 Test 3


[0] [1] [2]
[0] 20 21 22
[1] 18 16 20
[2] 23 24 24
[3] 10 12 17
[4] 08 18 21

6|Page Developed By: Thilak Reddy


The subscript value ranges from 0 to (size-1). Therefore, the subscript value for student varies from 0
to 4(5 students). Similarly, the subscript value for test varies from 0 to 2.

marks[0][0]=20 marks[0][1]=21 marks[0][2]=22

marks[1][0]=18 marks[1][1]=16 marks[1][2]=20

marks[2][0]=23 marks[2][1]=24 marks[2][2]=24

marks[3][0]=10 marks[3][1]=12 marks[3][2]=17

marks[4][0]=08 marks[4][1]=18 marks[4][2]=21

This indicates that for each row (Students number) element, the colums elements(tests) are rapidly
processed. This type of arrangement is known as row majoring or row major order. And the column
major order is one where for each column, the row elements are rapidly processed.

Initialization of two dimensional array

Like initialization to a one dimensional array elements, the two dimensional array elements can also be
initialized at the time of declaration.

Syntax

Datatype arrayname[size1][size2]={e1,e2,e3,..en};

Example

Int matrix[3][3]={1,2,3,4,5,6,7,8,9};

matrix[0][0]=1; matrix[0][1]=2; matrix[0][2]=3;

matrix[1][0]=4; matrix[1][1]=5; matrix[1][2]=6;

matrix[2][0]=7; matrix[2][1]=8; matrix[2][2]=9;

If the number of elements to be assigned are less than the total number of elements that a two
dimensional array has contained, then all the remaining elements of an array are assigned to zeros

Points while initializing two dimensional array

1. The number of sets of initial value must be equal to the number of rows in the arrayname.

2. One to one mapping is preserved. That is the first set of initial values is assigned to the first row
element and the second set of initial value is assigned to the second row elements and so on

3.If the number of initial values in each initializing set is less than the number of the corresponding
row elements, then all the elements of that row are automatically assigned to zeros.

4. If the number of initial value in each initializing set exceeds the number of the corresponding row
elements then there will be a compilation error.

Examples of Multidimensional Array In C

7|Page Developed By: Thilak Reddy


1. Write a C program to find sum of two matrix of order 2*2 using multidimensional arrays
where, elements of matrix are entered by user.

#include <stdio.h>
int main(){
float a[2][2], b[2][2], c[2][2];
int i,j;
printf("Enter the elements of 1st matrix\n");
/* Reading two dimensional Array with the help of two for loop. If there was an array
of 'n' dimension, 'n' numbers of loops are needed for inserting data to array.*/
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){
/* Writing the elements of multidimensional array using loop. */
c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */
}
printf("\nSum Of Matrix:");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("%.1f\t",c[i][j]);
if(j==1) /* To display matrix sum in order. */
printf("\n");
}
return 0;
}

Ouput

Enter the elements of 1st matrix


Enter a11: 2;
Enter a12: 0.5;
Enter a21: -1.1;
Enter a22: 2;
Enter the elements of 2nd matrix
Enter b11: 0.2;
Enter b12: 0;
Enter b21: 0.23;
Enter b22: 23;

8|Page Developed By: Thilak Reddy


Sum Of Matrix:
2.2 0.5
-0.9 25.0

2. C Program to Add Two Matrix in C programming

#include <stdio.h>
int main(){
int r,c,a[100][100],b[100][100],sum[100][100],i,j;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d",&r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d",&c);
printf("\nEnter elements of 1st matrix:\n");

/* Storing elements of first matrix entered by user. */

for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}

/* Storing elements of second matrix entered by user. */

printf("Enter elements of 2nd matrix:\n");


for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}

/*Adding Two matrices */

for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];

/* Displaying the resultant sum matrix. */


printf("\nSum of two matrix is: \n\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",sum[i][j]);
if(j==c-1)

9|Page Developed By: Thilak Reddy


printf("\n\n");
}
return 0;
}

Output:

Enter element a12: -4


Enter element a21: 8
Enter element a22: 5
Enter element a31: 1
Enter element a32: 0
Enter elements of 2nd matrix:
Enter element a11: 4
Enter element a12: -7
Enter element a21: 9
Enter element a22: 1
Enter element a31: 4
Enter element a32: 5

Sum of two matrix is:

8 -11
17 6
5 5

3. C Program to multiply to matrix in C programming


#include <stdio.h>
int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&r2, &c2);

/* If colum of first matrix in not equal to row of second matrix, asking user to enter the
size of matrix again. */
while (c1!=r2)
{
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&r2, &c2);
}

/* Storing elements of first matrix. */

10 | P a g e Developed By: Thilak Reddy


printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}

/* Storing elements of second matrix. */


printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}

/* Initializing elements of matrix mult to 0.*/


for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
mult[i][j]=0;
}

/* Multiplying matrix a and b and storing in array mult. */


for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
mult[i][j]+=a[i][k]*b[k][j];
}

/* Displaying the multiplication of two matrix. */


printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ",mult[i][j]);
if(j==c2-1)
printf("\n\n");
}
return 0;
}

Output

Enter rows and column for first matrix: 3


2

11 | P a g e Developed By: Thilak Reddy


Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of second.

Enter rows and column for first matrix: 2


3
Enter rows and column for second matrix: 3
2

Enter elements of matrix 1:


Enter elements a11: 3
Enter elements a12: -2
Enter elements a13: 5
Enter elements a21: 3
Enter elements a22: 0
Enter elements a23: 4

Enter elements of matrix 2:


Enter elements b11: 2
Enter elements b12: 3
Enter elements b21: -9
Enter elements b22: 0
Enter elements b31: 0
Enter elements b32: 4

Output Matrix:
24 29
6 25

12 | P a g e Developed By: Thilak Reddy

You might also like