Array
An array is a finite set of same type of data items. In other word it is a collection of homogeneous
data items.
The elements of an array are stored in successive memory locations. An element of an array is
referred by array name and index number (subscript).
Types of array
1. One dimensional (1-D) or linear array
2. Two dimensional (2-D) array
1-D Array
An array that can be represented by only one dimension such as row or column and that holds
finite number of same type of data items is called one dimensional array.
Array B
1 2 3 4 5 6 7 8 9
0 10 12 13 19 20 23 18 29
Fig: Graphical representation of 1-D array
1, 2, …………., 9 index number
0, 10, …….., 29 data items or elements of the array
B the array name
Symbolically the element of the array is expressed as Bi or B[i], which denotes ith
element of the
array.
Store/retrieve an element into/from an array
int a[10], i, n;
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
scanf(“%d”, &a[i]);
}
for(i=1;i<=n;i++)
{
printf(“%d”, a[i]);
}
C Program to Store/retrieve an element into/from an array
#include <stdio.h>
int main()
{
int a[100], i, n;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for (i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
printf("Array elementsn");
for (i = 1; i <= n; i++)
{
printf("%dt", a[i]);
}
return 0;
}
Sample Output
Enter number of elements in array
5
Enter 5 elements
1
2
3
4
5
Array elements
1 2 3 4 5
Algorithm to search the largest element of a list
1. Input x[1….n]
2. for(i=1;i<=n;i++)
Store data to x[i];
3. large=x[1];
4. for(i=2;i<=n;i++)
if(large<x[i])
large=x[i];
5. Output: The largest number (print the value of large)
C program to search the largest element of a list
#include<stdio.h>
#include<conio.h>
int main()
{
int x[100],i,n,large;
printf("Enter the number of elements: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&x[i]);
}
large=x[1];
for(i=2;i<=n;i++)
{
if(large<x[i])
{
large=x[i];
}
}
printf("The largest among the %d elements is %d",n,large);
return 0;
}
Sample Output
Enter the number of elements: 5
12
6
7
9
0
The largest among the 5 elements is 12
Algorithm to search a particular element from a list
1. Input: A set of data in array a, and variable x. i.e., the target element
a[1….n], x;
2. found=0
3. for (i=1; i<=n; i++)
{
if (a[i]==x)
location=i;
found= 1;
break;
}
4. Output: if (found==1)
print”FOUND” message and location.
else print ”NOT FOUND” message
C program to search a particular element from a list
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in arrayn");
scanf("%d",&n);
printf("Enter %d integer(s)n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to searchn");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
printf("%d is present at location %d.n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.n", search);
return 0;
}
Sample Output
Enter the number of elements in array
6
Enter 6 integer(s)
3
4
0
2
9
7
Enter the number to search
0
0 is present at location 3.
Algorithm to find out the summations of even and odd numbers
1. Input: An array and variable (to store the results of summations)
A[1….n], sum_odd=0, sum_even=0;
2. for (i=1; i<=n; i++)
{
if(A[i]%2==0), sum_even=sum_even+A[i];
else sum_odd=sum_odd+A[i];
}
3. Output: Summationof odd numbers (print sum_odd) and
Summation of even numbers (print sum_even)
C program to find out the summations of even and odd numbers
#include <stdio.h>
int main()
{
int array[100], i, num, sum_of_odd = 0, sum_of_even = 0;
printf("Enter the number of elementsn");
scanf("%d", &num);
printf("Enter the elements n");
for(i=1;i<=num;i++)
{
scanf("%d",&array[i]);
}
for (i = 1; i <= num; i++)
{
if (array[i] % 2 == 0)
sum_of_even = sum_of_even + array[i];
else
sum_of_odd = sum_of_odd + array[i];
}
printf("Sum of all odd numbers = %dn", sum_of_odd);
printf("Sum of all even numbers = %dn", sum_of_even);
return 0;
}
Sample Output
Enter the number of elements
5
Enter the elements
1
2
5
3
9
Sum of all odd numbers = 18
Sum of all even numbers = 2
Algorithm to find out the summations of even and odd indexed numbers
1. Input: An array and variable (to store the results of summations)
A[1….n], sum_odd=0, sum_even=0;
2. for (i=1; i<=n; i++)
{
if(i%2==0), sum_even=sum_even+A[i];
else sum_odd=sum_odd+A[i];
}
3. Output: Summation of numbers in odd indices (print sum_odd) and
Summation of numbers in even indices (print sum_even)
C program to find out the summations of even and odd indexed numbers
#include <stdio.h>
int main()
{
int array[100], i, num, sum_of_odd = 0, sum_of_even = 0;
printf("Enter the number of elementsn");
scanf("%d", &num);
printf("Enter the elements n");
for(i=1;i<=num;i++)
{
scanf("%d",&array[i]);
}
for (i = 1; i <= num; i++)
{
if (i % 2 == 0)
sum_of_even = sum_of_even + array[i];
else
sum_of_odd = sum_of_odd + array[i];
}
printf("Sum of all odd number indices = %dn", sum_of_odd);
printf("Sum of all even number indices = %dn", sum_of_even);
return 0;
}
Sample Output
Enter the number of elements
5
Enter the elements
1
2
5
8
7
Sum of all odd number indices = 13
Sum of all even number indices = 10
Algorithm to insert an element into an array
1. Input: An array A[1….n], the position of insertion m and the data x.
2. Increase the size of the array, A[1….n+1]
3. for (i=n; i>=m; i--)
A[i+1]=A[i];
4. A[m]=x;
5. Output: The array, A with size n+1
C program to insert an element into an array
#include <stdio.h>
int main()
{
int array[100], position, c, n, value;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for (c = 1; c <= n; c++)
{
scanf("%d", &array[c]);
}
printf("Enter the location where you wish to insert an
elementn");
scanf("%d", &position);
printf("Enter the value to insertn");
scanf("%d", &value);
for (c = n; c >= position; c--)
{
array[c+1] = array[c];
}
array[position] = value;
printf("Resultant array isn");
for (c = 1; c <= n+1; c++)
{
printf("%dn", array[c]);
}
return 0;
}
Sample Output
Enter number of elements in array
5
Enter 5 elements
1
2
3
7
5
Enter the location where you wish to insert an element
4
Enter the value to insert
0
Resultant array is
1
2
3
0
7
5
Algorithm to delete an element from an array
1. Input: An array A[1….n], the position of deletion m.
2. for (i=m; i<n; i++)
A[i]=A[i+1];
3. Output: The updated array, A
C program to delete an element from an array
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for ( c = 1 ; c <= n ; c++ )
{
scanf("%d", &array[c]);
}
printf("Enter the location where you wish to delete
elementn");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.n");
else
{
for ( c = position ; c < n ; c++ )
{
array[c] = array[c+1];
}
printf("Resultant array isn");
for( c = 1 ; c < n ; c++ )
printf("%dn", array[c]);
}
return 0;
}
Sample Output
Enter number of elements in array
4
Enter 4 elements
8
9
0
3
Enter the location where you wish to delete element
2
Resultant array is
8
0
3

