Arrays in C_modified
Arrays in C_modified
An array in C is a collection of data items of similar data type. One or more values same data
type, which may be primary data types (int, float, char), or user-defined types such as struct or
pointers can be stored in an array.
In C, the type of elements in the array should match with the data type of the array itself.
The size of the array, also called the length of the array, must be specified in the declaration
itself. Once declared, the size of a C array cannot be changed. When an array is declared, the
compiler allocates a continuous block of memory required to store the declared number of
elements.
Declaration of an Array in C
int main(){
int arr[5];
int i;
Output :
a[0]: -133071639
a[1]: 32767
a[2]: 100
a[3]: 0
a[4]: 4096
Initialization of an Array in C
At the time of declaring an array, you can initialize it by providing the set of comma-
separated values enclosed within the curly braces {}.
Syntax:
data_type array_name [size] = {value1, value2, value3, ...};
// Initialization of an integer array
#include <stdio.h>
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
return 0;
}
Output
The compiler allocates a continuous block of memory. The size of the allocated memory depends
on the data type of the array.
Example 1: Size of Integer Array
If an integer array of 5 elements is declared, the array size in number of bytes would be
"sizeof(int) x 5"
#include <stdio.h>
int main(){
int arr[5] = {1, 2, 3, 4, 5};
printf("Size of array: %ld", sizeof(arr));
return 0;
}
Output
Size of array: 20
#include <stdio.h>
int main(){
int a[] = {1, 2, 3, 4, 5};
int i;
Output
int main(){
Output:
n[0] = 100
n[1] = 101
n[2] = 102
n[3] = 103
n[4] = 104
int main(){
int marks[10] = {50, 55, 67, 73, 45, 21, 39, 70, 49, 51};
int i, sum = 0;
float avg;
1. Two-dimensional Array
2. Three-dimensional Array or higher dimensional arrays
Two-dimensional Array in C
To increase the readability, elements of each row can be optionally put in curly
brackets as follows −
int arr[ ][5] = {
{1,2,3,4,5},
{10,20,30,40,50},
{5,10,15,20,25}
};
The numbers are logically arranged in a tabular manner as follows –
#include <stdio.h>
int main () {
return 0;
}
Output:
a[0][0] = 0
a[0][1] = 0
a[1][0] = 1
a[1][1] = 2
a[2][0] = 2
a[2][1] = 4
a[3][0] = 3
a[3][1] = 6
a[4][0] = 4
a[4][1] = 8
#include <stdio.h>
int main() {
int arr[3][5] = {1,2,3,4,5, 10,20,30,40,50, 5,10,15,20,25};
int i, j;
Output:
1 2 3 4 5
10 20 30 40 50
5 10 15 20 25
Output:
Sum of row 0: 15
Sum of row 1: 150
Sum of row 2: 75
Matrix Multiplication
Criteria : For two matrices to be multiplied, the number of columns of the first
matrix must be equal to the number of rows of the second matrix, and the resultant
matrix will be of the order r1x c2, where r1 is the number of rows of the first
matrix and c2 is the number of columns of the second matrix.
The product of two matrices is the sum of the products across some row of matrix
A with the corresponding entries down some column of matrix B.
printf("\nMatrix 2 ...\n");
for(i=0; i<3; i++){
for(j=0; j<3; j++)
printf("%d\t", mat2[i][j]);
printf("\n");
}
return 0;
}
Output:
Matrix 1 ...
2 4 1
2 3 9
3 1 8
Matrix 2 ...
1 2 3
3 6 1
2 4 7
------------------------------------------------------------------------------------------------------------
# include <stdio.h>
# include <stdlib.h>
void input(int arr[][10], int m, int n) {
int i, j;
printf("\nEnter elements of matrix:\n");
void multiply(int a[][10], int b[][10], int c[][10], int m, int n, int p, int q) {
int i, j, k;
for (i = 0; i < m; ++i) {
for (j = 0; j < q; ++j) {
c[i][j] = 0;
}
}
for (i = 0; i < m; ++i) {
for (j = 0; j < q; ++j) {
for (k = 0; k < n; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
int main(){
int m, n, p, q, i, j, k;
int a[10][10], b[10][10], res[10][10];
if (n != p) {
printf("Matrix is incompatible for multiplication\n");
} else {
printf("Enter the elements of Matrix-A:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j]);
}
}
return 0;
}