1 Arrays 1090CS, Computer Programming 1 Manesh T

21
1 Arrays 1090CS, Computer Programming 1 Manesh T [email protected]

description

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

Transcript of 1 Arrays 1090CS, Computer Programming 1 Manesh T

Page 1: 1 Arrays 1090CS, Computer Programming 1 Manesh T

1

Arrays

1090CS, Computer Programming 1Manesh T

[email protected]

Page 2: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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

Page 3: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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

Page 4: 1 Arrays 1090CS, Computer Programming 1 Manesh T

4

Examples

• int A[5]• An array of ten integers• A[0], A[1], …, A[4]

0 1 2 3 40 1 2 3 4Array of Array of 5 5

elementselements

Array Array indexindex

Element Element of an of an arrayarray

…… …… …… …… ……A

Page 5: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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

Page 6: 1 Arrays 1090CS, Computer Programming 1 Manesh T

Arrays in C 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

Page 7: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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)

Page 8: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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

Page 9: 1 Arrays 1090CS, Computer Programming 1 Manesh T

Run time initialization of Arrays

int a[5], i;for(i=0;i<5 i++){ scanf(“%d”, &a[i]);}

9

Page 10: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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]);• }• • }

Page 11: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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]); } }

Page 12: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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); }

Page 13: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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); }

Page 14: 1 Arrays 1090CS, Computer Programming 1 Manesh T

Program to print even elements of the array

14

#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]); } }

Page 15: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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,

Page 16: 1 Arrays 1090CS, Computer Programming 1 Manesh T

Reading and printing single character

Method 1#include<stdio.h>#include<ctype.h>void main(){ char ch; printf("\nEnter a Character: "); ch=getchar(); putchar(ch);}

Method 2#include<stdio.h>void main(){ char ch; printf("\nEnter a Character: "); scanf(“%c”,&ch); printf(“%c”,ch);}

Page 17: 1 Arrays 1090CS, Computer Programming 1 Manesh T

Strings using Arrays

• A string is a sequence of characters treated as a group

• String constants are in double quotes• “Well done”

Page 18: 1 Arrays 1090CS, Computer Programming 1 Manesh T

Strings using Arrays

• The string is always ended with a null null charactercharacter ‘\0’‘\0’.

• The characters after the null character are ignored.

• e.g., char name[10] = “Well Done”;

e l l d o n eW

[0] [8]

‘\0’

[9]

Page 19: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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);}

Page 20: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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);}

20

Page 21: 1 Arrays 1090CS, Computer Programming 1 Manesh T

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 array5. Program to find sum of all even numbers in an array6. Program to print all array elements in reverse order.7. Program to read and print strings8. Programs using string manipulating functions.

(strcat(),strcpy(),strcmp() and strlen())