1D Array

  • 1.
    Array An array isa finite set of same type of data items. In other word it is a collection of homogeneous data items. The elements of an array are stored in successive memory locations. An element of an array is referred by array name and index number (subscript). Types of array 1. One dimensional (1-D) or linear array 2. Two dimensional (2-D) array 1-D Array An array that can be represented by only one dimension such as row or column and that holds finite number of same type of data items is called one dimensional array. Array B 1 2 3 4 5 6 7 8 9 0 10 12 13 19 20 23 18 29 Fig: Graphical representation of 1-D array 1, 2, …………., 9 index number 0, 10, …….., 29 data items or elements of the array B the array name Symbolically the element of the array is expressed as Bi or B[i], which denotes ith element of the array. Store/retrieve an element into/from an array int a[10], i, n; scanf(“%d”, &n); for(i=1;i<=n;i++) { scanf(“%d”, &a[i]); } for(i=1;i<=n;i++) { printf(“%d”, a[i]);
  • 2.
    } C Program toStore/retrieve an element into/from an array #include <stdio.h> int main() { int a[100], i, n; printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } printf("Array elementsn"); for (i = 1; i <= n; i++) { printf("%dt", a[i]); } return 0; } Sample Output Enter number of elements in array 5 Enter 5 elements 1 2 3 4
  • 3.
    5 Array elements 1 23 4 5 Algorithm to search the largest element of a list 1. Input x[1….n] 2. for(i=1;i<=n;i++) Store data to x[i]; 3. large=x[1]; 4. for(i=2;i<=n;i++) if(large<x[i]) large=x[i]; 5. Output: The largest number (print the value of large) C program to search the largest element of a list #include<stdio.h> #include<conio.h> int main() { int x[100],i,n,large; printf("Enter the number of elements: "); scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&x[i]); } large=x[1]; for(i=2;i<=n;i++) { if(large<x[i])
  • 4.
    { large=x[i]; } } printf("The largest amongthe %d elements is %d",n,large); return 0; } Sample Output Enter the number of elements: 5 12 6 7 9 0 The largest among the 5 elements is 12 Algorithm to search a particular element from a list 1. Input: A set of data in array a, and variable x. i.e., the target element a[1….n], x; 2. found=0 3. for (i=1; i<=n; i++) { if (a[i]==x) location=i; found= 1; break; } 4. Output: if (found==1) print”FOUND” message and location.
  • 5.
    else print ”NOTFOUND” message C program to search a particular element from a list #include <stdio.h> int main() { int array[100], search, c, n; printf("Enter the number of elements in arrayn"); scanf("%d",&n); printf("Enter %d integer(s)n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the number to searchn"); scanf("%d", &search); for (c = 0; c < n; c++) { if (array[c] == search) { printf("%d is present at location %d.n", search, c+1); break; } } if (c == n) printf("%d is not present in array.n", search); return 0; } Sample Output Enter the number of elements in array
  • 6.
    6 Enter 6 integer(s) 3 4 0 2 9 7 Enterthe number to search 0 0 is present at location 3. Algorithm to find out the summations of even and odd numbers 1. Input: An array and variable (to store the results of summations) A[1….n], sum_odd=0, sum_even=0; 2. for (i=1; i<=n; i++) { if(A[i]%2==0), sum_even=sum_even+A[i]; else sum_odd=sum_odd+A[i]; } 3. Output: Summationof odd numbers (print sum_odd) and Summation of even numbers (print sum_even) C program to find out the summations of even and odd numbers #include <stdio.h> int main() { int array[100], i, num, sum_of_odd = 0, sum_of_even = 0; printf("Enter the number of elementsn");
  • 7.
    scanf("%d", &num); printf("Enter theelements n"); for(i=1;i<=num;i++) { scanf("%d",&array[i]); } for (i = 1; i <= num; i++) { if (array[i] % 2 == 0) sum_of_even = sum_of_even + array[i]; else sum_of_odd = sum_of_odd + array[i]; } printf("Sum of all odd numbers = %dn", sum_of_odd); printf("Sum of all even numbers = %dn", sum_of_even); return 0; } Sample Output Enter the number of elements 5 Enter the elements 1 2 5 3 9 Sum of all odd numbers = 18 Sum of all even numbers = 2
  • 8.
    Algorithm to findout the summations of even and odd indexed numbers 1. Input: An array and variable (to store the results of summations) A[1….n], sum_odd=0, sum_even=0; 2. for (i=1; i<=n; i++) { if(i%2==0), sum_even=sum_even+A[i]; else sum_odd=sum_odd+A[i]; } 3. Output: Summation of numbers in odd indices (print sum_odd) and Summation of numbers in even indices (print sum_even) C program to find out the summations of even and odd indexed numbers #include <stdio.h> int main() { int array[100], i, num, sum_of_odd = 0, sum_of_even = 0; printf("Enter the number of elementsn"); scanf("%d", &num); printf("Enter the elements n"); for(i=1;i<=num;i++) { scanf("%d",&array[i]); } for (i = 1; i <= num; i++) { if (i % 2 == 0) sum_of_even = sum_of_even + array[i]; else sum_of_odd = sum_of_odd + array[i];
  • 9.
    } printf("Sum of allodd number indices = %dn", sum_of_odd); printf("Sum of all even number indices = %dn", sum_of_even); return 0; } Sample Output Enter the number of elements 5 Enter the elements 1 2 5 8 7 Sum of all odd number indices = 13 Sum of all even number indices = 10 Algorithm to insert an element into an array 1. Input: An array A[1….n], the position of insertion m and the data x. 2. Increase the size of the array, A[1….n+1] 3. for (i=n; i>=m; i--) A[i+1]=A[i]; 4. A[m]=x; 5. Output: The array, A with size n+1 C program to insert an element into an array #include <stdio.h> int main() { int array[100], position, c, n, value;
  • 10.
    printf("Enter number ofelements in arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for (c = 1; c <= n; c++) { scanf("%d", &array[c]); } printf("Enter the location where you wish to insert an elementn"); scanf("%d", &position); printf("Enter the value to insertn"); scanf("%d", &value); for (c = n; c >= position; c--) { array[c+1] = array[c]; } array[position] = value; printf("Resultant array isn"); for (c = 1; c <= n+1; c++) { printf("%dn", array[c]); } return 0; } Sample Output Enter number of elements in array 5 Enter 5 elements
  • 11.
    1 2 3 7 5 Enter the locationwhere you wish to insert an element 4 Enter the value to insert 0 Resultant array is 1 2 3 0 7 5 Algorithm to delete an element from an array 1. Input: An array A[1….n], the position of deletion m. 2. for (i=m; i<n; i++) A[i]=A[i+1]; 3. Output: The updated array, A C program to delete an element from an array #include <stdio.h> int main() { int array[100], position, c, n; printf("Enter number of elements in arrayn"); scanf("%d", &n);
  • 12.
    printf("Enter %d elementsn",n); for ( c = 1 ; c <= n ; c++ ) { scanf("%d", &array[c]); } printf("Enter the location where you wish to delete elementn"); scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.n"); else { for ( c = position ; c < n ; c++ ) { array[c] = array[c+1]; } printf("Resultant array isn"); for( c = 1 ; c < n ; c++ ) printf("%dn", array[c]); } return 0; } Sample Output Enter number of elements in array 4 Enter 4 elements 8 9
  • 13.
    0 3 Enter the locationwhere you wish to delete element 2 Resultant array is 8 0 3