0% found this document useful (0 votes)
2 views118 pages

C Programming & Datastructures Unit-II

This document covers Unit-II of a C Programming and Data Structures course, focusing on arrays, strings, functions, and pointers. It explains the concepts of linear and non-linear arrays, including their initialization and usage, as well as string handling in C. Additionally, it provides example code snippets for array and string operations.

Uploaded by

vikkystar9392
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views118 pages

C Programming & Datastructures Unit-II

This document covers Unit-II of a C Programming and Data Structures course, focusing on arrays, strings, functions, and pointers. It explains the concepts of linear and non-linear arrays, including their initialization and usage, as well as string handling in C. Additionally, it provides example code snippets for array and string operations.

Uploaded by

vikkystar9392
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
MCA I SEMESTER Paper-III: C Programming & Data Structures Unit-II Ms.K R Sravanthi Assistant Professor Department of Computer Science Dr CS Rao PG Centre Sri Y N College(Autonomous), Narsapur Unit-II: Arrays, Strings, Functions and Pointers Arrays: An array is a collection of adjacent memory locations used to store and access several similar data items using single name. (or) An array is a collection of elements of same data type that are stored in the continuous memory locations. Department of Computer Science C Programming and Data Structures Example: {13,26,7,15,10,67,9} are array values. “Space for all items of array is allocated to contiguous memory locations in the RAM and each item is accessed with their index value in the program. For example in the above array group the index of 13 is 0, 26 is 1, and 7 is 2 and so on. Department of Computer Science C Programming and Data Structures Types of Arrays: There are two types of arrays. They are 1. Linear Array (Single Dimension Array) 2. Non - Linear Array (Multi - Dimensional Arrays) Arrays can be extended as any number of dimensions but most of the time we use single or double dimension array, depending on the application. Department of Computer Science Programming and Data Structures The arrays are represented as int x[5]; —--® Single dimensional array representation x{0] x{1]_ x{2] x{3]_ x14] Department of Computer Science C Programming and Data Structures int yls][6]; Y{O}(O}] | Yfo}[4} | YfO}[2) | YfON3] | Yf0O)(4] | Y{O}[5) y(ayio) | (ayia) | veayt2) | ¥(2003) | YEA) | Yeats) Y(2)0] | YE2}(4)_ | vf2N2)_ | Y(2N3)__ | YI2I(4)_| YI2{5) Y3O} | Y(314)— | YISI2)—_ | YIBN3) | YI314)__| YIBII5] veayto) | vaya) | viata) | vais} | viata) | yeas) Initialisation of an array: int a[5]={10,20,30,40,50} 70 120 )30 140 |50 “Each value sets in relative order of array from a[0]. Department of Computer Science C Programming and Data Structures Linear Array (Single Dimension Array): Linear array is also called one dimensional array (or) single dimensional array (or) least array. * A list of items can be given one variable name using only one subscript and such a variable name using only one subscript is called a one dimensional array. Department of Computer Science C Programming and Data Structures To declare a one dimensional array, the following syntax must be used. Syntax: data type array name[size]; Example: int x(7]; x{o]X{1] x[2] x[3]— X14] x(5] —-X{6] Here ‘x’ is an array, which holds 7 integer items. Department of Computer Science C Programming and Data Structures Program: #include #include void main() { int m[5], total=o, avg,i; printf(“enter marks of 5 students:”); for(i=0;i<5;i++) Department of Computer Science C Programming and Data Structures scanf(“%d”,&m[i]); Output: for(i=o;i<5;i++) Enter marks of 5 students total=total+ml[i]; 45,56,55,72,89 avg=total/s; total=317 printf(“\n total=%d, average=%d”,total)| average=63 } Department of Computer Science C Programming and Data Structures Non - Linear Array: An array which is having different dimensions or subscripts is called as non linear. It is again classified into ‘n’ different types. These are Two dimensional array Three dimensional array n - dimensional array Department of Computer Science C Programming and Data Structures Two dimensional array: ** In this there are two subscripts one is for row size and another is for column size. So this array is also called row - column array or square array. “It is having two subscripts and so it is also called double subscripted array. Syntax: data type array - name[row - size][column - size]; Example: int x[5][5]; Here xis an array which is having 5 rows and 5 columns. Department of Computer Science C Programming and Data Structures int x{5][5]; Columns x(0}[0] | xf0}[1] | xf0][2] | xf0][3] __| xf0] [4] Rows [XML | x(t) | xfait2) | x(113)_| x(2(4) x(2](0} | x(2)(4) | xf2)(2)— | x(213)_—_| x24) x(3}(0] | x3)(2) | x{3)(2) | x(3)13) | x(3)14) x{4)(0] | x4)(2) | xf4)(2) | xf4)13) | xt4){4) Department of Computer Science C Programming and Data Structures Initialization of Multi Dimensional Arrays Int a[2][3]={51,35,67,45,23,21); Program: #include void main() int x[2][4], i, j; Department of Computer Science C Programming and Data Structures //scanning the data to an array for(i=0;i<2;i++) { for(j=0;j<45j++) printf( “enter value of x[%d][%d]:”, i,j); scanf(“%d”, &x{il[j]); } } Department of Computer Science C Programming and Data Structures //printing array data for(i=0;i<2;i++) { for(j=0;j<4;j++) printf( “%d”, x[i][j]);//printing each element printf(“\n”);//inserting new line of each row } } Department of Computer Science C Programming and Data Structures Output: enter value of x{o][ enter value of x{o][1] :2 enter value of x[o][2] :2 enter value of x[o][3] :2 enter value of x[iJ[o] :3 3 3 o]:2 enter value of x[1][2] enter value of x[][3] 2222 3333 enter value of x(1][1] 3 Department of Computer Science C Programming and Data Structures Strings: “String is a collection of characters positioned sequentially one after the other used to represent names of items, persons, cities, countries and sometimes to represent message. (or) «A group of characters is nothing but a string. Example: India, China, Hello etc can be taken as strings. Department of Computer Science C Programming and Data Structures The null character and its importance: “Strings are extensively used in almost all real life applications. “These string characters are stored sequentially in the memory. “There should be indication at the end of string. For this, the null character ‘\o’ is inserted at the end of string. Department of Computer Science C Programming and Data Structures String Constants: “A set of zero (or) more characters represented within a pair of double quotes is called a string constant. Example: “India”, “C-family”, ””, “A123”, “123” etc. “*The string INDIA occupies 6 bytes in the memory. 5 bytes for INDIA +1 byte for null. vv ‘No yD’ | ‘WN | \O" Department of Computer Science C Programming and Data Structures String Variables: “We know that variable is a space used to store and access a value, similarly to store and access characters of strings one is required that is “char[]” array. Example: char art[2o]; //here ‘arr’ is an array name with maximum capacity of 20 characters Department of Computer Science C Programming and Data Structures Initialization of Strings: **We can initialize character arrays with the strings are char a[g]= “ C-Family”;//here null character automatically appends char alg]= {‘C, “’, F,,’a’.’m’, i’, T,’y’, ’\o’} Department of Computer Science C Programming and Data Structures I/O functions on Strings: Input functions : scanf(), gets() Output functions : printf(), puts() “The scanf() and printf() are general I/O functions that they support all built-in data types such as int, float, long, double, strings,....etc. Department of Computer Science C Programming and Data Structures “But gets() and puts() are specialized to scan and print only string data. “There is a little difference between scanf() and gets() while reading string from keyboard. “The scanf() accepts character by character from keyboard until either a new line (‘\n’) or a blank space is found, whichever comes earlier, whereas gets() accepts until a new line is found. Department of Computer Science C Programming and Data Structures /*Scanning string with “scanf()”*/ #include printf(“\n output=%s”,a); #include } #include Output: void main() enter a string: { char a[20]; C Programming Language printf(“enter a string\n”); output=C scanf(“%s”, &a); Department of Computer Science C Programming and Data Structures /*Scanning string with “gets()’*/ #include printf(“\n output=%s”,a); #include } #include Output: void main() enter a string: { char a[20]; C Programming Language printf(“enter a string\n”); output= gets(a); C Programming Language Department of Computer Science C Programming and Data Structures

You might also like