You are on page 1of 28

Group of elements of, same type same size stored in continuous memory locations

Declaration Int a[10]; Declares an array with name a and reserves 10 continuous memory location

Array of different data types main() { float candy[365]; */ char code[12]; */ int states[50]; } /* array of 365 floats /* array of 12 chars /* array of 50 ints */

Initialization main() { int powers[8] = {1,2,4,6,8,16,32,64}; } int arr[6] = {0,0,0,0,0,212}; //traditional syntax int arr[6] = {[5] = 212}; // initialize arr[5] to 212

Assigning Array Values main() { int counter, e[50]; for (counter = 0; counter < 50; counter++) { e[counter] = counter;} }

main() { int counter, e[50]; for (counter = 0; counter < 50; counter++) { scanf(%d,&e[counter]);} }

Array Bounds int doofi[20]; index starts at 0 and goes till 19. Programmer must be careful while using index

Ex: Sum and average of n marks using 1D Array Finding the Maximum and minimum in an array Searching an element in an array Reversing the contents of the array Removing duplicates while taking inputs from the user Removing duplicates from an existing array

Searching in an 1D array Main() { int i,n,a[10]; Printf(enter the number of elements); Scanf(%d,&n); Printf(enter the number of elements); For(i=0;i<n;i++) { Scanf(%d,&a[i]); } Printf(enter the element to be searched); Scanf(%d,&e); For(i=0;i<n;i++) { If(a[i]==e) { printf(element found);

Break; } } If(i==n) { printf(element not found); } }

Array order reversal


1. 2. 3.

take the size of the array Take input elements of the array a. initialize I as 0 and j as the total no. of elements of array Repeat steps 5 to 9 while i<j/2 use a temporary variable temp and swap the elements of the array as follows temp=a[i]; a[i]=a[j]; a[j]=temp; Increment i and decrement j End.

4. 5.

6. 7. 8. 9. 10.

Finding minimum and maximum in an array 1. Get the size of the array in n. 2. Take the elements for the array a. 3. Initialize max as the first element of the array 4. Repeat steps 5,6 until i<n 5. if a[i] > max then make max as a[i] 6. increment i 7. Display the value of max. 8. end

Two Dimensional Arrays float rain[5][12]; // 5 arrays of 12 floats each

Initialization

main() { float rain[YEARS][MONTHS] = { {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3 .5,6.6}, {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1 .4,7.3}, {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6 .1,8.4}, {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4 .3,6.2}, {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5. 2} }; } Matrix addition Main() {

Int a[2][3], b[2][3], c[2][3]; Printf(enter the elements of first matrix); For(i=0;i<3;i++) { For(j=0;j<3;j++) { Scanf(%d\n,&a[i][j]); } } Printf(enter the elements of second matrix); For(i=0;i<3;i++) { For(j=0;j<3;j++) { Scanf(%d\n,&a[i][j]); } }

For(i=0;i<3;i++) { For(j=0;j<3;j++) { C[i][j]= a[i][j]+b[i][j]; } } Printf(The resultant matrix); For(i=0;i<3;i++) { For(j=0;j<3;j++) { printf(%d\n,c[i][j]); } } Ex: Searching in an 2D array Ex: Printing Multiplication tables

Multi Dimensional Arrays - int a[3][3][3];

Ex: Matrix operations on a 3D array.

main() {

int arr[5] = { 0 } ; int i, j, temp ; printf(enter the elements of the array); for ( i = 0 ; i <5 ; i++ ) scanf ( "%d", &arr[i] ) ; for ( i = 0 ; i <5 ; i++ ) { for ( j = i+1 ; j <5 ; j++ ) { if ( arr[i] < arr[j] ) { temp = arr[i] ; arr[i] = arr[j] ; arr[j] = temp ; } } } printf ( "\n\nArray after sorting:\n") ;

for ( i = 0 ; i <5 ; i++ ) printf ( "%d\t", arr[i] ) ; getch(); }

Character arrays

Strings Char a[10]; Char a[10]=welcome;

Char a[10]={w,e,l,c,o,m,e}; a is a character array and not a string

char m3[] = "\n Enough about me -- what's your name?"

main() {

Char a[10]; Printf(enter ur name); Gets(a); or scanf(%s,a); //scanf stops with a space and gets //stops with newline Puts(a); or printf(%s,a); }

Scanf and gets overflows the memory

char str1[80] = "An array was initialized to me."; puts(&str1[5]);

Reading inputs character by character

#include<stdio.h> main() { int count=0; while ( getchar() != -1 ) count ++ ; printf( "%d characters\n" , count ) ; getch(); }

#include<stdio.h>

main() { char s[100],c; int count=0,i=0; while ( (c=getchar()) != -1 ) { s[i++]=c;} s[i]='\0'; i=0; while ( s[i] != '\0' ) putchar(s[i++]); getch(); }

To count the alphabets of a given string #include<stdio.h> main() { char c; int count=0 ; while ( ( c = getchar() ) != -1 ) if ( ( c >= 'A' ) && ( c <= 'Z' ) || ( c >= 'a' ) && ( c <= 'z' ) ) count ++ ; printf( "%d letters\n" , count ) ; getch(); } Exercises

1. Length of a string 2. Copy one string to the other 3. Reverse a given string 4. Check which string is greater among 2 strings

You might also like