1 arrays 1090cs, computer programming 1 manesh t [email protected]

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

Upload: cuthbert-joseph

Post on 19-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

1

Arrays

1090CS, Computer Programming 1Manesh T

[email protected]

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

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

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

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

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

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)

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

Run time initialization of Arrays

int a[5], i;

for(i=0;i<5 i++)

{

scanf(“%d”, &a[i]);

}

9

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("\nArray Elements:\n"); for(i=0;i<n;i++) { printf("%d\n",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

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

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

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

}

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 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]

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 1

20

#include<stdio.h>#include<ctype.h>void main(){ char name[25],ch; int i=0; printf("\nEnter a String: "); while((ch=getchar())!='\n') { name[i]=ch; i++; } name[i]='\0'; printf("The String is \n"); i=0; while((ch=name[i])!='\0') { putchar(ch); i++; }}

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())