Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Arrays 1090CS, Computer Programming 1 Manesh T

Similar presentations


Presentation on theme: "1 Arrays 1090CS, Computer Programming 1 Manesh T"— Presentation transcript:

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

2 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 3

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

5 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 Arrays in C6 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

7 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. 8

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

10 Reading and printing array elements-Method 1 #include 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 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 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 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 14 #include 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 #include void main() { char ch; printf("\nEnter a Character: "); ch=getchar(); putchar(ch); } Method 2 #include void main() { char ch; printf("\nEnter a Character: "); scanf(“%c”,&ch); printf(“%c”,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 null character ‘\0’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”; elldo neW [0][8] ‘\0’ [9]

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

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

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


Download ppt "1 Arrays 1090CS, Computer Programming 1 Manesh T"

Similar presentations


Ads by Google