You are on page 1of 31

Def: An array is a collection of similar data elements The elements of the array are stored in consecutive memory locations

and are referenced by index(subscript) 1.Declaration of arrays: Data type:- what kind of values it can store Name-: to identify the array Size-: the maximum number of values that array can store Ex:int arr[2];

For accessing an individual element of the array-subscript must be used Eg: arr[3]// to access the fourth element To access all elements of the array loop must be used int marks[100]; Eg: for(int i=0;i<5;i++) Marks[i]=25;

In arrays, the values can be store in three ways1)Initialize the array elements 2)Input values to every individual element 3)Assign values to the elements Initialize the array elements: Elements of the array can be initialized at the time of declaration Syn: type arrayname[size]={list of values}; Float f[3]={1,2,3};
1)

Values will be passed through the keyboard While/do-while/for loop is executed to input the value for each element of the array Eg: for(int i=0;i<2;i++) 3) Assigning values: Passing a value to the individual elements of the array Eg: marks[3]=23;

1) 2) 3) 4) 5)

Traversal Insertion Deletion Merging sorting

Traversing the array element means accessing each and every element of the array for a specific purpose Algorithm: Step1: initialization set I=lower_bound Step2: repeat steps3 to 4 while i<=upper_bound Step3: apply process to A[i]; Step4: set i=i+1;[end of loop] Step5: exit

Algorithm: Step1: set upper_bound=upper_bound+1; Step2: set A[upper_bound]=val; Step3:exit; Insertion of element in the middle of the array Step1:initialization set i=n; Step2: repeat steps 3 and 4 while i>=pos Step3: set a[i+1]=a[i]; Step4: set i=i-1; Step5: set n=n+1; Step6: set a[pos]=val; Step7:exit;

Algorithm: Step1: set upper_bound =upper_bound-1; Step2:exit; Delete an element from the middle of the array Step1:[initialization] set i=pos; Step2: repeat steps 3 and 4 while i<=n-1; Step3: set a[i]=a[i+1]; step4: set i=i+1;//end of loop Step5: set n=n-1; Step6:exit;

Adding two arrays and storing in third array Searching: identifying a particular element in the given array Sorting: arranging the elements in the ascending /descending order

1) 2) 3)

one-dimensional arrays Two-dimensional arrays Multi dimensional arrays

Two-dimensional Array: a collection of a fixed number of components arranged in two dimensions


All components are of the same type

The syntax for declaring a two-dimensional array is:


dataType arrayName[intexp1][intexp2];

where intexp1 and intexp2 are expressions yielding positive integer values

The two expressions intexp1 and intexp2 specify the number of rows and the number of columns, respectively, in the array
Two-dimensional arrays are sometimes called matrices or tables

The syntax to access a component of a twodimensional array is:


arrayName[indexexp1][indexexp2]

where indexexp1 and indexexp2 are expressions yielding nonnegative integer values indexexp1 specifies the row position and indexexp2 specifies the column position

Like one-dimensional arrays

1. Elements of each row are enclosed within braces and separated by commas 2. All rows are enclosed within braces 3. For number arrays, if all components of a row are not specified, the unspecified components are initialized to zero

To initialize a two-dimensional array when it is declared

Two-dimensional arrays can be initialized when they are declared

A two-dimensional array can be processed in three different ways:


1. Process the entire array

2. Process a particular row of the array, called row processing


3. Process a particular column of the array, called column processing

Each row and each column of a twodimensional array is a one-dimensional array


When processing a particular row or column of a two-dimensional array
we use algorithms similar to processing onedimensional arrays

A string is a sequence of characters that is treated as a single data item

Common operations that can be performed on characters strings includes 1. Reading and writing programs 2. Combining strings together 3. Copying one string to another 4. Comparing strings for equality 5. Extracting a portion of a string

C does not support strings as a datatype It allows us to represent strings as a character arrays Declaration of string variable is: Char string_name [size]; When a character array is initialized, it automatically supports a null character \0 at the end of the string

Size of array= maximum number of characters+null;

Char name [10]; Multiple ways to initialize a string: Char name[]=mca; Char name[4]=mca; Char name[]={m,c,a,\0}; Char name[10]=mca;

Scanf can be used with %s format specification to read in a string of characters Ex: char add[10]; Scanf(%s, address); Reading a line of text: C supports a format specification known as the edit set conversion code %[..]used to read a line containing a variety of characters,including white spaces

Ex: char line[80]; Scanf(%[\n], line); Printf(%s,line);

Strcat- concatenates two strings Strcmp compares two strings Strcpy- copies one string to another Strlen- finds the length of the string Strcat() function: adds two strings together Syn: strcat(string1, string2) Strcat function may also append to a string constant Strcat(s1,abc); Strcat can be nested: Strcat(strcat(string1,string2),string3);

Compares two strings If it identifies both the strings are equal- its value is zero If it identifies both the strings are not equal its has the numeric difference Syn: strcmp(string1,string2); Strcmp function may also compare to a string constant Strcmp(string1,abc); Strcmp(abc,abc);

Copies one string to another Syn:strcpy(string1,string2) Eg: strcpy(city,delhi); Strcpy(city1,city2); Strlen-function Counts and returns the number of characters in a string N=len(string); Other string functions: Strncpy- copies only leftmost n characters of the source string to the target string variable Syn:strncpy(s1,s2,n); Strncmp- compares the leftmost n characters

Syn: strncmp(s1,s2,n);

You might also like