210 likes | 226 Views
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 ]
E N D
Arrays 1090CS, Computer Programming 1Manesh 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] • type: represent datatype of the array • arrayname: name of the array • size: number of elements in the array
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
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
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
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)
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.
Run time initialization of Arrays int a[5], i; for(i=0;i<5 i++) { scanf(“%d”, &a[i]); }
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]); • } • }
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]); } }
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); }
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); }
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]); } }
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,
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); }
Strings using Arrays • A string is a sequence of characters treated as a group • String constants are in double quotes • “Well done”
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’
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); }
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); }
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())