BY GOKILA.R
Why ARRAY?
ARRAY DEFINITION
 An array is a collection of similar data elements.
These data elements have the same data type.
 The elements of the array are stored in
consecutive memory locations and are referenced
by an index (also known as the subscript).
TYPES OF ARRAYS
 One dimensional array
 Two dimensional array
 Multi dimensional array
ARRAY DECLARATION
In C, arrays are declared using the following syntax:
type name[size];
 Data type—the kind of values it can store, for example, int, char, float,
double.
 Name—to identify the array.
 Size—the maximum number of values that the array can hold.
For example,
int marks[10];//mark contains 10 elements.index starts from zero.
 dataType arrayName[arraySize];
float marks[5];
 It can hold 5 float values.
 Note:the size and type of an array cannot be changed once it is declared.
Memory representation of 10 elements
Declaring arrays of different data types and sizes
Calculating the Length of an Array
 The length of an array is given by the number of elements stored in it. The
general formula to calculate the length of an array is
Length = upper_bound – lower_bound + 1
 where upper_bound is the index of the last element and lower_bound is the
index of the first element in the array.
Example : Let Age[5] be an array of integers such that
Age[0] = 2, Age[1] = 5, Age[2] = 3, Age[3] = 1, Age[4] = 7
Show the memory representation of the array and calculate its length.
Solution
The memory representation of the array Age[5] is given as below.
Age[ 0] Age[1] Age[2] Age[3] Age[4]
Length = upper_bound – lower_bound + 1
Here, lower_bound = 0, upper_bound = 4
Therefore, length = 4 – 0 + 1 = 5
2 5 3 1 7
ARRAY INITIALIZATION
 It is possible to initialize an array during declaration.
For example,
 int mark[5] = {19, 10, 8, 17, 9};
/*You can also initialize an array like this.*/
 int mark[] = {19, 10, 8, 17, 9};
/*Here, we haven't specified the size. However, the
compiler knows its size is 5 as we are initializing it with
5 elements.*/
int mark[5] = {19, 10, 8, 17, 9};
mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
 mark[3] is equal to 17
 mark[4] is equal to 9
ARRAY INTIALIZATION
Access Array Elements
 You can access elements of an array by indices.
 Suppose you declared an array mark as above. The first
element is mark[0], the second element is mark[1] and
so on.
Note:
 Arrays have 0 as the first index, not 1. In this
example, mark[0] is the first element.
 If the size of an array is n, to access the last element,
the n-1 index is used. In this example, mark[4]
 Suppose the starting address of mark[0] is 2120d.
Then, the address of the mark[1] will be 2124d.
Similarly, the address of mark[2] will be 2128d and so
on.
This is because the size of a float is 4 bytes.
Calculating the Address of Array Elements
 Base address, that is the address of the first element in the array.
FORMULA
 Address of data element, A[k] = BA(A) + w(k – lower_bound)
 Here, A is the array, k is the index of the element of which we have to calculate the
address, BA is the base address of the array A, and w is the size of one element in
memory, for example, size of int is 2.
CHANGE VALUE OF ARRAY ELEMENTS
int mark[5] = {19, 10, 8, 17, 9}
mark[2] = -1;
// make the value of the third element to -1
mark[4] = 0;
// make the value of the fifth element to 0
scanf("%d", &mark[2]); //take input and store it in the
3rd element
scanf("%d", &mark[i-1]);/ // take input and store it in
the ith element
PRINT AN INDIVIDUAL ELEMENT OF AN ARRAY
// print the first element of the array printf("%d",
mark[0]);
// print the third element of the array printf("%d",
mark[2]);
 // print ith element of the array
printf("%d", mark[i-1]);
Program to take 5 values from the user and store them in an array
#include <stdio.h>
int main()
{
int values[5];
printf("Enter 5 integers: "); // taking input and storing it in an array
for(int i = 0; i < 5; ++i) // for loop to take 5 inputs from the user and store in array
{
scanf("%d", &values[i]);
}
printf("Displaying integers: "); // printing elements of an array
for(int i = 0; i < 5; ++i) //another for loop, these elements are displayed on the screen.
{
printf("%dn", values[i]);
}
return 0;
}
ASSIGNMENT
1. Summary of array upto you have learnt today in
introduction to c programming note with all programs.
2.Write a C program to find the sum and average of a
given numbers using array.
THANK YOU
ANY QUERIES??

