1. PREPARED BY- PRADEEP DWIVEDI(persuing B.TECH-IT) from HINDUSTAN COLLEGE OF SCIENCE AND TECHNOLOGY(MATHURA) MOB-+919027843806 E-MAIL-pradeep.it74@gmail.com C-PROGRAMMING SLIDE-5 Wednesday, September 01, 2010 1 PRADEEP DWIVWDI
2. ARRAY TOPIC Wednesday, September 01, 2010 2 PRADEEP DWIVWDI
3. ARRAY We use fundamental data type, namely char, int, float, double. Variable of these types can store only one value at a time. In many application we need to store more than one value of data in a single variable that time we use array. ARRAY:- is a fixed size sequenced collection of same data type. In other words, an ARRAY is a special variable that can hold the place for more than one values of same data type at adjacent places. Wednesday, September 01, 2010 3 PRADEEP DWIVWDI
4. EXAMPLE OF ARRAY List of name. List of number. List of students mark. etc. Wednesday, September 01, 2010 4 PRADEEP DWIVWDI
5. DECLARATION AND MEMORY REPRESENTATION OF AN ARRAY int a[10]; 0 1 2 3 4 5 6 7 8 9 Data type Declaration of an array variable dimension Memory representation Index /subscripted number Wednesday, September 01, 2010 5 PRADEEP DWIVWDI
6. TYPES OF ARRAY We have following types of array- One dimensional array. Two dimensional array. Multidimensional array. Wednesday, September 01, 2010 6 PRADEEP DWIVWDI
7. ONE DIMENSIONAL ARRAY A list of item can given one variable name using only one subscript and such a variable called single subscripted variable or a one dimensional array. For, eg- if we want to represent a set of five numbers says (10,20,30,40,50), by an array variable num, then we declare the variable num as follows- int num[5]; Wednesday, September 01, 2010 7 PRADEEP DWIVWDI
8. ONE DIMENSIONAL ARRAY The computer reserves five storage in memory- num[0] num[1] num[2] num[3] num[4] The value to the array element can be assigned as follows- num[0]=10; num[1]=20; num[2]=30; num[3]=40; num[4]=50; Wednesday, September 01, 2010 8 PRADEEP DWIVWDI
9.
10. DECLARATION OF ONE DIMENSIONAL ARRAY character array represents maximum number of characters that the string can hold. for eg, char name[10]; declare the name array(string) variable that can hold a maximum of 10 characters. suppose we want to read the following string constant into the string variable name. “WELL DONE” Wednesday, September 01, 2010 10 PRADEEP DWIVWDI
11. DECLARATION OF ONE DIMENSIONAL ARRAY each character of the string treated as an element of array name and stored in the memory as follows- NOTE:- when the compiler sees the string, it terminate with an additional null character thus, the element name[10] holds the null character ‘’. when declaring character array, we must allow one extra element space for null terminator Wednesday, September 01, 2010 11 PRADEEP DWIVWDI
12. INITIALIZATION OF ONE DIMENSIONAL ARRAY an array can be initialized at either of the following stages- at compile time. at run time. Wednesday, September 01, 2010 12 PRADEEP DWIVWDI
13. COMPILE TIME INITIALIZATION the general form of initialization of an array is- the value in the list are separated by commas- eg; int num[3]={1,1,1}; if the number of values in the list is less than the number of elements, then only that many element will be initialized , the remaining element will be set to zero automatically. eg; float total[5]={0.0,1.4,-4.7}; type array_name[size]={list of values}; Wednesday, September 01, 2010 13 PRADEEP DWIVWDI
14.
15. NOTE At the time of declaration size must be specified of an array. int a[]; If we don’t mention the size of an array at the declaration that time we must initialized it- int a[]={10,20,30,40}; int a[4]={10,20,30,40, 50,60,70}; only take first four values. Wednesday, September 01, 2010 15 PRADEEP DWIVWDI incorrect
16. NOTE Wednesday, September 01, 2010 PRADEEP DWIVWDI 16 In character array null character placed after the string char[8]; Suppose we want to store-PRADEEP char a[20];
17. RUN TIME INITIALIZATION Wednesday, September 01, 2010 PRADEEP DWIVWDI 17 an array can be explicitly initialized at run time . this approach is usually applied for initializing large arrays. for eg; for(i=0;i<100;i++) { if(i<50) sum[i]=0.0; else sum[i]=1.0; }
18. prog23 //Demo for array #include<stdio.h> #include<conio.h> void main() { int num[5],i; clrscr(); printf("Enter five numbers:"); for(i=0;i<5;i++) { scanf("%d",&num[i]); } printf("The element of array:-"); for(i=0;i<5;i++) { printf("%d",num[i]); } getch(); } Wednesday, September 01, 2010 18 PRADEEP DWIVWDI
19. prog24 //w.a.p. to find out smallest element in the array. #include<stdio.h> #include<conio.h> void main() { int num[5],small,i; clrscr(); printf("Enter any five numbers:"); for(i=0;i<5;i++) { scanf("%d",&num[i]); } small=num[0]; for(i=0;i<5;i++) { if(small>num[i]) small=num[i]; } printf("the smallest number is: %d",small); getch(); } Wednesday, September 01, 2010 19 PRADEEP DWIVWDI
20. prog25 //w.a.p. to arrange the element in ascending order.(selection sort) #include<stdio.h> #include<conio.h> void main() { int num[5],i,j,temp; clrscr(); printf("Enter five numbers:"); for(i=0;i<5;i++) { scanf("%d",&num[i]); } for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(num[i]>num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } } } for(i=0;i<5;i++) { printf("%d",num[i]); } getch(); } Wednesday, September 01, 2010 20 PRADEEP DWIVWDI
21. TWO DIMENSIONAL ARRAY Wednesday, September 01, 2010 PRADEEP DWIVWDI 21 If we want to arrange the element in a row and column format of an array that time we use two dimensional array. In that first dimensional tells about the number of rows and second dimensional tells about the number of columns. For eg- int a[3][2]; rows columns
22. REPRESENTATION OF TWO DIMENSIONAL ARRAY Wednesday, September 01, 2010 PRADEEP DWIVWDI 22 If we want to represent an array for eg- int a[2][3]; col 0 col 1 col 2 00 01 02 Row 0 Row 1 10 11 12
23. prog26 //write a program to print a matrix #include<stdio.h> #include<conio.h> void main() { int a[3][3],i,j; clrscr(); printf("Enter the array elements:"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } printf("The elements of array are:"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d",a[i][j]); } printf(""); } getch(); } Wednesday, September 01, 2010 23 PRADEEP DWIVWDI