Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

5 Arrays in C

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

23CSE201

PROCEDURAL PROGRAMMING USING C


ARRAYS IN C
• Array is defined as the collection of similar type of data items stores at
contiguous memory location.
• An Array is a derived datatype in C programming Language which can store
the primitive type data such as int, float, double, char.
ARRAYS IN C
• If you want 5 numbers of integer datatype instead of declaring individual
variables, such as; int num0 , num1 , num2 , num3 , num4 ;
• You can use 1D array i,e., int num[5];
• You can store and access the values with index i.e., num[0], num[1], num[2],
num[3], num[4]. It represents individual variable.
• All Arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
ARRAY DECLARATION
• The array represents a group of similar values under a single name. The C
programming language allows user to define one dimensional array as follows;
• Syntax: Datatype ArrayName[Size];
• Example: int a[5];
• Here a is the name of array and it’s size is 5. (integer values)
ARRAY INITIALIZATION
• Initialization of array is a comma separated list of constant value which is enclosed
with in braces { }.
• An array can also initialize when they are declared, just like any other variables.
• Syntax: (Initializing 1D array at compile time)
Datatype ArrayName[size] = {List of values};
• Example: int age[5]={20,21,22,23,24};
ARRAY INITIALIZATION
• You can also initialize an array like this;
int age[ ]={20,21,22,23,24};
• Here, we have not specified the size so that the compiler knows its size is 5 as we have
initialize 5 elements in the array.
• Another way is;
int age[5]={20,24,22};
• Here we specified the size of array as 5 and assigning 3 values, Means it will initialize 0th
element of an array to 20,1st element of an array to 24 and 2nd to 22 and all other will be
initialized to 0.
INITILIZATION OF ARRAY AT RUN TIME
• An array can be explicitly initialized at run time.(just like normal variable)
• Example:
int age[5];
printf(“Enter Values:”);
for(i=0; i<5;i++)
{
scanf(“%d”, &age[i]);
}
TYPES OF ARRAY

1. Single dimensional array (1D)


2. Two dimensional array (2D)
3. Multi dimensional array (ND)
CHARACTERISTICS OF ARRAY
• An Array holds elements that have the same datatype.
• An Array elements stored in a subsequent memory location.
• Array size should be mentioned in the declaration.
• Array size must be a constant expression/value and not a variable.
• Array has homogeneous data structure. (i.e. all data elements have the same data type)
• Array allows random access to any element.
• Two-dimensional array considered as table with rows and column.
ADVANTAGES OF ARRAY
• You can use one name to store many values with different index.
• When you are working with sequences of the same kind of data, array is very useful.
• An Array program easier to read write and debug.
• An array better and convenient way of storing the data of same datatype with same
size. Interacting the array using the index is faster as compared to any other methods.
(i.e., linked list)
• It allows storing elements in any dimensional, Array supports multidimensional array.
• The searching and sorting process can be applied on arrays easily.
DISADVANTAGES OF ARRAY
• Wastage of memory space.
• Array is a static structure. It means that array is of fixed size, we cannot change size
of array at run time.
• It can store only similar type of data.
• Sometimes it is not easy to operate with many index arrays.
• Adding and removing/deleting element in array is very difficult because we need to
manage memory space too. So that array is inefficient for those applications, which
very often need insert and delete operations in between.
Example of ARRAY
#include <stdio.h>
Output:
void main()
Value of array are:
{ 20
int i; 21
int age[5] ={20,21,22,23,24}; 22
printf("Value of array are:\n"); 23
24
for (i=0; i < 5; i++)
{
printf("%d \n", age[i]);
}
}
Write a program in C to initialize one dimensional array and print all
the elements of an array
#include <stdio.h>
Output:
void main() {
Enter 5 elements:
int i,a[5]; 10 20 30 40 50
printf("Enter 5 elements: \n"); Array elements are:
a[0]=10
for (i=0; i < 5; i++) { a[1]=20
scanf("%d ", &a[i]); } a[2]=30
a[3]=40
printf("Array Elements are: \n");
a[4]=50
for (i=0; i < 5; i++) {
printf("a[%d] = %d\n",i, a[i]);
}}
ARRAY OPERATION – INSERTION
Insert means to add a ‘new element’ into an array within some specific position.

• Consider an array a[10] having 3


elements in it initially and a[0]=1,
a[1]=2 and a[2]=3 and you want to
insert a number 45 at location 0 that
is: a[0]=45, So we have to move
elements one step i.e.;
a[0]=45, a[1]=1, a[2]=2 and a[3]=3
Major limitations of insertion operation on an array are...

