Array ….? Need of an Array.  Classification of an Array. Declaration. Access Array Elements.  Initialize an Array.
 An array is a data structure, which can store a fixed-size collection of elements of the same data type.  All these elements are stored in consecutive memory locations.  May be of type int,float,char ,double and so on….  The size should be an individual constant.  C arrays are always indexed from 0.
* We can use normal variables (v1, v2, v3, ..) when we have a small number of objects, but if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable. * We will learn the need of an array with the program . Program is at 9th and 10th slide.
 Single / One Dimensional Array (1-D Array):-  Multi Dimensional Array:- # Single Dimensional Array # single dimensional arrays are used to store a row of values. In single dimensional array, data is stored in linear form. Single dimensional arrays are also called as One-dimensional Arrays, Linear Arrays or simply 1-D Arrays.
 Datatype arrayName[arraySize];  For example:- Here, we declared an array, , of floating-point type. And its size is 5. Meaning, it can hold 5 floating-point values.  It's important to note that the size and type of an array cannot be changed once it is declared. float mark[5]; mark
 You can access elements of an array by indices.  Suppose you declared an array as above. The first element is , the second element is and so on. mark mark[0] mark[1]
Few Keynotes:- • Arrays have 0 as the first index, not 1. In this example, is the first element. • If the size of an array is , to access the last element, the index is used. In this example, • Suppose the starting addressing of is 2530d. Then, the address of the will be 2534d. Similarly, the address of will be 2538d. And so on ….. This is because the size of a float is 4 bytes. mark[0] n n-1 mark[4] mark[0] Mark[1] Mark[2]
 It is possible to initialize an array during declaration. For example,  You can also initialize an array like this.  Here, we haven't specified the size. However, the compiler knows its size is 5 as we are initializing it with 5 elements. mark[0] mark[1] mark[2] mark[3] mark[4] Here, int mark[5] = {5, 17, 22, 15, 8}; int mark[] = {5, 17, 22, 15, 8}; 5 17 22 15 8 mark[0] is equal to: 5 mark[1] is equal to: 17 mark[2] is equal to: 22 mark[3] is equal to: 15 mark[4] is equal to: 8
Que:- Program to take 5 values from the user and store them. And print the stored element. Without using array. #include <stdio.h> int main() { int a,b,c,d,e; scanf("%d",&a); scanf("%d",&b); scanf("%d",&c scanf("%d",&d); scanf("%d",&e); printf("%dn",a); printf("%dn",b); printf("%dn",c); printf("%dn",d); printf("%dn",e); return 0; } Output 1 8 34 0 3 1 8 34 0 3
Que:- Program to take 5 values from the user and store them in an array. And print the stored element. #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) { scanf("%d", &values[i]); } printf("Displaying integers: "); // printing elements of an array for(int i = 0; i < 5; ++i) { printf("%dn", values[i]); } return 0; } Output Enter 5 integers: 1 8 34 0 3 Displaying integers: 1 8 34 0 3
PPt. on An _Array in C

PPt. on An _Array in C

  • 2.
    Array ….? Need ofan Array.  Classification of an Array. Declaration. Access Array Elements.  Initialize an Array.
  • 3.
     An arrayis a data structure, which can store a fixed-size collection of elements of the same data type.  All these elements are stored in consecutive memory locations.  May be of type int,float,char ,double and so on….  The size should be an individual constant.  C arrays are always indexed from 0.
  • 4.
    * We canuse normal variables (v1, v2, v3, ..) when we have a small number of objects, but if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable. * We will learn the need of an array with the program . Program is at 9th and 10th slide.
  • 5.
     Single /One Dimensional Array (1-D Array):-  Multi Dimensional Array:- # Single Dimensional Array # single dimensional arrays are used to store a row of values. In single dimensional array, data is stored in linear form. Single dimensional arrays are also called as One-dimensional Arrays, Linear Arrays or simply 1-D Arrays.
  • 6.
     Datatype arrayName[arraySize]; For example:- Here, we declared an array, , of floating-point type. And its size is 5. Meaning, it can hold 5 floating-point values.  It's important to note that the size and type of an array cannot be changed once it is declared. float mark[5]; mark
  • 7.
     You canaccess elements of an array by indices.  Suppose you declared an array as above. The first element is , the second element is and so on. mark mark[0] mark[1]
  • 8.
    Few Keynotes:- • Arrayshave 0 as the first index, not 1. In this example, is the first element. • If the size of an array is , to access the last element, the index is used. In this example, • Suppose the starting addressing of is 2530d. Then, the address of the will be 2534d. Similarly, the address of will be 2538d. And so on ….. This is because the size of a float is 4 bytes. mark[0] n n-1 mark[4] mark[0] Mark[1] Mark[2]
  • 9.
     It ispossible to initialize an array during declaration. For example,  You can also initialize an array like this.  Here, we haven't specified the size. However, the compiler knows its size is 5 as we are initializing it with 5 elements. mark[0] mark[1] mark[2] mark[3] mark[4] Here, int mark[5] = {5, 17, 22, 15, 8}; int mark[] = {5, 17, 22, 15, 8}; 5 17 22 15 8 mark[0] is equal to: 5 mark[1] is equal to: 17 mark[2] is equal to: 22 mark[3] is equal to: 15 mark[4] is equal to: 8
  • 10.
    Que:- Program totake 5 values from the user and store them. And print the stored element. Without using array. #include <stdio.h> int main() { int a,b,c,d,e; scanf("%d",&a); scanf("%d",&b); scanf("%d",&c scanf("%d",&d); scanf("%d",&e); printf("%dn",a); printf("%dn",b); printf("%dn",c); printf("%dn",d); printf("%dn",e); return 0; } Output 1 8 34 0 3 1 8 34 0 3
  • 11.
    Que:- Program totake 5 values from the user and store them in an array. And print the stored element. #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) { scanf("%d", &values[i]); } printf("Displaying integers: "); // printing elements of an array for(int i = 0; i < 5; ++i) { printf("%dn", values[i]); } return 0; } Output Enter 5 integers: 1 8 34 0 3 Displaying integers: 1 8 34 0 3