23ADT001 - C Programming
Module I
Topic – Arrays – Example program
23ADT001 - C PROGRAMMING
Insertion of elements
23ADT001 - C PROGRAMMING
Insertion of elements
Elements insertion in single dimensional array:
for(i=0;i<5;i++)
{
printf(“Enter the element”);
scanf(“%d”, &a[i]);
}
Elements insertion in two dimensional array:
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“Enter the element”);
scanf(“%d”, &a[i][j]);
}
23ADT001 - C PROGRAMMING
Use of Arrays
• Arrays in C act to store related data under a
single variable name with an index, also known
as a subscript.
• It is easiest to think of an array as simply a list or
ordered grouping for variables of the same type.
23ADT001 - C PROGRAMMING
Examples
• Program 1: Sum of array’s element
• Program 2: Searching the smallest value
• Program 3: Searching the biggest value
• Program 4: Searching the location for the
given value in an array
• Program 4: Sort the elements in an array in
ascending order
23ADT001 - C PROGRAMMING
23ADT001 - C PROGRAMMING
Sum of Elements in the array
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int arr[MAX_SIZE];
int i, n, sum=0;
/* Input size of the array */
printf("Enter size of the array: ");
scanf("%d", &n);
/* Input elements in array */
printf("Enter %d elements in the array: ", n);
for(i=0; i<n; i++) {
scanf("%d", &arr[i]); }
/* * Add each array element to sum */
for(i=0; i<n; i++) { sum = sum + arr[i]; }
printf("Sum of all elements of array = %d", sum);
return 0;
}
23ADT001 - C PROGRAMMING
Searching the smallest value
23ADT001 - C PROGRAMMING
Searching the location for the given value in an
array
23ADT001 - C PROGRAMMING
Sort the elements in an array in ascending
order
23ADT001 - C PROGRAMMING
Initialization
int x[4][4] = { {2, 3, 7, 2},
{7, 4, 5, 9},
{5, 1, 6, -3},
{2, 5, -1, 3}};
int x[][4] = { {2, 3, 7, 2},
{7, 4, 5, 9},
{5, 1, 6, -3},
{2, 5, -1, 3}};
23ADT001 - C PROGRAMMING
Matrix Addition
23ADT001 - C PROGRAMMING
23ADT001 - C PROGRAMMING
Summary
• Insertion of elements
• Accessing Matrix Elements
• Examples
23ADT001 - C PROGRAMMING
References
1. Stephen [Link] “Programming in C”, Fourth
edition, Addison Wesley Publishing, August 2014.
2. [Link], “Programming in ANSI C 6/e”,
Tata McGraw Hill, 2012.
23ADT001 - C PROGRAMMING
Thank You
23ADT001 - C PROGRAMMING