• All the values from index at which a new element is to be inserted to last
element are to be shifted down which is time consuming operation.
• If array is already full, then more elements cannot be added as there is no
space left at the end.
• Array insertion does not mean increasing its size. i.e., array can not
contain (11th ) element if its size is 10.
Write a program to insert an element into one dimensional array.
#include<stdio.h> scanf(“%d”,&value);
void main() { for(i=n-1; i>=position; i--) {
int a[10],position,i,n,value; a[i+1]=a[i]; }
printf(“Enter size of array: ”); a[position]=value;
scanf(“%d”,&n); n=n+1;
printf(“Enter array elements: \n”); printf(“\nResultant array is: \n”);
for(i=0; i<n; i++) { for(i=0; i<n; i++) {
scanf(“%d”,&a[i]); } printf(“a[%d] = %d\n”,i,a[i]); }
printf(“Enter the position: ”); }
scanf(“%d”,& position);
printf(“Enter the value you want to insert:”);
OUTPUT
Enter size of array: 5
Enter array elements:
10 20 30 40 50
Enter the position: 2
Enter the value want to insert: 100
Resultant array is:
a[0]=10
a[1]=20
a[2]=100
a[3]=30
a[4]=40
a[5]=50
ARRAY OPERATION – SEARCHING
• The search operation is used to find whether the desired data is present in a set
of data or not (present in array or not).
• The search operation which compares the given data with each data in an array
and if its matches with any of them it’s position will be printed otherwise "Data
not found" message will displayed.
Write a program to search a given value in a one dimensional array.
#include<stdio.h> for(i=0; i<n; i++) {
void main() { if (a[i]==s) {
int a[10], s, i, n; printf(“%d is present at location a[%d]”,s,i);
printf(“Enter size of array: ”); break;
scanf(“%d”,&n); } }
printf(“Enter array elements: ”); if(i==n) {
for(i=0; i<n; i++) { printf(“data not found”);
scanf(“%d”,&a[i]); } }
printf(“Enter a number to search: ”); }
scanf(“%d”,&s);
OUTPUT
Enter size of array: 5
Enter array elements:
10
20
30
40
50
Enter a number to search: 20
20 is present at location a[1]
ARRAY OPERATION – MERGING
• The Merging operation joins two array one after another. It combines two arrays
into large array.
Write a program to merge two one-dimensional array.
#include<stdio.h> printf(“Enter second array elements:\n”);
void main() { for(i=0; i<n2; i++) {
int a[20], b[20], i, n1, ,n2; scanf(“%d”,&b[i]); }
printf(“Enter the size of first array:\n”); n2=n1+n2;
scanf(“%d”,&n1); for(i=0; i< n2; i++) {
printf(“Enter the size of second array:\”); a[n1+i] = b[i]; }
scanf(“%d”,&n2); printf(“Merged elements are: \n”);
printf(“Enter first array elements:\n”); for(i=0; i<n2; i++) {
for(i=0; i<n1; i++) { printf(“%d ”,a[i]); }
scanf(“%d”,&a[i]); }
}
OUTPUT
Enter the size of first array : 3
Enter the size of second array: 4
Enter first array elements:
10 20 30
Enter second array elements:
40 50 60 70
Merge elements are:
10 20 30 40 50 60 70
ARRAY OPERATION – SORTING
• The sorting operation is very frequently used on arrays to arrange them in
either ascending or descending order.
• The sorting is performed by comparing and exchanging the elements of an
array if they are not in order.
Write a program to sort array in ascending order
#include<stdio.h> for(i=0; i<n; i++) {
void main() { for(j=i+1; j<n; j++) {
int a[20]; if(a[j] < a[i]) {
int i, n, j, temp; temp=a[i];
printf(“Enter the size of array:”); a[i]=a[j];
scanf(“%d”,&n); a[j]=temp;
printf(“Enter array elements:”); }}}
for(i=0; i<n; i++) { printf(“\n Array in ascending order:”);
scanf(“%d”,&a[i]); } for(i=o; i<n; i++) {
printf(“%d\n”,a[i]);
} }
OUTPUT
Enter the size of array: 3
Enter array elements:
30
10
20
Array in ascending order:
10
20
30
ARRAY OPERATION – DELETION
• Delete operation removes an element from the specified index.
• Deleting an element does not affect the size of the array.
• A user will enter the position/index at which the array element deletion is required.
• It also checks whether deletion is possible or not; for example, if an array contains five
elements and user wants to delete the element at the sixth position, it is not possible.
• In deletion we need to shift array elements which are after the elements to be deleted.
• It’s very inefficient in the size of the array is large or to remove elements from an array
repeatedly.
Write a program to delete an element from one dimensional array.
#include<stdio.h> if(index>n) {
void main() { printf("Deletion not possible\n"); }
int a[10], index, i, n; else {
printf("Enter the size of array:\n"); for(i=index; i<=n; i++) {
scanf("%d",&n); a[i]=a[i+1];
printf("Enter array elements: \n"); }
for(i=0; i<n; i++) { printf("Resultant array: \n");
scanf("%d",&a[i]); } for(i=0; i<n-1; i++) {
printf("Enter the index value : \n"); printf("%d\n",a[i]);
scanf("%d",&index); }
}}
OUTPUT
Enter the size of array:
4
Enter array element:
10 20 30 40
Enter the index value :
2
Resultant array:
10
20
40
TWO-DIMENSIONAL ARRAY
• An Array of Array is known as 2D Array.
• Two dimensional arrays are used to represent the two-dimensional structures like
matrix. In C programming 2D is also known as matrix.
• A matrix can be represented as a table of rows and columns.
• Synatx: Datatype ArrayName [row_size][column_size];
• Where the “row_size” denotes total number of rows and columns and “column_size”
denotes total number of columns.
• Declaration: int a[3][2];
• First subscript (ith) is use for total number of rows and 2nd one (jth) is use for total
number of columns in matrix.
TWO-DIMENSIONAL ARRAY
Datatype arrayname [row_size][column-size];
Example: int a[3][2];
• Here we have 3 rows and 2 columns;
INITILIZATION OF 2D ARRAY
• Initialization of 2D Array:
int a[3][2] = { {10,20}, {30,40}, {50,60} };
OR
int a[3][2] = {
{10,20},
{30,40},
{50,60}
};
OR
• The nested braces, which indicate the intended row, are optional.
int a[3][2]={10,20,30,40,50,60};
SIMPLE PROGRAM OF 2D ARRAY
#include<stdio.h>
void main()
{ Output:
int a[3][2]={10,20,30,40,50,60}; a[0][0]=10
a[0][1]=20
for(i=0; i<3; i++) a[1][0]=30
{ a[1][1]=40
for(j=0; j<2; j++) { a[2][0]=50
printf(“a[%d][%d] = %d \n” , i , j , a[i][j] ); a[2][1]=60
}
}
MATRIX ADDITION OPERATION IN C
A=

B=

C=
C=A+B
ADDITION OF TWO 3 X 3 MATRIX
#include<stdio.h> scanf(“%d”, &b[i][j]); }}
void main() { for(i=0; i<3; i++) {
int a[3][3], b[3][3], add[3][3]; for(j=0; j<3; j++) {
int i,j; add[i][j]=a[i][j]+b[i][j]; }}
printf(“Enter Matrix a:”); printf(“\nAddition of a and b is: ”);
for(i=0; i<3; i++) { for(i=0; i<3; i++) {
for(j=0; j<3; j++) { for(j=0; j<3; j++) {
scanf(“%d”, &a[i][j]); }} printf(“%d ”,add[i][j]); }
printf(“Enter Matrix b:”); printf(“\n”);
for(i=0; i<3; i++) { }
for(j=0; j<3; j++) { }
OUTPUT
Enter Matrix A:
1 2 3
4 5 6
7 8 9
Enter Matrix B:
9 8 7
6 5 4
3 2 1
Addition of matrix A and B is:
10 10 10
10 10 10
10 10 10
N-D ARRAY/ MULTI DIMENSIONAL ARRAY
• The C programming also allows the use of three or more-dimensional arrays which are
known as multi-dimensional arrays or N-D Array.
• SYNTAX: Datatype array_name[d1][d2][d3]……..[dn];
OR
Datatype array_name [size][size][size];
• EXAMPLE:
int a[3][3][3];
OR
int a[2][4][4];
EXAMPLE OF N-D ARRAY
#include<stdio.h>
void main() printf(“Multi-dimensional elements are:\n”);
{ for(i=0; i<3; i++) {
int i, j, k; for(j=0; j<3; j++) {
int a[3][3][3] = for(k=0; k<3; k++) {
{ printf(“%d ”,a[i][j][k]);
{ {1,2,3},{4,5,6}, {7,8,9}}, }
{ {10,11,12},{13,14,15},{16,17,18}}, printf(“\n”); }
{ {19,20,21}, {22,23,24},{25,26,27},} printf(“\n”); }
}; }
OUTPUT
Multi-dimensional elements are:
123
456
789

10 11 12
13 14 15
16 17 18

19 20 21
22 23 24
25 26 27
THANK YOU

You might also like