Вы находитесь на странице: 1из 9

Arrays in C (unit 3)

Introduction:
An array is a group (or collection) of same data types. For example an int array holds
the elements of int types while a float array holds the elements of float types.

Why we need Array in C Programming?


Consider a when you need to find out the average of 100 integer numbers.
In C, you have two ways to do this:
1) Define 100 variables of int data type and then find average
2) Use a loop to store all the entered values and calculate the average.

But in the above two situations it is time consuming and difficult to handle.
The solution to this situation is the use of arrays.

Define an Array.

An Array is a collection of similar data elements which shares a common name.


(Or)
An Array is a group of homogenous data elements.

Array of 5 students marks →

Examples of arrays:
Marks list of students in a subject→ Integer Array
Salaries of employees in an Office→ Float Array

Terminology:
The common terms associated with arrays are as follows:
Size of array → The total number of elements in the array.
Element → Each of the member in the array.
Data type → The data type of elements.
Index / sub script/ indice → The relative position of every element in the array.

Creation of an array may involve 3 steps:


1. Declaring the Array
2. Initialization of Array
3. Accessing elements of Array
Characteristics of an array:
1) An array holds elements that have the same data type.

2) Array elements are stored in subsequent memory locations.

3) Two-dimensional array elements are used to store rows and columns data.

4) Array name represents the address of the starting element.

5) Array size should be mentioned in the declaration.

6) Array size must be a constant expression and not a variable.

7) An array may be of integers, floats or Characters.

8) An array allows direct access to any element.

9) Insertion and deletion of elements is easy.

10) An array size can be of static(pre declared) or dynamic(instant declare).

Types of arrays:
In c programming language, arrays are classified
into two types. They are as follows...

1. Single Dimensional Array / 1-D Array

2. Multi Dimensional Array/ M-D Array

Single Dimensional Array


Single dimensional arrays are used to store a row of values.
In single dimensional array, data is stored in linear form (one after another).
Single dimensional arrays are also called as Linear Arrays or simply 1-D Arrays.

Multi Dimensional Array


An array of arrays is called as multi dimensional array.
An array created with more than one dimension (size) is called as multi dimensional
array. Multi dimensional array can be of 2-D,3-D .
1D arrays(Single Dimensional) :
Single dimensional arrays are used to store a row of values or a column of values.
In single dimensional array, data is stored in linear form (one after another).
Single dimensional arrays are also called as Linear Arrays or simply 1-D Arrays.

1. Declaring a 1D Array:
Like any other variable, arrays must be declared before they are used. General form
of array declaration is:
data-type variable-name[size]; e.g : int a[6]

Here int is the data type, a is the name of the array and 6 is the size of array. It
means array arr can only contain 6 elements of int type. Index of an array starts
from 0 to 5.
2. Initialization of 1D Array:
After an array is declared it must be initialized. An array can be initialized at
either compile time or at runtime.
Compile time Array initialization
Compile time initialization of array elements is same as ordinary variable
initialization. The general form of initialization of array is,
type array-name[size] = { list of values };

int marks[4]={ 67, 87, 56, 77 }; //integer array initialization

3. Accessing Elements of 1 D Array

To access the elements of single dimensional array we use array name followed by
index value of the element.
Here the index value must be enclosed in square braces. Index value of an element
is the position of element in the array .The index value starts with zero (0) for first
element and incremented by one for each element.
The index value in an array is also called as subscript or indices.

We use the following general syntax to access individual elements of single


dimensional array.

arrayName [ indexValue ]

Example : marks [2] = 99 ;


In the above statement, the third element of 'marks' array is assigned with
value '99'.

Example program on 1 D array:


// 1 d arrays
#include <stdio.h>
int main()
{
Int marks[ ]= {89, 76, 98, 91, 84}; → declaration and Initialization
int i;

printf("\n---Students marks details--- ");


for(i = 0; i < 5; i++)
{
printf("\ns%d = %d ", i + 1, marks[i]); → accessing elements
}
return 0;
}

Output:
---Students marks details---
marks 1 = 89
marks 2 = 76
marks 3 = 98
marks 4 = 91
marks 5 = 84
2D Arrays (Two dimensional):
C language supports multidimensional arrays. The simplest form of them is 2D array.
It can be used to store Row and column data.
1. Declaring a 2D Array
At compile time a Two-dimensional array is declared as follows:
Data type array-name[row-size][column-size];
e.g: : int a[3][3];

2. Initialization of a 2D Array:
After an array is declared it must be initialized. A 2D array can be initialized as
datatype arrayName [rows][colmns] = {{row1}, {row2},...{row n} };
The above array can also be declared and initialized together. Such as:
int a[3][3] = { {0,0,0},{1,1,1},{5,1,3} };

3. Accessing I Elements of 2D Array


To access elements of a two-dimensional array we use array name followed by row
index value and column index value . In case of the two-dimensional array the
compiler assigns separate index values for rows and columns.

We use the following general syntax to access the individual elements of a two-
dimensional array...

arrayName [ rowIndex ] [ columnIndex ]

Example :
matrix_A [0][1] = 10 ;

In the above statement, the element with row index 0 and column index 1
of matrix_A array is assinged with value 10.
Example program on 2 D array:

///2 d arrays
#include <stdio.h>
int main()
{
int mat [2] [2]= {{1,2},{3,4}}; //declaration and Initialization
int i,j;

printf("enter elements into matrix\n");


for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
scanf("%d",&mat[i][j]); // accessing elements
}
}

printf("elements of the matrix are\n");


for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
printf("%d ",mat[i][j]); // accessing elements
}
printf("\n");
}
return 0;
}

Output:
Program for matrix addition.

/* matrix addition*/
#include <stdio.h>
int main()
{
int m, n, c, d;
int 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;
}

Output of program:
Program for matrix subtraction.

#include <stdio.h>

int main()
{
int m, n, c, d;
int first[10][10], second[10][10], difference[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("Difference of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
difference[c][d] = first[c][d] - second[c][d];
printf("%d\t",difference[c][d]);
}
printf("\n");
}
return 0;
}

Output of program:
Program for matrix multiplication:
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first 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 number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("Matrices with entered orders can't be multiplied ");
else
{
printf("Enter the elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
} }
return 0;
}

Вам также может понравиться