Array

  • 1.
  • 2.
  • 3.
    ARRAY DEFINITION  Anarray is a collection of similar data elements. These data elements have the same data type.  The elements of the array are stored in consecutive memory locations and are referenced by an index (also known as the subscript).
  • 4.
    TYPES OF ARRAYS One dimensional array  Two dimensional array  Multi dimensional array
  • 5.
    ARRAY DECLARATION In C,arrays are declared using the following syntax: type name[size];  Data type—the kind of values it can store, for example, int, char, float, double.  Name—to identify the array.  Size—the maximum number of values that the array can hold. For example, int marks[10];//mark contains 10 elements.index starts from zero.  dataType arrayName[arraySize]; float marks[5];  It can hold 5 float values.  Note:the size and type of an array cannot be changed once it is declared.
  • 6.
  • 7.
    Declaring arrays ofdifferent data types and sizes
  • 8.
    Calculating the Lengthof an Array  The length of an array is given by the number of elements stored in it. The general formula to calculate the length of an array is Length = upper_bound – lower_bound + 1  where upper_bound is the index of the last element and lower_bound is the index of the first element in the array. Example : Let Age[5] be an array of integers such that Age[0] = 2, Age[1] = 5, Age[2] = 3, Age[3] = 1, Age[4] = 7 Show the memory representation of the array and calculate its length. Solution The memory representation of the array Age[5] is given as below. Age[ 0] Age[1] Age[2] Age[3] Age[4] Length = upper_bound – lower_bound + 1 Here, lower_bound = 0, upper_bound = 4 Therefore, length = 4 – 0 + 1 = 5 2 5 3 1 7
  • 9.
    ARRAY INITIALIZATION  Itis possible to initialize an array during declaration. For example,  int mark[5] = {19, 10, 8, 17, 9}; /*You can also initialize an array like this.*/  int mark[] = {19, 10, 8, 17, 9}; /*Here, we haven't specified the size. However, the compiler knows its size is 5 as we are initializing it with 5 elements.*/
  • 10.
    int mark[5] ={19, 10, 8, 17, 9}; mark[0] is equal to 19 mark[1] is equal to 10 mark[2] is equal to 8  mark[3] is equal to 17  mark[4] is equal to 9
  • 11.
  • 12.
    Access Array Elements You can access elements of an array by indices.  Suppose you declared an array mark as above. The first element is mark[0], the second element is mark[1] and so on.
  • 13.
    Note:  Arrays have0 as the first index, not 1. In this example, mark[0] is the first element.  If the size of an array is n, to access the last element, the n-1 index is used. In this example, mark[4]  Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so on. This is because the size of a float is 4 bytes.
  • 14.
    Calculating the Addressof Array Elements  Base address, that is the address of the first element in the array. FORMULA  Address of data element, A[k] = BA(A) + w(k – lower_bound)  Here, A is the array, k is the index of the element of which we have to calculate the address, BA is the base address of the array A, and w is the size of one element in memory, for example, size of int is 2.
  • 15.
    CHANGE VALUE OFARRAY ELEMENTS int mark[5] = {19, 10, 8, 17, 9} mark[2] = -1; // make the value of the third element to -1 mark[4] = 0; // make the value of the fifth element to 0 scanf("%d", &mark[2]); //take input and store it in the 3rd element scanf("%d", &mark[i-1]);/ // take input and store it in the ith element
  • 16.
    PRINT AN INDIVIDUALELEMENT OF AN ARRAY // print the first element of the array printf("%d", mark[0]); // print the third element of the array printf("%d", mark[2]);  // print ith element of the array printf("%d", mark[i-1]);
  • 17.
    Program to take5 values from the user and store them in an array #include <stdio.h> int main() { int values[5]; printf("Enter 5 integers: "); // taking input and storing it in an array for(int i = 0; i < 5; ++i) // for loop to take 5 inputs from the user and store in array { scanf("%d", &values[i]); } printf("Displaying integers: "); // printing elements of an array for(int i = 0; i < 5; ++i) //another for loop, these elements are displayed on the screen. { printf("%dn", values[i]); } return 0; }
  • 18.
    ASSIGNMENT 1. Summary ofarray upto you have learnt today in introduction to c programming note with all programs. 2.Write a C program to find the sum and average of a given numbers using array.
  • 19.