1 / 21

Arrays

Arrays. 1090CS, Computer Programming 1 Manesh T maneshpadmayil@gmail.com. Definition – Array. A collection of objects of the same type stored contiguously in memory under one name. It is a collection of variables of the same type. Declaring Arrays. Syntax: type arrayname [ size ]

Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays 1090CS, Computer Programming 1Manesh T maneshpadmayil@gmail.com

  2. Definition – Array • A collection of objects of the same type stored contiguously in memory under one name. • It is a collection of variables of the same type

  3. Declaring Arrays • Syntax: type arrayname[size] • type: represent datatype of the array • arrayname: name of the array • size: number of elements in the array

  4. Examples • int A[5] • An array of ten integers • A[0], A[1], …, A[4] Element of an array Array index 0 1 2 3 4 Array of 5 elements A

  5. Examples (continued) • int C[] • An array of an unknown number of integers (allowable in a parameter of a function) • C[0], C[1], …, C[max-1] • int D[10][20] • An array of ten rows, each of which is an array of twenty integers • D[0][0], D[0][1], …, D[1][0], D[1][1], …, D[9][19] • Not used so often as arrays of pointers

  6. Array Initialization • You can initialize array in C either one by one or using a single statement as follows: • int A[5] = {2, 4, 8, 16, 32}; • Static or automatic • int B[20] = {2, 4, 8, 16, 32}; • Unspecified elements are guaranteed to be zero • int C[4] = {2, 4, 8, 16, 32}; • Error — compiler detects too many initial values • int C[5] = {2, 4, 8}; • Now arrays last three elements will be initialized to zero • char ch[7] ={‘S’,‘A’,’L’,’M’, ‘A’,’N’}; • array initialized to characters • Array indexes always start at zero in C Arrays in C

  7. Implicit Array Size Determination • int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; • Array is created with as many elements as initial values • In this case, 12 elements • Values must be compile-time constants (for static arrays) • Values may be run-time expressions (for automatic arrays)

  8. Compile time initialization of Arrays • int A[5] = {2, 4, 8, 16, 32}; • here array A will be assigned with five elements as mentioned before execution of the program.

  9. Run time initialization of Arrays int a[5], i; for(i=0;i<5 i++) { scanf(“%d”, &a[i]); }

  10. Reading and printing array elements-Method 1 • #include<stdio.h> • void main() OUTPUT • { • int a[5]={12,23,34,45,,10},i,n; • printf("\nArray Elements:\n"); • for(i=0;i<n;i++) • { • printf("%d\n",a[i]); • } • }

  11. Reading and printing array elements-Method 2 #include<stdio.h> void main() { OUTPUT int a[25],i,n; printf("\nEnter no. Elements of Array:"); scanf("%d",&n); printf("\nEnter %d Elements:\n",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nArray Elements:\n"); for(i=0;i<n;i++) { printf("%d\n",a[i]); } }

  12. Program to find sum of elements of the array #include<stdio.h> void main() { int a[25],i,n,sum=0; printf("\nEnter no. Elements of Array:"); scanf("%d",&n); printf("\nEnter %d Elements:\n",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } printf("Sum=%d\n",sum); }

  13. Program to find average of elements of the array #include<stdio.h> void main() { int a[25],i,n,sum=0; float avg; printf("\nEnter no. Elements of Array:"); scanf("%d",&n); printf("\nEnter %d Elements:\n",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } avg=(float)sum/n; printf("Average=%f\n",avg); }

  14. Program to print even elements of the array #include<stdio.h> void main() { int a[25],i,n;; printf("\nEnter no. Elements of Array:"); scanf("%d",&n); printf("\nEnter %d Elements:\n",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nEven Elements:\n"); for(i=0;i<n;i++) { if(a[i]%2==0) printf("%d\n",a[i]); } }

  15. Characters and Strings using Arrays • char is a one-byte data type capable of holding a character • Char ch; • char ch=‘A’; etc. • Character constants • 'a', 'b', 'c', …'z', '0', '1', … '9', '+', '-', '=', '!', '~', etc, '\n', '\t', '\0', etc. • A-Z, a-z, 0-9 are in order,

  16. Reading and printing single character Method 1 Method 2 #include<stdio.h> void main() { char ch; printf("\nEnter a Character: "); scanf(“%c”,&ch); printf(“%c”,ch); } #include<stdio.h> #include<ctype.h> void main() { char ch; printf("\nEnter a Character: "); ch=getchar(); putchar(ch); }

  17. Strings using Arrays • A string is a sequence of characters treated as a group • String constants are in double quotes • “Well done”

  18. Strings using Arrays • The string is always ended with a null character‘\0’. • The characters after the null character are ignored. • e.g., char name[10] = “Well Done”; [0] [8] [9] W e l l d o n e ‘\0’

  19. Reading and printing String-Method 1 #include<stdio.h> #include<ctype.h> void main() { char ch[20]; printf("\nEnter a String: "); scanf("%s",ch); printf("%s",ch); }

  20. Reading and printing String-Method 2 #include<stdio.h> #include<ctype.h> void main() { char ch[20]; printf("\nEnter a String: "); gets(ch); puts(ch); }

  21. Model Programming Excersices • Program to print elements of a array • Program to find sum of elements in an array • Program to find average of elements in an array • Program to print even elements of the array • Program to find sum of all even numbers in an array • Program to print all array elements in reverse order. • Program to read and print strings • Programs using string manipulating functions.(strcat(),strcpy(),strcmp() and strlen())

More Related