PREPARED BY-<br />PRADEEP DWIVEDI(persuing  B.TECH-IT)<br />	from HINDUSTAN COLLEGE OF SCIENCE AND 			  TECHNOLOGY(MATHURA...
 ARRAY<br />TOPIC<br />Wednesday, September 01, 2010<br />2<br />PRADEEP DWIVWDI<br />
ARRAY<br />We use fundamental data type, namely char, int, float, double. Variable of these types can store only one value...
EXAMPLE OF ARRAY<br />List of name.<br />List of number.<br />List of students mark.<br />etc.<br />Wednesday, September 0...
DECLARATION AND MEMORY REPRESENTATION OF AN ARRAY<br />               int a[10];<br />0 	    1    2     3    4   5    6   ...
TYPES OF ARRAY<br />We have following types of array-<br />One dimensional array.<br />Two dimensional array.<br />Multidi...
ONE DIMENSIONAL ARRAY<br />A list of item can given one variable name using only one subscript  and such a variable called...
ONE DIMENSIONAL ARRAY<br />The computer reserves five storage in memory-<br />                  num[0]<br />			num[1]<br /...
DECLARATION OF ONE DIMENSIONAL ARRAY<br />Declaration of an array must specifies three things-<br />Type<br />Name<br />Si...
DECLARATION OF ONE DIMENSIONAL ARRAY<br />character array represents maximum number of characters that the string can hold...
DECLARATION OF ONE DIMENSIONAL ARRAY<br />each character of the string treated as an element of array name and stored in t...
INITIALIZATION OF ONE DIMENSIONAL ARRAY<br />an array can be initialized at either of the following stages-<br />at compil...
COMPILE TIME INITIALIZATION<br />the general form of initialization of an array is-<br />the value in the list are separat...
COMPILE TIME INITIALIZATION<br />size may be omitted , in such case compiler allocates enough free space for all initializ...
NOTE<br />At the time of declaration size must be specified of an array.<br />          int a[];<br />If we don’t mention ...
NOTE<br />Wednesday, September 01, 2010<br />PRADEEP DWIVWDI<br />16<br />In character array null character placed after t...
RUN TIME INITIALIZATION<br />Wednesday, September 01, 2010<br />PRADEEP DWIVWDI<br />17<br />an array can be explicitly in...
prog23<br />//Demo for array<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />int num[5],i;<br /...
prog24<br />//w.a.p. to find out smallest element in the array.<br />#include<stdio.h><br />#include<conio.h><br />void ma...
prog25<br />//w.a.p. to arrange the element in ascending order.(selection sort)<br />#include<stdio.h><br />#include<conio...
TWO DIMENSIONAL ARRAY<br />Wednesday, September 01, 2010<br />PRADEEP DWIVWDI<br />21<br />If we want to arrange the eleme...
REPRESENTATION OF TWO DIMENSIONAL ARRAY<br />Wednesday, September 01, 2010<br />PRADEEP DWIVWDI<br />22<br />If we want to...
prog26<br />//write a program to print a matrix<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br /...
Memory representation of prog 26<br />Wednesday, September 01, 2010<br />PRADEEP DWIVWDI<br />24<br />			0      1       2<...
prog27<br />//write a program for matrix addition<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br...
Explaination<br />Wednesday, September 01, 2010<br />PRADEEP DWIVWDI<br />26<br />matric a                 matrix b       ...
Wednesday, September 01, 2010<br />PRADEEP DWIVWDI<br />27<br />THANKS<br />
Upcoming SlideShare
Loading in …5
×

C programming slide c05

5,367 views
5,453 views

Published on

Published in: Technology
4 Comments
1 Like
Statistics
Notes
No Downloads
Views
Total views
5,367
On SlideShare
0
From Embeds
0
Number of Embeds
1,578
Actions
Shares
0
Downloads
83
Comments
4
Likes
1
Embeds 0
No embeds

No notes for slide

C programming slide c05

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

×