Arrays
Arrays
Arrays
-by
Ujwala Wanaskar
Contents:
• Arrays: One Dimensional Arrays, Declaration of One-dimensional
Arrays, Initialization of One- dimensional Arrays, Two –dimensional
Arrays, Initialization of Two- dimensional Arrays.
Int arr[10], n, i;
//read the number of elements in array as n
For(i=0;i<=n-1;i++)
{
Printf(“Enter array element:”);
Scanf(“%d”,&arr[i]);
}
Display Array Elements or Array Traversal:
int arr[] = { 1, 2, 3, 4, 5 };
int len = sizeof(arr)
// Traversing over arr[]
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
Searching in Array:
Int arr[10],n,I,ele,flag=0;
//read the number of elemets in array as n
//read the elemets to be stored in an array
Printf(“Enter the element to be searched in an array”);
Scanf(“%d”,&ele);
For(i=0;i<=n-1;i++)
{
if(a[i]==ele)
{
printf(“Element is found at %d position”,i+1);
flag=1;
}
}
If(flag==0)
Printf(‘Element is not found in an array”);
Finding min and max element
int arr[10],n, min,max;
//read the number of elements in array as n
//read the elements in an array
Min=max=a[0];
For(i=1;i<=n-1;i++)
{
if(a[i]<min)
min=a[i];
if(a[i]>max
max=a[i];
}
Printf(“Max elements=%d”,max);
Printf(“Min elements=%d”,min);
Finding sum and average of elements in an array
int arr[10],n, sum=0,avg;
//read the number of elements in array as n
//read the elements in an array
For(i=0;i<=n-1;i++)
{
sum=sum+a[i];
}
Avg=sum/n;
Printf(“Average of array elements=%d”,avg);
Finding frequency of an element in an array
int arr[10],n, ele,count=0;
//read the number of elements in array as n
//read the elements in an array
Printf(‘Enter the element to count the frequency”);
Scanf(“%d”,&ele);
For(i=0;i<=n-1;i++)
{
if(a[i]==ele)
count++;
}
If(count==0)
printf(“Elelment is not present”);
Else
printf(“Element is present %d times in array”,count);
Sorting array element in ascending order(Selection Sort)
int arr[10],n, i,j;
//read the number of elements in array as n
//read the elements in an array
for (i = 0; i < n; ++i)
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
Inserting an element in an array
• Insert the element at the end.
Deleting an element from an array
• Delete the last element in an array
Advantages of Array
• Arrays allow random access to elements. This makes
accessing elements by position faster.
• Arrays represent multiple data items of the same
type using a single name.
• Arrays are used to implement the other data
structures like linked lists, stacks, queues, trees,
graphs, etc.
Disadvantages of Array
• As arrays have a fixed size, once the memory is allocated to
them, it cannot be increased or decreased, making it impossible
to store extra data if required. An array of fixed size is referred to
as a static array.
• Allocating less memory than required to an array leads to loss of
data.
• An array is homogeneous in nature so, a single array cannot
store values of different data types.
• Arrays store data in contiguous memory locations, which makes
deletion and insertion very difficult to implement. This problem
is overcome by implementing linked lists, which allow elements
to be accessed sequentially.
Applications of Array
• Storing and accessing data: Arrays store elements in a
specific order and allow constant-time O(1) access to any
element.
• Searching: If data in array is sorted, we can search an item in
O(log n) time. We can also find floor(), ceiling(), kth smallest,
kth largest, etc. efficiently.
• Matrices: Two-dimensional arrays are used for matrices in
computations like graph algorithms and image processing.
• Implementing other data structures: Arrays are used as the
underlying data structure for implementing stacks and
queues.
2-D Arrays:
• Declaration of 2D array:
• Datatype arrayName[row][columns];
• Arrays are 0-indexed, so the row number ranges from 0 to (m-1) and
the column number ranges from 0 to (n-1).
2-D array initialization:
• int arr[2][3]={1,2,3,4,5,6};
• or
• int arr[2][3] = { {1, 4, 2}, {3, 6, 8} };
• The first dimension represents the number of rows [0], while the
second dimension represents the number of columns [1]. The values
are placed in row-order, and can be visualized like this:
Access the Elements of a 2D Array
• To access an element of a two-dimensional array, you must specify
the index number of both the row and column.
• This statement accesses the value of the element in the first row
(0) and third column (2) of the matrix array.
• Example
• int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
10 8 6
printf("multiply of the matrix=\n");
Matrix Multiplication
#include<stdio.h>
for(i=0;i<r;i++)
{
#include<stdlib.h> for(j=0;j<c;j++)
int main(){
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls"); mul[i][j]=0;
printf("enter the number of row="); for(k=0;k<c;k++)
scanf("%d",&r);
{
printf("enter the number of column=");
scanf("%d",&c);
mul[i][j]+=a[i][k]*b[k][j];
printf("enter the first matrix element=\n"); }
for(i=0;i<r;i++)
}
{
}
for(j=0;j<c;j++)
{ //for printing result
scanf("%d",&a[i][j]); for(i=0;i<r;i++)
}
{
}
printf("enter the second matrix element=\n"); for(j=0;j<c;j++)
for(i=0;i<r;i++) {
{
printf("%d\t",mul[i][j]);
for(j=0;j<c;j++)
{ }
scanf("%d",&b[i][j]); printf("\n");
}
}
}
return 0;
}
• enter the number of row=3
• enter the number of column=3
• enter the first matrix element=
• 111
• 222
• 333
• enter the second matrix element=
• 111
• 222
• 333
• multiply of the matrix=
• 666
• 12 12 12
• 18 18 18
Matrix Transpose: // computing the transpose
#include <stdio.h>
for (int i = 0; i < r; ++i)
int main() {
int a[10][10], transpose[10][10], r, c; for (int j = 0; j < c; ++j) {
printf("Enter rows and columns: "); transpose[j][i] = a[i][j];
scanf("%d %d", &r, &c);
// asssigning elements to the matrix
}
printf("\nEnter matrix elements:\n"); // printing the transpose
for (int i = 0; i < r; ++i)
printf("\nTranspose of the matrix:\n");
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1); for (int i = 0; i < c; ++i)
scanf("%d", &a[i][j]); for (int j = 0; j < r; ++j) {
}
// printing the matrix a[][]
printf("%d ", transpose[i][j]);
printf("\nEntered matrix: \n"); // if (j == r - 1)
for (int i = 0; i < r; ++i)
printf("\n");
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]); }
if (j == c - 1) return 0;
printf("\n");
}
}
Output
Enter rows and columns: 2
3
Entered matrix:
1 4 0
-5 2 7