UNIT - 2.2 Arrays
UNIT - 2.2 Arrays
Arrays
In C Programming
What is an array?
• An array is a variable that holds multiple
values of the same data type.
(OR)
• An array is a collection of continuous
memory locations which can store similar
data type values and all these memory
locations shares same name.
How an array is declared?
Syntax:
DataType arrayName[size];
Example:
int rollNo[60];
How an array is initialized?
Syntax:
DataType arrayName[size] = {value1,value2….,valuen};
Example:
int rollNo[5] = {1,2,3,4,5};
How an array element is assign?
❑To know how to assign a value to the
individual elements of an array first we must
know how an array is organized in memory.
How an array is organized?
Normal Variable –
int a, b, c; a b
Array Variable – c
int x[3];
x
0 1 2
index
How an array element is assign?
Syntax –
arrayName [indexValue] = value;
Example –
x [1] = 100;
Arrays are of two types:
One dimension array
for example:
□ scanf("%d",&age[2]);
/* statement to insert value in the third element
of array age[]. */
□ scanf("%d",&age[i]);
/* Statement to insert value in (i+1)th element of
array age[i]. */
/* Because, the first element of array is age[0],
second is age[1], ith is age[i-1] and (i+1)th is
age[i]. */
□ printf("%d",age[0]);
/* statement to print first element of an array.
*/
□ printf("%d",age[i]);
/* statement to print (i+1)th element of an
array. */
□ /* C program to find the sum marks of n students using
arrays */
#include <stdio.h>
int main()
{
int marks[10],i,n,sum=0;
printf("Enter number of students: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("Enter marks of student%d: ",i+1);
scanf("%d",&marks[i]);
sum+=marks[i];
}
printf("Sum= %d",sum);
return 0;
}
Output:
Enter number of students: 3
Enter marks of student1: 12
Enter marks of student2: 31
Enter marks of student3: 2
sum=45
Multi-Dimensional Array
In C programming language allows to create arrays of arrays
known as multi-dimensional arrays.
or
□ An array of arrays is called as multi dimensional array.0
□ In simple words, an array created with more than one
dimension (size) is called as multi dimensional array.
□ Multi dimensional array can be of two dimensional array
or three dimensional array or four dimensional
array or more...
□ Most popular and commonly used multi dimensional array
is two dimensional array. The 2-D arrays are used to
store data in the form of table. We also use 2-D arrays to
create mathematical matrices.
Declaration of Two Dimensional Array
Syntax:
datatype arrayname[rowsize][columnsize];
For example:1
float a[2][6];
❑ Here, a is an array of two-dimension, which is an example of
two-dimensional array. This array has 2 rows and 6 columns.
For example:2
int matrix_A [2][3] ;
□ The above declaration of two dimensional array reserves 6
continuous memory locations of 2 bytes each in the form of 2
rows and 3 columns.
□ For easy understanding of two-dimensional arrays,
array elements of above example are showed below.
Initialization of two-dimensional Arrays
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf("\nafter pass %d elements are: ",i+1);
for(k=0;k<n;k++)
printf("\t%d",a[k]); // these are optional part for understanding
movements of elements
}
printf("\nthe sorted list:");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
2. Selection sort
□ As the name suggests selection sort is the selection of an element and
keeping it in sorted order.
□ If list of elements in unsorted order and to make a list of elements in
sorted order then first take the smallest element and keep in the new list,
after that second smallest element and so on until the largest element of
list.
□ Let us take an array arr[0], arr[1]…..arr[N-1] of elements.
□ First search the position of smallest element from arr[0]….arr[N-1].
□ Then interchange that smallest element with arr[0].
□ Now search position of smallest element from arr[1]… arr[N-1],
arr[2]….arr[N-1].
□ Pass 1:
Search the smallest element from arr[0]….arr[N-1].
Interchange arr[0] with smallest element.
Result: arr[0] is sorted.
□ Pass 2:
Search the smallest element from arr[1]….arr[N-1].
Interchange arr[1] with smallest element.
Result: arr[0], arr[1] is sorted.
…..
…..
□ Pass N-1:
Search the smallest element from arr[N-2]….arr[N-1].
Interchange arr[N-2] with smallest element.
Result: arr[0]……..arr[N-1] is sorted.
Let us take list of elements in unsorted order and sort them
by applying selection sort. Pass
1) 75 35 42 13 87 24 64 57 (swa
2) 13 35 42 75 87 24 64 57 (swa
3) 13 24 42 75 87 35 64 57 (swa
4) 13 24 35 75 87 42 64 57 (swa
5) 13 24 35 42 87 75 64 57 (swa
6) 13 24 35 42 57 75 64 87 (swa
7) 13 24 35 42 57 64 75 87 final output
1. Selection Sort- example
/*Program to sorting using Selection Sort */
#include<stdio.h>
main()
{
int a[10],i,j,k,n,temp,small; printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d:",i+1); scanf("%d",&a[i]);
}
printf("Unsorted List:"\n"); for(i=0;i<n;i++)
printf("%d",a[i]);
/*Selection Sort */ for(i=0;i<n-1;i++)
{
/* Find the smallest element*/ small=i;
for(k=i+1;k<n;k++)
{
1. Selection Sort- example
if(a[small]>a[k]) small=k;
}
if(i!=small)
{
temp=a[i]; a[i]=a[small]; a[small]=temp;
}
OR
Linear
Search
Searchin
g Binary
Search
Linear Search Algorithm (Sequential Search)
0 1 2 3
65 20 10 55
List 32 12 50 99
4 5 6 7
Search element 12
step 1:
search element(12) is compared with first
element list(65)
0 1 2 3 4 5
6 2 10 5 3 1 5 9
5 0 5 2 2 0 9
6 7
List 12
□ Both are not matching .so move to the next element.
Step 2:
search element(12) is compared with first
element list(20)
List
6 2 1 5 3 1 5 9
5 0 0 5 2 2 0 9
Binary Search Algorithm
#include <stdio.h>
void main()
{ int i,j;
float a[2][2], b[2][2], c[2][2];
printf("Enter the elements of 1st matrix\n");
/* Reading two dimensional Array with the help of two for loop. If
there was an array of 'n' dimension, 'n' numbers of loops are
needed for inserting data to array.*/
for(i=0;i<2;++i)
for(j=0;j<2;++j)
{ printf("Enter a%d%d: ",i+1,j+1);
scanf("%f",&a[i][j]);
} printf("Enter the elements of 2nd matrix\n");
for(i=0;i<2;++i)
for(j=0;j<2;++j)
{
printf("Enter b%d%d: ",i+1,j+1);
scanf("%f",&b[i][j]);
}
for(i=0;i<2;++i)
for(j=0;j<2;++j)
{ /* Writing the elements of multidimensional array using loop. */
c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two
arrays. */
}
printf("\nSum Of Matrix:");
for(i=0;i<2;++i)
for(j=0;j<2;++j)
{
printf("%.1f\t",c[i][j]);
if(j==1) /* To display matrix sum in order. */
printf("\n");
}
}
Applications of Arrays in C
In c programming language, arrays are used in wide range of
applications. Few of them are as follows...
1.Arrays are used to Store List of values
□ In c programming language, single dimensional arrays are
used to store list of values of same data type. In other
words, single dimensional arrays are used to store a row
of values. In single dimensional array data is stored in
linear form.
2.Arrays are used to Perform Matrix Operations
□ We use two dimensional arrays to create matrix. We can
perform various operations on matrices using two
dimensional arrays.
3.Arrays are used to implement
Search Algorithms
#include <stdio.h>
void main()
{
int a[5]={10,20,30,40,50}; 1 2 3 4 5
clrscr(); 0 0 0 0 0
printf(“%u\n”,a);
1 1 1 1 1
printf(“%d\n”,*a); 0 0 0 0 0
printf(“%d\n”,*(a+1)); 0 2 4 6 8
printf(“%u\n”, &a[2]);
printf(“%d\n”, a[3]);
}
Output:
100
10
20
104
40
Arrays and Functions
#include<stdio.h>
#include<conio.h>
void sub(int []);
void main() a[0 a[ a[ a[ a[ a[ a[ a[ a[ a[
] 1] 2] 3] 4] 5] 6] 7] 8] 9]
{
int a[10];
printf(“read the values keyboard”);
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
sub(a);
}
void sub(int b[])
{
int i;
for(i=0;i<10;i++) a[ a[ a[ a[ a[ a[ a[ a[ a[ a[
printf(“%d”,b[i]); 0] 1] 2] 3] 4] 5] 6] 7] 8] 9]
}
Method :-2 using pointer
#include<stdio.h>
#include<conio.h>
void sub(int *);
void main()
a[ a[ a[ a[ a[ a[ a[ a[ a[ a[
{ 0] 1] 2] 3] 4] 5] 6] 7] 8] 9]
int a[10];
printf(“read the values keyboard”);
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
sub(a);
}
65 65 65 65 65 65 65 65 65 65
void sub(int *b) 52 52 52 53 53 53 53 53 54 54
{
4 6 8 0 2 4 16 8 0 2
1 2 3 4 5 6 7 8 9
0
int i; 1 2 3 4 5 6 7 8 9
a[
0
a[ a[ a[ a[ a[ a[ a[ a[ a[
for(i=0;i<10;i++) 0] 1] 2] 3] 4] 5] 6] 7] 8] 9]
printf(“%d”,*(b+i));
}
65
52
4
#include<stdio.h>
void display(int );
void main()
{
int c[]={2,3,4};
clrscr();
display(c[2]); //Passing array element c[2]
only.
}
void display(int a)
{
printf("%d",a);
}
Output: 4
Passing entire one-dimensional array to a
function
#include<stdio.h>
#include<conio.h>
void modify(int b[3]);
void main()
{
int arr[3] = {1,2,3},i;
modify(arr);
for(i=0;i<3;i++)
printf("%d",arr[i]);
getch();
}
void modify(int a[3])
{
int i;
for(i=0;i<3;i++)
a[i] = a[i]*a[i];
}
Output: 1 4 9