Академический Документы
Профессиональный Документы
Культура Документы
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.
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.
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.
3) Two-dimensional array elements are used to store rows and columns data.
Types of arrays:
In c programming language, arrays are classified
into two types. They are as follows...
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 };
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.
arrayName [ indexValue ]
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} };
We use the following general syntax to access the individual elements of a two-
dimensional array...
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;
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;
}